Merge "Fix positioning of jQuery.tipsy tooltip arrows"
[mediawiki.git] / includes / config / ConfigFactory.php
blobb20794b1e7c00e908b76482644140d5a465b4972
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 = array();
37 /**
38 * Config objects that have already been created
39 * name => Config object
40 * @var array
42 protected $configs = array();
44 /**
45 * @var ConfigFactory
47 private static $self;
49 /**
50 * @return ConfigFactory
52 public static function getDefaultInstance() {
53 if ( !self::$self ) {
54 self::$self = new self;
55 global $wgConfigRegistry;
56 foreach ( $wgConfigRegistry as $name => $callback ) {
57 self::$self->register( $name, $callback );
60 return self::$self;
63 /**
64 * Destroy the default instance
65 * Should only be called inside unit tests
66 * @throws MWException
67 * @codeCoverageIgnore
69 public static function destroyDefaultInstance() {
70 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
71 throw new MWException( __METHOD__ . ' was called outside of unit tests' );
74 self::$self = null;
77 /**
78 * Register a new config factory function
79 * Will override if it's already registered
80 * @param string $name
81 * @param callable $callback That takes this ConfigFactory as an argument
82 * @throws InvalidArgumentException If an invalid callback is provided
84 public function register( $name, $callback ) {
85 if ( !is_callable( $callback ) ) {
86 throw new InvalidArgumentException( 'Invalid callback provided' );
88 $this->factoryFunctions[$name] = $callback;
91 /**
92 * Create a given Config using the registered callback for $name.
93 * If an object was already created, the same Config object is returned.
94 * @param string $name Name of the extension/component you want a Config object for
95 * 'main' is used for core
96 * @throws ConfigException If a factory function isn't registered for $name
97 * @throws UnexpectedValueException If the factory function returns a non-Config object
98 * @return Config
100 public function makeConfig( $name ) {
101 if ( !isset( $this->configs[$name] ) ) {
102 if ( !isset( $this->factoryFunctions[$name] ) ) {
103 throw new ConfigException( "No registered builder available for $name." );
105 $conf = call_user_func( $this->factoryFunctions[$name], $this );
106 if ( $conf instanceof Config ) {
107 $this->configs[$name] = $conf;
108 } else {
109 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
113 return $this->configs[$name];