Merge "Do not show useless form at Special:ChangeContentModel"
[lhc/web/wiklou.git] / includes / config / ConfigFactory.php
index d52103c..09b0baa 100644 (file)
@@ -42,49 +42,48 @@ class ConfigFactory {
        protected $configs = [];
 
        /**
-        * @var ConfigFactory
-        */
-       private static $self;
-
-       /**
+        * @deprecated since 1.27, use MediaWikiServices::getConfigFactory() instead.
+        *
         * @return ConfigFactory
         */
        public static function getDefaultInstance() {
-               if ( !self::$self ) {
-                       self::$self = new self;
-                       global $wgConfigRegistry;
-                       foreach ( $wgConfigRegistry as $name => $callback ) {
-                               self::$self->register( $name, $callback );
-                       }
-               }
-               return self::$self;
+               return \MediaWiki\MediaWikiServices::getInstance()->getConfigFactory();
        }
 
        /**
-        * Destroy the default instance
-        * Should only be called inside unit tests
-        * @throws MWException
-        * @codeCoverageIgnore
+        * @return string[]
         */
-       public static function destroyDefaultInstance() {
-               if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
-                       throw new MWException( __METHOD__ . ' was called outside of unit tests' );
-               }
-
-               self::$self = null;
+       public function getConfigNames() {
+               return array_keys( $this->factoryFunctions );
        }
 
        /**
-        * Register a new config factory function
-        * Will override if it's already registered
+        * Register a new config factory function.
+        * Will override if it's already registered.
+        * Use "*" for $name to provide a fallback config for all unknown names.
         * @param string $name
-        * @param callable $callback That takes this ConfigFactory as an argument
+        * @param callable|Config $callback A factory callabck that takes this ConfigFactory
+        *        as an argument and returns a Config instance, or an existing Config instance.
         * @throws InvalidArgumentException If an invalid callback is provided
         */
        public function register( $name, $callback ) {
+               if ( $callback instanceof Config ) {
+                       $instance = $callback;
+
+                       // Register a callback anyway, for consistency. Note that getConfigNames()
+                       // relies on $factoryFunctions to have all config names.
+                       $callback = function() use ( $instance ) {
+                               return $instance;
+                       };
+               } else {
+                       $instance = null;
+               }
+
                if ( !is_callable( $callback ) ) {
                        throw new InvalidArgumentException( 'Invalid callback provided' );
                }
+
+               $this->configs[$name] = $instance;
                $this->factoryFunctions[$name] = $callback;
        }
 
@@ -99,10 +98,14 @@ class ConfigFactory {
         */
        public function makeConfig( $name ) {
                if ( !isset( $this->configs[$name] ) ) {
-                       if ( !isset( $this->factoryFunctions[$name] ) ) {
+                       $key = $name;
+                       if ( !isset( $this->factoryFunctions[$key] ) ) {
+                               $key = '*';
+                       }
+                       if ( !isset( $this->factoryFunctions[$key] ) ) {
                                throw new ConfigException( "No registered builder available for $name." );
                        }
-                       $conf = call_user_func( $this->factoryFunctions[$name], $this );
+                       $conf = call_user_func( $this->factoryFunctions[$key], $this );
                        if ( $conf instanceof Config ) {
                                $this->configs[$name] = $conf;
                        } else {
@@ -112,4 +115,5 @@ class ConfigFactory {
 
                return $this->configs[$name];
        }
+
 }