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
25 * Factory class to create Config objects
32 * Map of config name => callback
35 protected $factoryFunctions = array();
38 * Config objects that have already been created
39 * name => Config object
42 protected $configs = array();
49 public static function getDefaultInstance() {
51 self
::$self = new self
;
52 global $wgConfigRegistry;
53 foreach ( $wgConfigRegistry as $name => $callback ) {
54 self
::$self->register( $name, $callback );
61 * Destroy the default instance
62 * Should only be called inside unit tests
65 public static function destroyDefaultInstance() {
66 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
67 throw new MWException( __METHOD__
. ' was called outside of unit tests' );
74 * Register a new config factory function
75 * Will override if it's already registered
77 * @param callable $callback That takes this ConfigFactory as an argument
78 * @throws InvalidArgumentException If an invalid callback is provided
80 public function register( $name, $callback ) {
81 if ( !is_callable( $callback ) ) {
82 throw new InvalidArgumentException( 'Invalid callback provided' );
84 $this->factoryFunctions
[$name] = $callback;
88 * Create a given Config using the registered callback for $name.
89 * If an object was already created, the same Config object is returned.
90 * @param string $name Name of the extension/component you want a Config object for
91 * 'main' is used for core
92 * @throws ConfigException If a factory function isn't registered for $name
93 * @throws UnexpectedValueException If the factory function returns a non-Config object
96 public function makeConfig( $name ) {
97 if ( !isset( $this->configs
[$name] ) ) {
98 if ( !isset( $this->factoryFunctions
[$name] ) ) {
99 throw new ConfigException( "No registered builder available for $name." );
101 $conf = call_user_func( $this->factoryFunctions
[$name], $this );
102 if ( $conf instanceof Config
) {
103 $this->configs
[$name] = $conf;
105 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
109 return $this->configs
[$name];