MediaWiki
1.34.0
|
ServiceContainer provides a generic service to manage named services using lazy instantiation based on instantiator callback functions. More...
Public Member Functions | |
__construct (array $extraInstantiationParams=[]) | |
addServiceManipulator ( $name, callable $manipulator) | |
Add a service manipulator callback for the given service. More... | |
applyWiring (array $serviceInstantiators) | |
Registers multiple services (aka a "wiring"). More... | |
defineService ( $name, callable $instantiator) | |
Define a new service. More... | |
destroy () | |
Destroys all contained service instances that implement the DestructibleService interface. More... | |
disableService ( $name) | |
Disables a service. More... | |
get ( $name) | |
getService ( $name) | |
Returns a service object of the kind associated with $name. More... | |
getServiceNames () | |
has ( $name) | |
hasService ( $name) | |
Returns true if a service is defined for $name, that is, if a call to getService( $name ) would return a service instance. More... | |
importWiring (ServiceContainer $container, $skip=[]) | |
Imports all wiring defined in $container. More... | |
isServiceDisabled ( $name) | |
loadWiringFiles (array $wiringFiles) | |
peekService ( $name) | |
Returns the service instance for $name only if that service has already been instantiated. More... | |
redefineService ( $name, callable $instantiator) | |
Replace an already defined service. More... | |
Protected Member Functions | |
resetService ( $name, $destroy=true) | |
Resets a service by dropping the service instance. More... | |
Private Member Functions | |
createService ( $name) | |
Private Attributes | |
bool | $destroyed = false |
bool[] | $disabled = [] |
disabled status, per service name More... | |
array | $extraInstantiationParams |
callable[] | $serviceInstantiators = [] |
callable[][] | $serviceManipulators = [] |
object[] | $services = [] |
array | $servicesBeingCreated = [] |
Set of services currently being created, to detect loops. More... | |
ServiceContainer provides a generic service to manage named services using lazy instantiation based on instantiator callback functions.
Services managed by an instance of ServiceContainer may or may not implement a common interface.
Definition at line 49 of file ServiceContainer.php.
Wikimedia\Services\ServiceContainer::__construct | ( | array | $extraInstantiationParams = [] | ) |
array | $extraInstantiationParams | Any additional parameters to be passed to the instantiator function when creating a service. This is typically used to provide access to additional ServiceContainers or Config objects. |
Definition at line 91 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\$extraInstantiationParams.
Wikimedia\Services\ServiceContainer::addServiceManipulator | ( | $name, | |
callable | $manipulator | ||
) |
Add a service manipulator callback for the given service.
This method may be used by extensions that need to wrap, replace, or re-configure a service. It would typically be called from a MediaWikiServices hook handler.
The manipulator callback is called just after the service is instantiated. It can call methods on the service to change configuration, or wrap or otherwise replace it.
string | $name | The name of the service to manipulate. |
callable | $manipulator | Callback function that manipulates, wraps or replaces a service instance. The callback receives the new service instance and this ServiceContainer as parameters, as well as any extra instantiation parameters specified when constructing this ServiceContainer. If the callback returns a value, that value replaces the original service instance. |
NoSuchServiceException | if $name is not a known service. |
CannotReplaceActiveServiceException | if the service was already instantiated. |
Definition at line 322 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\hasService().
Wikimedia\Services\ServiceContainer::applyWiring | ( | array | $serviceInstantiators | ) |
Registers multiple services (aka a "wiring").
array | $serviceInstantiators | An associative array mapping service names to instantiator functions. |
Definition at line 145 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\$serviceInstantiators, and Wikimedia\Services\ServiceContainer\defineService().
Referenced by Wikimedia\Services\ServiceContainer\loadWiringFiles().
|
private |
string | $name |
InvalidArgumentException | if $name is not a known service. |
RuntimeException | if a circular dependency is detected. |
Definition at line 445 of file ServiceContainer.php.
Referenced by Wikimedia\Services\ServiceContainer\getService().
Wikimedia\Services\ServiceContainer::defineService | ( | $name, | |
callable | $instantiator | ||
) |
Define a new service.
The service must not be known already.
string | $name | The name of the service to register, for use with getService(). |
callable | $instantiator | Callback that returns a service instance. Will be called with this ServiceContainer instance as the only parameter. Any extra instantiation parameters provided to the constructor will be passed as subsequent parameters when invoking the instantiator. |
RuntimeException | if there is already a service registered as $name. |
Definition at line 252 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\hasService().
Referenced by Wikimedia\Services\ServiceContainer\applyWiring().
Wikimedia\Services\ServiceContainer::destroy | ( | ) |
Destroys all contained service instances that implement the DestructibleService interface.
This will render all services obtained from this ServiceContainer instance unusable. In particular, this will disable access to the storage backend via any of these services. Any future call to getService() will throw an exception.
Implements Wikimedia\Services\DestructibleService.
Definition at line 103 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\getServiceNames(), and Wikimedia\Services\ServiceContainer\peekService().
Wikimedia\Services\ServiceContainer::disableService | ( | $name | ) |
Disables a service.
string | $name | The name of the service to disable. |
RuntimeException | if $name is not a known service. |
Definition at line 355 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\resetService().
Wikimedia\Services\ServiceContainer::get | ( | $name | ) |
Definition at line 434 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\getService().
Wikimedia\Services\ServiceContainer::getService | ( | $name | ) |
Returns a service object of the kind associated with $name.
Services instances are instantiated lazily, on demand. This method may or may not return the same service instance when called multiple times with the same $name.
string | $name | The service name |
NoSuchServiceException | if $name is not a known service. |
ContainerDisabledException | if this container has already been destroyed. |
ServiceDisabledException | if the requested service has been disabled. |
Definition at line 417 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\createService().
Referenced by Wikimedia\Services\ServiceContainer\get().
Wikimedia\Services\ServiceContainer::getServiceNames | ( | ) |
Definition at line 234 of file ServiceContainer.php.
Referenced by Wikimedia\Services\ServiceContainer\destroy().
Wikimedia\Services\ServiceContainer::has | ( | $name | ) |
Definition at line 204 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\hasService().
Wikimedia\Services\ServiceContainer::hasService | ( | $name | ) |
Returns true if a service is defined for $name, that is, if a call to getService( $name ) would return a service instance.
string | $name |
Definition at line 199 of file ServiceContainer.php.
Referenced by Wikimedia\Services\ServiceContainer\addServiceManipulator(), Wikimedia\Services\ServiceContainer\defineService(), Wikimedia\Services\ServiceContainer\has(), Wikimedia\Services\ServiceContainer\peekService(), and Wikimedia\Services\ServiceContainer\redefineService().
Wikimedia\Services\ServiceContainer::importWiring | ( | ServiceContainer | $container, |
$skip = [] |
|||
) |
Imports all wiring defined in $container.
Wiring defined in $container will override any wiring already defined locally. However, already existing service instances will be preserved.
ServiceContainer | $container | |
string[] | $skip | A list of service names to skip during import |
Definition at line 163 of file ServiceContainer.php.
Wikimedia\Services\ServiceContainer::isServiceDisabled | ( | $name | ) |
string | $name |
Definition at line 492 of file ServiceContainer.php.
Wikimedia\Services\ServiceContainer::loadWiringFiles | ( | array | $wiringFiles | ) |
array | $wiringFiles | A list of PHP files to load wiring information from. Each file is loaded using PHP's include mechanism. Each file is expected to return an associative array that maps service names to instantiator functions. |
Definition at line 125 of file ServiceContainer.php.
References $file, and Wikimedia\Services\ServiceContainer\applyWiring().
Wikimedia\Services\ServiceContainer::peekService | ( | $name | ) |
Returns the service instance for $name only if that service has already been instantiated.
This is intended for situations where services get destroyed/cleaned up, so we can avoid creating a service just to destroy it again.
string | $name |
RuntimeException | if $name does not refer to a known service. |
Definition at line 223 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\hasService().
Referenced by Wikimedia\Services\ServiceContainer\destroy(), and Wikimedia\Services\ServiceContainer\resetService().
Wikimedia\Services\ServiceContainer::redefineService | ( | $name, | |
callable | $instantiator | ||
) |
Replace an already defined service.
string | $name | The name of the service to register. |
callable | $instantiator | Callback function that returns a service instance. Will be called with this ServiceContainer instance as the only parameter. The instantiator must return a service compatible with the originally defined service. Any extra instantiation parameters provided to the constructor will be passed as subsequent parameters when invoking the instantiator. |
NoSuchServiceException | if $name is not a known service. |
CannotReplaceActiveServiceException | if the service was already instantiated. |
Definition at line 281 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\hasService().
|
finalprotected |
Resets a service by dropping the service instance.
If the service instances implements DestructibleService, destroy() is called on the service instance.
string | $name | The name of the service to reset. |
bool | $destroy | Whether the service instance should be destroyed if it exists. When set to false, any existing service instance will effectively be detached from the container. |
RuntimeException | if $name is not a known service. |
Definition at line 384 of file ServiceContainer.php.
References Wikimedia\Services\ServiceContainer\peekService().
Referenced by Wikimedia\Services\ServiceContainer\disableService().
|
private |
Definition at line 79 of file ServiceContainer.php.
|
private |
disabled status, per service name
Definition at line 69 of file ServiceContainer.php.
|
private |
Definition at line 74 of file ServiceContainer.php.
Referenced by Wikimedia\Services\ServiceContainer\__construct().
|
private |
Definition at line 59 of file ServiceContainer.php.
Referenced by Wikimedia\Services\ServiceContainer\applyWiring().
|
private |
Definition at line 64 of file ServiceContainer.php.
|
private |
Definition at line 54 of file ServiceContainer.php.
|
private |
Set of services currently being created, to detect loops.
Definition at line 84 of file ServiceContainer.php.