MediaWiki  1.27.2
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=[])
 
 applyWiring (array $serviceInstantiators)
 Registers multiple services (aka a "wiring"). More...
 
 defineService ($name, callable $instantiator)
 Define a new service. More...
 
 getService ($name)
 Returns a service object of the kind associated with $name. More...
 
 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. More...
 
 loadWiringFiles (array $wiringFiles)
 
 redefineService ($name, callable $instantiator)
 Replace an already defined service. More...
 

Private Member Functions

 createService ($name)
 

Private Attributes

array $extraInstantiationParams
 
callable[] $serviceInstantiators = []
 
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

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 68 of file ServiceContainer.php.

References MediaWiki\Services\ServiceContainer\$extraInstantiationParams.

Member Function Documentation

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 97 of file ServiceContainer.php.

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

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

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

Definition at line 209 of file ServiceContainer.php.

References $name.

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

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

Define a new service.

The service must not be known already.

See also
getService().
replaceService().
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 138 of file ServiceContainer.php.

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

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

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
InvalidArgumentExceptionif $name is not a known service.
Returns
object The service instance

Definition at line 195 of file ServiceContainer.php.

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

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

Definition at line 120 of file ServiceContainer.php.

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 113 of file ServiceContainer.php.

References $name.

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

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 77 of file ServiceContainer.php.

References MediaWiki\Services\ServiceContainer\applyWiring(), and as.

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

Replace an already defined service.

See also
defineService().
Note
This causes any previously instantiated instance of the service to be discarded.
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
RuntimeExceptionif $name is not a known service.

Definition at line 164 of file ServiceContainer.php.

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

Member Data Documentation

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

Definition at line 56 of file ServiceContainer.php.

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: