callback * @var array */ protected $factoryFunctions = []; /** * Config objects that have already been created * name => Config object * @var array */ protected $configs = []; /** * @deprecated since 1.27, use MediaWikiServices::getConfigFactory() instead. * * @return ConfigFactory */ public static function getDefaultInstance() { return \MediaWiki\MediaWikiServices::getInstance()->getConfigFactory(); } /** * @return string[] */ public function getConfigNames() { return array_keys( $this->factoryFunctions ); } /** * 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|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; } /** * Create a given Config using the registered callback for $name. * If an object was already created, the same Config object is returned. * @param string $name Name of the extension/component you want a Config object for * 'main' is used for core * @throws ConfigException If a factory function isn't registered for $name * @throws UnexpectedValueException If the factory function returns a non-Config object * @return Config */ public function makeConfig( $name ) { if ( !isset( $this->configs[$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[$key], $this ); if ( $conf instanceof Config ) { $this->configs[$name] = $conf; } else { throw new UnexpectedValueException( "The builder for $name returned a non-Config object." ); } } return $this->configs[$name]; } }