SkinFactory: register skins in Setup.php
authorKunal Mehta <legoktm@gmail.com>
Mon, 11 Aug 2014 11:33:54 +0000 (12:33 +0100)
committerBartosz Dziewoński <matma.rex@gmail.com>
Wed, 13 Aug 2014 11:43:09 +0000 (13:43 +0200)
This un-makes $wgValidSkinNames a legacy thing, and is
more backwards-compatible friendly.

Change-Id: I5c442f3c9e4ee7a4a3980fd02138ee756ef9fa7a

includes/Setup.php
includes/skins/SkinFactory.php
tests/phpunit/includes/resourceloader/ResourceLoaderModuleTest.php

index 935fa15..b100c7a 100644 (file)
@@ -263,8 +263,21 @@ if ( $wgSkipSkin ) {
        $wgSkipSkins[] = $wgSkipSkin;
 }
 
-// Register a hidden "fallback" skin
-$wgValidSkinNames['fallback'] = 'Fallback'; // SkinFallback
+// Register skins
+// Use a closure to avoid leaking into global state
+call_user_func( function() use ( $wgValidSkinNames ) {
+       $factory = SkinFactory::getDefaultInstance();
+       foreach ( $wgValidSkinNames as $name => $skin ) {
+               $factory->register( $name, $skin, function() use ( $skin ) {
+                       $class = "Skin$skin";
+                       return new $class;
+               } );
+       }
+       // Register a hidden "fallback" skin
+       $factory->register( 'fallback', 'Fallback', function() {
+               return new SkinFallback;
+       } );
+} );
 $wgSkipSkins[] = 'fallback';
 
 if ( $wgLocalInterwiki ) {
index 51aced9..52eaddd 100644 (file)
@@ -39,6 +39,13 @@ class SkinFactory {
         */
        private $displayNames = array();
 
+       /**
+        * Map of name => class name without "Skin" prefix
+        * for legacy skins using the autoloader
+        * @var array
+        */
+       private $legacySkins = array();
+
        /**
         * @var SkinFactory
         */
@@ -72,10 +79,9 @@ class SkinFactory {
         * @return array
         */
        private function getLegacySkinNames() {
-               global $wgValidSkinNames;
                static $skinsInitialised = false;
 
-               if ( !$skinsInitialised || !count( $wgValidSkinNames ) ) {
+               if ( !$skinsInitialised || !count( $this->legacySkins ) ) {
                        # Get a list of available skins
                        # Build using the regular expression '^(.*).php$'
                        # Array keys are all lower case, array value keep the case used by filename
@@ -117,7 +123,7 @@ class SkinFactory {
                                                        "The mechanism will be removed in MediaWiki 1.25 and the skin will no longer be recognized. " .
                                                        "See https://www.mediawiki.org/wiki/Manual:Skin_autodiscovery for information how to fix this."
                                                );
-                                               $wgValidSkinNames[strtolower( $aSkin )] = $aSkin;
+                                               $this->legacySkins[strtolower( $aSkin )] = $aSkin;
                                        }
                                }
                                $skinDir->close();
@@ -125,7 +131,7 @@ class SkinFactory {
                        $skinsInitialised = true;
                        wfProfileOut( __METHOD__ . '-init' );
                }
-               return $wgValidSkinNames;
+               return $this->legacySkins;
 
        }
 
index e98f0e8..038a0e1 100644 (file)
@@ -5,9 +5,9 @@ class ResourceLoaderModuleTest extends ResourceLoaderTestCase {
        protected function setUp() {
                parent::setUp();
 
-               $this->setMwGlobals( array(
-                       'wgValidSkinNames' => array( 'vector' => 'Vector' ),
-               );
+               // The return value of the closure shouldn't matter since this test should
+               // never call it
+               SkinFactory::getDefaultInstance()->register( 'vector', 'Vector', function(){});
        }
 
        /**