Allow adding of context-based LESS Variables
authorFlorianschmidtwelzow <florian.schmidt.welzow@t-online.de>
Sun, 1 Feb 2015 21:37:34 +0000 (22:37 +0100)
committerMatthew Flaschen <mflaschen@wikimedia.org>
Tue, 10 Feb 2015 21:25:37 +0000 (16:25 -0500)
Add new hook (ResourceLoaderGetLessVars) called in ResourceLoader::getLessVars to
allow context-based less variables. Cache the resulting array to avoid multiple runs
of this hook.

Change-Id: I5a73bbd0ab58f8fe34519931c4f26c90998e3451

docs/hooks.txt
includes/DefaultSettings.php
includes/resourceloader/ResourceLoader.php

index dba6281..f47890d 100644 (file)
@@ -2277,6 +2277,10 @@ configuration variables to JavaScript. Things that depend on the current page
 or request state must be added through MakeGlobalVariablesScript instead.
 &$vars: array( variable name => value )
 
+'ResourceLoaderGetLessVars': Called in ResourceLoader::getLessVars after variables
+from $wgResourceLoaderLESSVars are added. Can be used to add context-based variables.
+&$lessVars: array of variables already added
+
 'ResourceLoaderRegisterModules': Right before modules information is required,
 such as when responding to a resource
 loader request or generating HTML output.
index 82011fe..59a9c42 100644 (file)
@@ -3541,6 +3541,9 @@ $wgResourceLoaderExperimentalAsyncLoading = false;
  *
  * Changes to LESS variables do not trigger cache invalidation.
  *
+ * If the LESS variables need to be dynamic, you can use the
+ * ResourceLoaderGetLessVars hook (since 1.25).
+ *
  * @par Example:
  * @code
  *   $wgResourceLoaderLESSVars = array(
index 15bb13f..1922999 100644 (file)
@@ -35,6 +35,9 @@ class ResourceLoader {
        /** @var bool */
        protected static $debugMode = null;
 
+       /** @var array */
+       private static $lessVars = null;
+
        /**
         * Module name/ResourceLoaderModule object pairs
         * @var array
@@ -1557,9 +1560,13 @@ class ResourceLoader {
         * @return array Map of variable names to string CSS values.
         */
        public static function getLessVars( Config $config ) {
-               $lessVars = $config->get( 'ResourceLoaderLESSVars' );
-               // Sort by key to ensure consistent hashing for cache lookups.
-               ksort( $lessVars );
-               return $lessVars;
+               if ( !self::$lessVars ) {
+                       $lessVars = $config->get( 'ResourceLoaderLESSVars' );
+                       Hooks::run( 'ResourceLoaderGetLessVars', array( &$lessVars ) );
+                       // Sort by key to ensure consistent hashing for cache lookups.
+                       ksort( $lessVars );
+                       self::$lessVars = $lessVars;
+               }
+               return self::$lessVars;
        }
 }