MediaWiki REL1_39
ConfigFactory.php
Go to the documentation of this file.
1<?php
2
25use Wikimedia\Assert\Assert;
26use Wikimedia\Services\SalvageableService;
27
33class ConfigFactory implements SalvageableService {
34
39 protected $factoryFunctions = [];
40
46 protected $configs = [];
47
53 public static function getDefaultInstance() {
54 return MediaWikiServices::getInstance()->getConfigFactory();
55 }
56
68 public function salvage( SalvageableService $other ) {
69 Assert::parameterType( self::class, $other, '$other' );
70
72 '@phan-var self $other';
73 foreach ( $other->factoryFunctions as $name => $otherFunc ) {
74 if ( !isset( $this->factoryFunctions[$name] ) ) {
75 continue;
76 }
77
78 // if the callback function is the same, salvage the Cache object
79 // XXX: Closures are never equal!
80 if ( isset( $other->configs[$name] )
81 && $this->factoryFunctions[$name] == $otherFunc
82 ) {
83 $this->configs[$name] = $other->configs[$name];
84 unset( $other->configs[$name] );
85 }
86 }
87
88 // disable $other
89 $other->factoryFunctions = [];
90 $other->configs = [];
91 }
92
96 public function getConfigNames() {
97 return array_keys( $this->factoryFunctions );
98 }
99
109 public function register( $name, $callback ) {
110 if ( !is_callable( $callback ) && !( $callback instanceof Config ) ) {
111 if ( is_array( $callback ) ) {
112 $callback = '[ ' . implode( ', ', $callback ) . ' ]';
113 } elseif ( is_object( $callback ) ) {
114 $callback = 'instanceof ' . get_class( $callback );
115 }
116 throw new InvalidArgumentException( 'Invalid callback \'' . $callback . '\' provided' );
117 }
118
119 unset( $this->configs[$name] );
120 $this->factoryFunctions[$name] = $callback;
121 }
122
132 public function makeConfig( $name ) {
133 if ( !isset( $this->configs[$name] ) ) {
134 $key = $name;
135 if ( !isset( $this->factoryFunctions[$key] ) ) {
136 $key = '*';
137 }
138 if ( !isset( $this->factoryFunctions[$key] ) ) {
139 throw new ConfigException( "No registered builder available for $name." );
140 }
141
142 if ( $this->factoryFunctions[$key] instanceof Config ) {
143 $conf = $this->factoryFunctions[$key];
144 } else {
145 $conf = call_user_func( $this->factoryFunctions[$key], $this );
146 }
147
148 if ( $conf instanceof Config ) {
149 $this->configs[$name] = $conf;
150 } else {
151 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
152 }
153 }
154
155 return $this->configs[$name];
156 }
157
158}
Factory class to create Config objects.
static getDefaultInstance()
salvage(SalvageableService $other)
Re-uses existing Cache objects from $other.
array $factoryFunctions
Map of config name => callback.
array $configs
Config objects that have already been created name => Config object.
Service locator for MediaWiki core services.
Interface for configuration instances.
Definition Config.php:30