MediaWiki master
ConfigFactory.php
Go to the documentation of this file.
1<?php
2
24namespace MediaWiki\Config;
25
26use InvalidArgumentException;
28use UnexpectedValueException;
29use Wikimedia\Assert\Assert;
30use Wikimedia\Services\SalvageableService;
31
37class ConfigFactory implements SalvageableService {
38
43 protected $factoryFunctions = [];
44
50 protected $configs = [];
51
58 public static function getDefaultInstance() {
59 wfDeprecated( __METHOD__, '1.27' );
60 return MediaWikiServices::getInstance()->getConfigFactory();
61 }
62
74 public function salvage( SalvageableService $other ) {
75 Assert::parameterType( self::class, $other, '$other' );
76
78 '@phan-var self $other';
79 foreach ( $other->factoryFunctions as $name => $otherFunc ) {
80 if ( !isset( $this->factoryFunctions[$name] ) ) {
81 continue;
82 }
83
84 // if the callback function is the same, salvage the Cache object
85 // XXX: Closures are never equal!
86 if ( isset( $other->configs[$name] )
87 && $this->factoryFunctions[$name] == $otherFunc
88 ) {
89 $this->configs[$name] = $other->configs[$name];
90 unset( $other->configs[$name] );
91 }
92 }
93
94 // disable $other
95 $other->factoryFunctions = [];
96 $other->configs = [];
97 }
98
102 public function getConfigNames() {
103 return array_keys( $this->factoryFunctions );
104 }
105
115 public function register( $name, $callback ) {
116 if ( !is_callable( $callback ) && !( $callback instanceof Config ) ) {
117 if ( is_array( $callback ) ) {
118 $callback = '[ ' . implode( ', ', $callback ) . ' ]';
119 } elseif ( is_object( $callback ) ) {
120 $callback = 'instanceof ' . get_class( $callback );
121 }
122 throw new InvalidArgumentException( 'Invalid callback \'' . $callback . '\' provided' );
123 }
124
125 unset( $this->configs[$name] );
126 $this->factoryFunctions[$name] = $callback;
127 }
128
138 public function makeConfig( $name ) {
139 if ( !isset( $this->configs[$name] ) ) {
140 $key = $name;
141 if ( !isset( $this->factoryFunctions[$key] ) ) {
142 $key = '*';
143 }
144 if ( !isset( $this->factoryFunctions[$key] ) ) {
145 throw new ConfigException( "No registered builder available for $name." );
146 }
147
148 if ( $this->factoryFunctions[$key] instanceof Config ) {
149 $conf = $this->factoryFunctions[$key];
150 } else {
151 $conf = call_user_func( $this->factoryFunctions[$key], $this );
152 }
153
154 if ( $conf instanceof Config ) {
155 $this->configs[$name] = $conf;
156 } else {
157 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
158 }
159 }
160
161 return $this->configs[$name];
162 }
163
164}
165
167class_alias( ConfigFactory::class, 'ConfigFactory' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Factory class to create Config objects.
array $configs
Config objects that have already been created name => Config object.
array $factoryFunctions
Map of config name => callback.
salvage(SalvageableService $other)
Re-uses existing Cache objects from $other.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Interface for configuration instances.
Definition Config.php:32