MediaWiki REL1_31
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 if ( is_array( $callback ) ) {
109 $callback = '[ ' . implode( ', ', $callback ) . ' ]';
110 } elseif ( is_object( $callback ) ) {
111 $callback = 'instanceof ' . get_class( $callback );
112 }
113 throw new InvalidArgumentException( 'Invalid callback \'' . $callback . '\' provided' );
114 }
115
116 unset( $this->configs[$name] );
117 $this->factoryFunctions[$name] = $callback;
118 }
119
129 public function makeConfig( $name ) {
130 if ( !isset( $this->configs[$name] ) ) {
131 $key = $name;
132 if ( !isset( $this->factoryFunctions[$key] ) ) {
133 $key = '*';
134 }
135 if ( !isset( $this->factoryFunctions[$key] ) ) {
136 throw new ConfigException( "No registered builder available for $name." );
137 }
138
139 if ( $this->factoryFunctions[$key] instanceof Config ) {
140 $conf = $this->factoryFunctions[$key];
141 } else {
142 $conf = call_user_func( $this->factoryFunctions[$key], $this );
143 }
144
145 if ( $conf instanceof Config ) {
146 $this->configs[$name] = $conf;
147 } else {
148 throw new UnexpectedValueException( "The builder for $name returned a non-Config object." );
149 }
150 }
151
152 return $this->configs[$name];
153 }
154
155}
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.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
Interface for configuration instances.
Definition Config.php:28
SalvageableService defines an interface for services that are able to salvage state from a previous i...