MediaWiki REL1_30
ConfigFactory.php
Go to the documentation of this file.
1<?php
2
24use 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 foreach ( $other->factoryFunctions as $name => $otherFunc ) {
71 if ( !isset( $this->factoryFunctions[$name] ) ) {
72 continue;
73 }
74
75 // if the callback function is the same, salvage the Cache object
76 // XXX: Closures are never equal!
77 if ( isset( $other->configs[$name] )
78 && $this->factoryFunctions[$name] == $otherFunc
79 ) {
80 $this->configs[$name] = $other->configs[$name];
81 unset( $other->configs[$name] );
82 }
83 }
84
85 // disable $other
86 $other->factoryFunctions = [];
87 $other->configs = [];
88 }
89
93 public function getConfigNames() {
94 return array_keys( $this->factoryFunctions );
95 }
96
106 public function register( $name, $callback ) {
107 if ( !is_callable( $callback ) && !( $callback instanceof Config ) ) {
108 throw new InvalidArgumentException( 'Invalid callback provided' );
109 }
110
111 unset( $this->configs[$name] );
112 $this->factoryFunctions[$name] = $callback;
113 }
114
124 public function makeConfig( $name ) {
125 if ( !isset( $this->configs[$name] ) ) {
126 $key = $name;
127 if ( !isset( $this->factoryFunctions[$key] ) ) {
128 $key = '*';
129 }
130 if ( !isset( $this->factoryFunctions[$key] ) ) {
131 throw new ConfigException( "No registered builder available for $name." );
132 }
133
134 if ( $this->factoryFunctions[$key] instanceof Config ) {
135 $conf = $this->factoryFunctions[$key];
136 } else {
137 $conf = call_user_func( $this->factoryFunctions[$key], $this );
138 }
139
140 if ( $conf instanceof Config ) {
141 $this->configs[$name] = $conf;
142 } else {
143 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
144 }
145 }
146
147 return $this->configs[$name];
148 }
149
150}
Exceptions for config failures.
Factory class to create Config objects.
static getDefaultInstance()
makeConfig( $name)
Create a given Config using the registered callback for $name.
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.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
SalvageableService defines an interface for services that are able to salvage state from a previous i...