MediaWiki REL1_33
ServiceContainer.php
Go to the documentation of this file.
1<?php
2namespace Wikimedia\Services;
3
4use InvalidArgumentException;
5use RuntimeException;
7
47
51 private $services = [];
52
57
62
66 private $disabled = [];
67
72
76 private $destroyed = false;
77
84 $this->extraInstantiationParams = $extraInstantiationParams;
85 }
86
95 public function destroy() {
96 foreach ( $this->getServiceNames() as $name ) {
97 $service = $this->peekService( $name );
98 if ( $service !== null && $service instanceof DestructibleService ) {
99 $service->destroy();
100 }
101 }
102
103 // Break circular references due to the $this reference in closures, by
104 // erasing the instantiator array. This allows the ServiceContainer to
105 // be deleted when it goes out of scope.
106 $this->serviceInstantiators = [];
107 // Also remove the services themselves, to avoid confusion.
108 $this->services = [];
109 $this->destroyed = true;
110 }
111
117 public function loadWiringFiles( array $wiringFiles ) {
118 foreach ( $wiringFiles as $file ) {
119 // the wiring file is required to return an array of instantiators.
120 $wiring = require $file;
121
122 Assert::postcondition(
123 is_array( $wiring ),
124 "Wiring file $file is expected to return an array!"
125 );
126
127 $this->applyWiring( $wiring );
128 }
129 }
130
138 Assert::parameterElementType( 'callable', $serviceInstantiators, '$serviceInstantiators' );
139
140 foreach ( $serviceInstantiators as $name => $instantiator ) {
141 $this->defineService( $name, $instantiator );
142 }
143 }
144
155 public function importWiring( ServiceContainer $container, $skip = [] ) {
156 $newInstantiators = array_diff_key(
157 $container->serviceInstantiators,
158 array_flip( $skip )
159 );
160
161 $this->serviceInstantiators = array_merge(
162 $this->serviceInstantiators,
163 $newInstantiators
164 );
165
166 $newManipulators = array_diff(
167 array_keys( $container->serviceManipulators ),
168 $skip
169 );
170
171 foreach ( $newManipulators as $name ) {
172 if ( isset( $this->serviceManipulators[$name] ) ) {
173 $this->serviceManipulators[$name] = array_merge(
174 $this->serviceManipulators[$name],
175 $container->serviceManipulators[$name]
176 );
177 } else {
178 $this->serviceManipulators[$name] = $container->serviceManipulators[$name];
179 }
180 }
181 }
182
191 public function hasService( $name ) {
192 return isset( $this->serviceInstantiators[$name] );
193 }
194
210 public function peekService( $name ) {
211 if ( !$this->hasService( $name ) ) {
212 throw new NoSuchServiceException( $name );
213 }
214
215 return $this->services[$name] ?? null;
216 }
217
221 public function getServiceNames() {
222 return array_keys( $this->serviceInstantiators );
223 }
224
239 public function defineService( $name, callable $instantiator ) {
240 Assert::parameterType( 'string', $name, '$name' );
241
242 if ( $this->hasService( $name ) ) {
243 throw new ServiceAlreadyDefinedException( $name );
244 }
245
246 $this->serviceInstantiators[$name] = $instantiator;
247 }
248
268 public function redefineService( $name, callable $instantiator ) {
269 Assert::parameterType( 'string', $name, '$name' );
270
271 if ( !$this->hasService( $name ) ) {
272 throw new NoSuchServiceException( $name );
273 }
274
275 if ( isset( $this->services[$name] ) ) {
276 throw new CannotReplaceActiveServiceException( $name );
277 }
278
279 $this->serviceInstantiators[$name] = $instantiator;
280 unset( $this->disabled[$name] );
281 }
282
309 public function addServiceManipulator( $name, callable $manipulator ) {
310 Assert::parameterType( 'string', $name, '$name' );
311
312 if ( !$this->hasService( $name ) ) {
313 throw new NoSuchServiceException( $name );
314 }
315
316 if ( isset( $this->services[$name] ) ) {
317 throw new CannotReplaceActiveServiceException( $name );
318 }
319
320 $this->serviceManipulators[$name][] = $manipulator;
321 }
322
342 public function disableService( $name ) {
343 $this->resetService( $name );
344
345 $this->disabled[$name] = true;
346 }
347
371 final protected function resetService( $name, $destroy = true ) {
372 Assert::parameterType( 'string', $name, '$name' );
373
374 $instance = $this->peekService( $name );
375
376 if ( $destroy && $instance instanceof DestructibleService ) {
377 $instance->destroy();
378 }
379
380 unset( $this->services[$name] );
381 unset( $this->disabled[$name] );
382 }
383
404 public function getService( $name ) {
405 if ( $this->destroyed ) {
406 throw new ContainerDisabledException();
407 }
408
409 if ( isset( $this->disabled[$name] ) ) {
410 throw new ServiceDisabledException( $name );
411 }
412
413 if ( !isset( $this->services[$name] ) ) {
414 $this->services[$name] = $this->createService( $name );
415 }
416
417 return $this->services[$name];
418 }
419
426 private function createService( $name ) {
427 if ( isset( $this->serviceInstantiators[$name] ) ) {
428 $service = ( $this->serviceInstantiators[$name] )(
429 $this,
430 ...$this->extraInstantiationParams
431 );
432
433 if ( isset( $this->serviceManipulators[$name] ) ) {
434 foreach ( $this->serviceManipulators[$name] as $callback ) {
436 $callback,
437 array_merge( [ $service, $this ], $this->extraInstantiationParams )
438 );
439
440 // If the manipulator callback returns an object, that object replaces
441 // the original service instance. This allows the manipulator to wrap
442 // or fully replace the service.
443 if ( $ret !== null ) {
444 $service = $ret;
445 }
446 }
447 }
448
449 // NOTE: when adding more wiring logic here, make sure importWiring() is kept in sync!
450 } else {
451 throw new NoSuchServiceException( $name );
452 }
453
454 return $service;
455 }
456
462 public function isServiceDisabled( $name ) {
463 return isset( $this->disabled[$name] );
464 }
465}
466
471class_alias( ServiceContainer::class, 'MediaWiki\Services\ServiceContainer' );
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
Exception thrown when trying to replace an already active service.
Exception thrown when trying to access a service on a disabled container or factory.
Exception thrown when the requested service is not known.
Exception thrown when a service was already defined, but the caller expected it to not exist.
ServiceContainer provides a generic service to manage named services using lazy instantiation based o...
getService( $name)
Returns a service object of the kind associated with $name.
addServiceManipulator( $name, callable $manipulator)
Add a service manipulator callback for the given service.
resetService( $name, $destroy=true)
Resets a service by dropping the service instance.
__construct(array $extraInstantiationParams=[])
applyWiring(array $serviceInstantiators)
Registers multiple services (aka a "wiring").
importWiring(ServiceContainer $container, $skip=[])
Imports all wiring defined in $container.
defineService( $name, callable $instantiator)
Define a new service.
redefineService( $name, callable $instantiator)
Replace an already defined service.
destroy()
Destroys all contained service instances that implement the DestructibleService interface.
disableService( $name)
Disables a service.
peekService( $name)
Returns the service instance for $name only if that service has already been instantiated.
bool[] $disabled
disabled status, per service name
hasService( $name)
Returns true if a service is defined for $name, that is, if a call to getService( $name ) would retur...
Exception thrown when trying to access a disabled service.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2003
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:271
DestructibleService defines a standard interface for shutting down a service instance.
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition router.php:42