MediaWiki  1.34.0
ConfigFactory.php
Go to the documentation of this file.
1 <?php
2 
23 use Wikimedia\Assert\Assert;
25 
32 
37  protected $factoryFunctions = [];
38 
44  protected $configs = [];
45 
51  public static function getDefaultInstance() {
52  return \MediaWiki\MediaWikiServices::getInstance()->getConfigFactory();
53  }
54 
66  public function salvage( SalvageableService $other ) {
67  Assert::parameterType( self::class, $other, '$other' );
68 
70  '@phan-var self $other';
71  foreach ( $other->factoryFunctions as $name => $otherFunc ) {
72  if ( !isset( $this->factoryFunctions[$name] ) ) {
73  continue;
74  }
75 
76  // if the callback function is the same, salvage the Cache object
77  // XXX: Closures are never equal!
78  if ( isset( $other->configs[$name] )
79  && $this->factoryFunctions[$name] == $otherFunc
80  ) {
81  $this->configs[$name] = $other->configs[$name];
82  unset( $other->configs[$name] );
83  }
84  }
85 
86  // disable $other
87  $other->factoryFunctions = [];
88  $other->configs = [];
89  }
90 
94  public function getConfigNames() {
95  return array_keys( $this->factoryFunctions );
96  }
97 
107  public function register( $name, $callback ) {
108  if ( !is_callable( $callback ) && !( $callback instanceof Config ) ) {
109  if ( is_array( $callback ) ) {
110  $callback = '[ ' . implode( ', ', $callback ) . ' ]';
111  } elseif ( is_object( $callback ) ) {
112  $callback = 'instanceof ' . get_class( $callback );
113  }
114  throw new InvalidArgumentException( 'Invalid callback \'' . $callback . '\' provided' );
115  }
116 
117  unset( $this->configs[$name] );
118  $this->factoryFunctions[$name] = $callback;
119  }
120 
130  public function makeConfig( $name ) {
131  if ( !isset( $this->configs[$name] ) ) {
132  $key = $name;
133  if ( !isset( $this->factoryFunctions[$key] ) ) {
134  $key = '*';
135  }
136  if ( !isset( $this->factoryFunctions[$key] ) ) {
137  throw new ConfigException( "No registered builder available for $name." );
138  }
139 
140  if ( $this->factoryFunctions[$key] instanceof Config ) {
141  $conf = $this->factoryFunctions[$key];
142  } else {
143  $conf = call_user_func( $this->factoryFunctions[$key], $this );
144  }
145 
146  if ( $conf instanceof Config ) {
147  $this->configs[$name] = $conf;
148  } else {
149  throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
150  }
151  }
152 
153  return $this->configs[$name];
154  }
155 
156 }
ConfigFactory\$configs
array $configs
Config objects that have already been created name => Config object.
Definition: ConfigFactory.php:44
Config
Interface for configuration instances.
Definition: Config.php:28
ConfigFactory\salvage
salvage(SalvageableService $other)
Re-uses existing Cache objects from $other.
Definition: ConfigFactory.php:66
ConfigFactory\getDefaultInstance
static getDefaultInstance()
Definition: ConfigFactory.php:51
Wikimedia\Services\SalvageableService
SalvageableService defines an interface for services that are able to salvage state from a previous i...
Definition: SalvageableService.php:36
ConfigFactory\getConfigNames
getConfigNames()
Definition: ConfigFactory.php:94
ConfigFactory
Factory class to create Config objects.
Definition: ConfigFactory.php:31
ConfigFactory\$factoryFunctions
array $factoryFunctions
Map of config name => callback.
Definition: ConfigFactory.php:37