MediaWiki REL1_32
MediaWiki\Services\ServiceContainer Class Reference

ServiceContainer provides a generic service to manage named services using lazy instantiation based on instantiator callback functions. More...

Inheritance diagram for MediaWiki\Services\ServiceContainer:
Collaboration diagram for MediaWiki\Services\ServiceContainer:

Public Member Functions

 __construct (array $extraInstantiationParams=[])
 
 addServiceManipulator ( $name, callable $manipulator)
 Add a service manipulator callback for the given service.
 
 applyWiring (array $serviceInstantiators)
 Registers multiple services (aka a "wiring").
 
 defineService ( $name, callable $instantiator)
 Define a new service.
 
 destroy ()
 Destroys all contained service instances that implement the DestructibleService interface.
 
 disableService ( $name)
 Disables a service.
 
 getService ( $name)
 Returns a service object of the kind associated with $name.
 
 getServiceNames ()
 
 hasService ( $name)
 Returns true if a service is defined for $name, that is, if a call to getService( $name ) would return a service instance.
 
 importWiring (ServiceContainer $container, $skip=[])
 Imports all wiring defined in $container.
 
 isServiceDisabled ( $name)
 
 loadWiringFiles (array $wiringFiles)
 
 peekService ( $name)
 Returns the service instance for $name only if that service has already been instantiated.
 
 redefineService ( $name, callable $instantiator)
 Replace an already defined service.
 

Protected Member Functions

 resetService ( $name, $destroy=true)
 Resets a service by dropping the service instance.
 

Private Member Functions

 createService ( $name)
 

Private Attributes

bool $destroyed = false
 
bool[] $disabled = []
 disabled status, per service name
 
array $extraInstantiationParams
 
callable[] $serviceInstantiators = []
 
callable[][] $serviceManipulators = []
 
object[] $services = []
 

Detailed Description

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.

Note
When using ServiceContainer to manage a set of services, consider creating a wrapper or a subclass that provides access to the services via getter methods with more meaningful names and more specific return type declarations.
See also
docs/injection.txt for an overview of using dependency injection in the MediaWiki code base.

Definition at line 46 of file ServiceContainer.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Services\ServiceContainer::__construct ( array $extraInstantiationParams = [])
Parameters
array$extraInstantiationParamsAny 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 83 of file ServiceContainer.php.

References MediaWiki\Services\ServiceContainer\$extraInstantiationParams.

Member Function Documentation

◆ addServiceManipulator()

MediaWiki\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.

See also
defineService().
redefineService().
Note
This will fail if the service was already instantiated.
Since
1.32
Parameters
string$nameThe name of the service to manipulate.
callable$manipulatorCallback function that manipulates, wraps or replaces a service instance. The callback receives the new service instance and this the 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.
Exceptions
NoSuchServiceExceptionif $name is not a known service.
CannotReplaceActiveServiceExceptionif the service was already instantiated.

Definition at line 309 of file ServiceContainer.php.

References $name, and MediaWiki\Services\ServiceContainer\hasService().

◆ applyWiring()

MediaWiki\Services\ServiceContainer::applyWiring ( array $serviceInstantiators)

Registers multiple services (aka a "wiring").

Parameters
array$serviceInstantiatorsAn associative array mapping service names to instantiator functions.

Definition at line 137 of file ServiceContainer.php.

References MediaWiki\Services\ServiceContainer\$serviceInstantiators, and MediaWiki\Services\ServiceContainer\defineService().

Referenced by MediaWiki\Services\ServiceContainer\loadWiringFiles().

◆ createService()

MediaWiki\Services\ServiceContainer::createService ( $name)
private
Parameters
string$name
Exceptions
InvalidArgumentExceptionif $name is not a known service.
Returns
object

Definition at line 426 of file ServiceContainer.php.

References $name, and $ret.

Referenced by MediaWiki\Services\ServiceContainer\getService().

◆ defineService()

MediaWiki\Services\ServiceContainer::defineService ( $name,
callable $instantiator )

Define a new service.

The service must not be known already.

See also
getService().
redefineService().
Parameters
string$nameThe name of the service to register, for use with getService().
callable$instantiatorCallback that returns a service instance. Will be called with this MediaWikiServices instance as the only parameter. Any extra instantiation parameters provided to the constructor will be passed as subsequent parameters when invoking the instantiator.
Exceptions
RuntimeExceptionif there is already a service registered as $name.

Definition at line 239 of file ServiceContainer.php.

References $name, and MediaWiki\Services\ServiceContainer\hasService().

Referenced by MediaWiki\Services\ServiceContainer\applyWiring().

◆ destroy()

MediaWiki\Services\ServiceContainer::destroy ( )

Destroys all contained service instances that implement the DestructibleService interface.

This will render all services obtained from this MediaWikiServices 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.

See also
resetGlobalInstance()

Implements MediaWiki\Services\DestructibleService.

Definition at line 95 of file ServiceContainer.php.

References MediaWiki\Services\ServiceContainer\getServiceNames(), and MediaWiki\Services\ServiceContainer\peekService().

◆ disableService()

MediaWiki\Services\ServiceContainer::disableService ( $name)

Disables a service.

Note
Attempts to call getService() for a disabled service will result in a DisabledServiceException. Calling peekService for a disabled service will return null. Disabled services are listed by getServiceNames(). A disabled service can be enabled again using redefineService().
If the service was already active (that is, instantiated) when getting disabled, and the service instance implements DestructibleService, destroy() is called on the service instance.
See also
redefineService()
resetService()
Parameters
string$nameThe name of the service to disable.
Exceptions
RuntimeExceptionif $name is not a known service.

Definition at line 342 of file ServiceContainer.php.

References $name, and MediaWiki\Services\ServiceContainer\resetService().

◆ getService()

MediaWiki\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.

Note
Rather than calling this method directly, it is recommended to provide getters with more meaningful names and more specific return types, using a subclass or wrapper.
See also
redefineService().
Parameters
string$nameThe service name
Exceptions
NoSuchServiceExceptionif $name is not a known service.
ContainerDisabledExceptionif this container has already been destroyed.
ServiceDisabledExceptionif the requested service has been disabled.
Returns
object The service instance

Definition at line 404 of file ServiceContainer.php.

References $name, and MediaWiki\Services\ServiceContainer\createService().

◆ getServiceNames()

MediaWiki\Services\ServiceContainer::getServiceNames ( )
Returns
string[]

Definition at line 221 of file ServiceContainer.php.

Referenced by MediaWiki\Services\ServiceContainer\destroy().

◆ hasService()

MediaWiki\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.

Parameters
string$name
Returns
bool

Definition at line 191 of file ServiceContainer.php.

Referenced by MediaWiki\Services\ServiceContainer\addServiceManipulator(), MediaWiki\Services\ServiceContainer\defineService(), MediaWiki\Services\ServiceContainer\peekService(), and MediaWiki\Services\ServiceContainer\redefineService().

◆ importWiring()

MediaWiki\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.

Since
1.28
Parameters
ServiceContainer$container
string[]$skipA list of service names to skip during import

Definition at line 155 of file ServiceContainer.php.

References $name.

◆ isServiceDisabled()

MediaWiki\Services\ServiceContainer::isServiceDisabled ( $name)
Parameters
string$name
Returns
bool Whether the service is disabled
Since
1.28

Definition at line 462 of file ServiceContainer.php.

◆ loadWiringFiles()

MediaWiki\Services\ServiceContainer::loadWiringFiles ( array $wiringFiles)
Parameters
array$wiringFilesA 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 117 of file ServiceContainer.php.

References MediaWiki\Services\ServiceContainer\applyWiring().

◆ peekService()

MediaWiki\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.

Note
This is intended for internal use and for test fixtures. Application logic should use getService() instead.
See also
getService().
Parameters
string$name
Returns
object|null The service instance, or null if the service has not yet been instantiated.
Exceptions
RuntimeExceptionif $name does not refer to a known service.

Definition at line 210 of file ServiceContainer.php.

References $name, and MediaWiki\Services\ServiceContainer\hasService().

Referenced by MediaWiki\Services\ServiceContainer\destroy(), and MediaWiki\Services\ServiceContainer\resetService().

◆ redefineService()

MediaWiki\Services\ServiceContainer::redefineService ( $name,
callable $instantiator )

Replace an already defined service.

See also
defineService().
Note
This will fail if the service was already instantiated. If the service was previously disabled, it will be re-enabled by this call. Any manipulators registered for the service will remain in place.
Parameters
string$nameThe name of the service to register.
callable$instantiatorCallback function that returns a service instance. Will be called with this MediaWikiServices 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.
Exceptions
NoSuchServiceExceptionif $name is not a known service.
CannotReplaceActiveServiceExceptionif the service was already instantiated.

Definition at line 268 of file ServiceContainer.php.

References $name, and MediaWiki\Services\ServiceContainer\hasService().

◆ resetService()

MediaWiki\Services\ServiceContainer::resetService ( $name,
$destroy = true )
finalprotected

Resets a service by dropping the service instance.

If the service instances implements DestructibleService, destroy() is called on the service instance.

Warning
This is generally unsafe! Other services may still retain references to the stale service instance, leading to failures and inconsistencies. Subclasses may use this method to reset specific services under specific instances, but it should not be exposed to application logic.
Note
This is declared final so subclasses can not interfere with the expectations disableService() has when calling resetService().
See also
redefineService()
disableService().
Parameters
string$nameThe name of the service to reset.
bool$destroyWhether the service instance should be destroyed if it exists. When set to false, any existing service instance will effectively be detached from the container.
Exceptions
RuntimeExceptionif $name is not a known service.

Definition at line 371 of file ServiceContainer.php.

References MediaWiki\Services\ServiceContainer\peekService().

Referenced by MediaWiki\Services\ServiceContainer\disableService().

Member Data Documentation

◆ $destroyed

bool MediaWiki\Services\ServiceContainer::$destroyed = false
private

Definition at line 76 of file ServiceContainer.php.

◆ $disabled

bool [] MediaWiki\Services\ServiceContainer::$disabled = []
private

disabled status, per service name

Definition at line 66 of file ServiceContainer.php.

◆ $extraInstantiationParams

array MediaWiki\Services\ServiceContainer::$extraInstantiationParams
private

◆ $serviceInstantiators

callable [] MediaWiki\Services\ServiceContainer::$serviceInstantiators = []
private

◆ $serviceManipulators

callable [][] MediaWiki\Services\ServiceContainer::$serviceManipulators = []
private

Definition at line 61 of file ServiceContainer.php.

◆ $services

object [] MediaWiki\Services\ServiceContainer::$services = []
private

Definition at line 51 of file ServiceContainer.php.


The documentation for this class was generated from the following file: