Run generateLocalAutoload.php
[mediawiki.git] / includes / config / ConfigFactory.php
blob09b0baa7be85f716ee595ab0bd6ca53ffe08a63b
1 <?php
3 /**
4 * Copyright 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
24 /**
25 * Factory class to create Config objects
27 * @since 1.23
29 class ConfigFactory {
31 /**
32 * Map of config name => callback
33 * @var array
35 protected $factoryFunctions = [];
37 /**
38 * Config objects that have already been created
39 * name => Config object
40 * @var array
42 protected $configs = [];
44 /**
45 * @deprecated since 1.27, use MediaWikiServices::getConfigFactory() instead.
47 * @return ConfigFactory
49 public static function getDefaultInstance() {
50 return \MediaWiki\MediaWikiServices::getInstance()->getConfigFactory();
53 /**
54 * @return string[]
56 public function getConfigNames() {
57 return array_keys( $this->factoryFunctions );
60 /**
61 * Register a new config factory function.
62 * Will override if it's already registered.
63 * Use "*" for $name to provide a fallback config for all unknown names.
64 * @param string $name
65 * @param callable|Config $callback A factory callabck that takes this ConfigFactory
66 * as an argument and returns a Config instance, or an existing Config instance.
67 * @throws InvalidArgumentException If an invalid callback is provided
69 public function register( $name, $callback ) {
70 if ( $callback instanceof Config ) {
71 $instance = $callback;
73 // Register a callback anyway, for consistency. Note that getConfigNames()
74 // relies on $factoryFunctions to have all config names.
75 $callback = function() use ( $instance ) {
76 return $instance;
78 } else {
79 $instance = null;
82 if ( !is_callable( $callback ) ) {
83 throw new InvalidArgumentException( 'Invalid callback provided' );
86 $this->configs[$name] = $instance;
87 $this->factoryFunctions[$name] = $callback;
90 /**
91 * Create a given Config using the registered callback for $name.
92 * If an object was already created, the same Config object is returned.
93 * @param string $name Name of the extension/component you want a Config object for
94 * 'main' is used for core
95 * @throws ConfigException If a factory function isn't registered for $name
96 * @throws UnexpectedValueException If the factory function returns a non-Config object
97 * @return Config
99 public function makeConfig( $name ) {
100 if ( !isset( $this->configs[$name] ) ) {
101 $key = $name;
102 if ( !isset( $this->factoryFunctions[$key] ) ) {
103 $key = '*';
105 if ( !isset( $this->factoryFunctions[$key] ) ) {
106 throw new ConfigException( "No registered builder available for $name." );
108 $conf = call_user_func( $this->factoryFunctions[$key], $this );
109 if ( $conf instanceof Config ) {
110 $this->configs[$name] = $conf;
111 } else {
112 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
116 return $this->configs[$name];