ServiceManager
in package
implements
ServiceLocatorInterface
Service Manager.
Default implementation of the ServiceLocatorInterface, providing capabilities for object creation via:
- factories
- abstract factories
- delegator factories
- lazy service factories (generated proxies)
- initializers (interface injection)
It also provides the ability to inject specific service instances and to define aliases.
Interfaces, Classes, Traits and Enums
- ServiceLocatorInterface
- Interface for service locator
Table of Contents
- $abstractFactories : array<string|int, AbstractFactoryInterface>
- $aliases : array<string|int, string>
- A list of aliases
- $allowOverride : mixed
- Whether or not changes may be made to this instance.
- $configured : bool
- Service manager was already configured?
- $creationContext : ContainerInterface
- $delegators : array<string|int, array<string|int, string>>|array<string|int, array<string|int, DelegatorFactoryInterface>>
- $factories : array<string|int, string>|array<string|int, callable>
- A list of factories (either as string name or callable)
- $initializers : array<string|int, InitializerInterface>|array<string|int, callable>
- $lazyServices : array<string|int, mixed>
- $services : array<string|int, mixed>
- A list of already loaded services (this act as a local cache)
- $shared : array<string|int, bool>
- Enable/disable shared instances by service name.
- $sharedByDefault : bool
- Should the services be shared by default?
- $cachedAbstractFactories : array<string|int, mixed>
- Cached abstract factories from string.
- $lazyServicesDelegator : null|LazyServiceFactory
- $resolvedAliases : array<string|int, string>
- __construct() : mixed
- Constructor.
- addAbstractFactory() : mixed
- Add an abstract factory for resolving services.
- addDelegator() : mixed
- Add a delegator for a given service.
- addInitializer() : mixed
- Add an initializer.
- build() : mixed
- Build a service by its name, using optional options (such services are NEVER cached).
- configure() : self
- Configure the service manager
- get() : mixed
- {@inheritDoc}
- getAllowOverride() : bool
- Retrieve the flag indicating immutability status.
- getServiceLocator() : ContainerInterface
- Implemented for backwards compatibility with previous plugin managers only.
- has() : mixed
- {@inheritDoc}
- mapLazyService() : mixed
- Create a lazy service mapping to a class.
- setAlias() : mixed
- Add an alias.
- setAllowOverride() : mixed
- Indicate whether or not the instance is immutable.
- setFactory() : mixed
- Specify a factory for a given service name.
- setInvokableClass() : mixed
- Add an invokable class mapping.
- setService() : mixed
- Map a service.
- setShared() : mixed
- Add a service sharing rule.
- configureAliases() : void
- createAliasesForInvokables() : array<string|int, mixed>
- Create aliases for invokable classes.
- createDelegatorFromName() : object
- createFactoriesForInvokables() : array<string|int, mixed>
- Create invokable factories for invokable classes.
- createLazyServiceDelegatorFactory() : LazyServiceFactory
- Create the lazy services delegator factory.
- doCreate() : mixed
- Create a new instance with an already resolved name
- getFactory() : callable
- Get a factory for the given service name
- resolveAbstractFactories() : void
- Instantiate abstract factories for to avoid checks during service construction.
- resolveAliases() : void
- Resolve aliases to their canonical service names.
- resolveInitializers() : void
- Instantiate initializers for to avoid checks during service construction.
- resolveNewAliasesWithPreviouslyResolvedAliases() : void
- Rewrites the map of aliases by resolving the given $aliases with the existing resolved ones.
- validateOverrides() : mixed
- Determine if one or more services already exist in the container.
- validateOverrideSet() : mixed
- Determine if one or more services already exist for a given type.
Properties
$abstractFactories
protected
array<string|int, AbstractFactoryInterface>
$abstractFactories
= []
$aliases
A list of aliases
protected
array<string|int, string>
$aliases
= []
Should map one alias to a service name, or another alias (aliases are recursively resolved)
$allowOverride
Whether or not changes may be made to this instance.
protected
mixed
$allowOverride
= false
$configured
Service manager was already configured?
protected
bool
$configured
= false
$creationContext
protected
ContainerInterface
$creationContext
$delegators
protected
array<string|int, array<string|int, string>>|array<string|int, array<string|int, DelegatorFactoryInterface>>
$delegators
= []
$factories
A list of factories (either as string name or callable)
protected
array<string|int, string>|array<string|int, callable>
$factories
= []
$initializers
protected
array<string|int, InitializerInterface>|array<string|int, callable>
$initializers
= []
$lazyServices
protected
array<string|int, mixed>
$lazyServices
= []
$services
A list of already loaded services (this act as a local cache)
protected
array<string|int, mixed>
$services
= []
$shared
Enable/disable shared instances by service name.
protected
array<string|int, bool>
$shared
= []
Example configuration:
'shared' => [ MyService::class => true, // will be shared, even if "sharedByDefault" is false MyOtherService::class => false // won't be shared, even if "sharedByDefault" is true ]
$sharedByDefault
Should the services be shared by default?
protected
bool
$sharedByDefault
= true
$cachedAbstractFactories
Cached abstract factories from string.
private
array<string|int, mixed>
$cachedAbstractFactories
= []
$lazyServicesDelegator
private
null|LazyServiceFactory
$lazyServicesDelegator
$resolvedAliases
private
array<string|int, string>
$resolvedAliases
= []
Methods
__construct()
Constructor.
public
__construct([array<string|int, mixed> $config = [] ]) : mixed
See ServiceManager::configure() for details on what $config accepts.
Parameters
- $config : array<string|int, mixed> = []
Return values
mixed —addAbstractFactory()
Add an abstract factory for resolving services.
public
addAbstractFactory(string|AbstractFactoryInterface $factory) : mixed
Parameters
- $factory : string|AbstractFactoryInterface
-
Service name
Return values
mixed —addDelegator()
Add a delegator for a given service.
public
addDelegator(string $name, string|callable|DelegatorFactoryInterface $factory) : mixed
Parameters
- $name : string
-
Service name
- $factory : string|callable|DelegatorFactoryInterface
-
Delegator factory to assign.
Return values
mixed —addInitializer()
Add an initializer.
public
addInitializer(string|callable|InitializerInterface $initializer) : mixed
Parameters
- $initializer : string|callable|InitializerInterface
Return values
mixed —build()
Build a service by its name, using optional options (such services are NEVER cached).
public
build(mixed $name[, array<string|int, mixed> $options = null ]) : mixed
Parameters
- $name : mixed
- $options : array<string|int, mixed> = null
Return values
mixed —configure()
Configure the service manager
public
configure(array<string|int, mixed> $config) : self
Valid top keys are:
- services: service name => service instance pairs
- invokables: service name => class name pairs for classes that do not have required constructor arguments; internally, maps the class to an InvokableFactory instance, and creates an alias if the service name and class name do not match.
- factories: service name => factory pairs; factories may be any callable, string name resolving to an invokable class, or string name resolving to a FactoryInterface instance.
- abstract_factories: an array of abstract factories; these may be instances of AbstractFactoryInterface, or string names resolving to classes that implement that interface.
- delegators: service name => list of delegator factories for the given service; each item in the list may be a callable, a string name resolving to an invokable class, or a string name resolving to a class implementing DelegatorFactoryInterface.
- shared: service name => flag pairs; the flag is a boolean indicating whether or not the service is shared.
- aliases: alias => service name pairs.
- lazy_services: lazy service configuration; can contain the keys:
- class_map: service name => class name pairs.
- proxies_namespace: string namespace to use for generated proxy classes.
- proxies_target_dir: directory in which to write generated proxy classes; uses system temporary by default.
- write_proxy_files: boolean indicating whether generated proxy classes should be written; defaults to boolean false.
- shared_by_default: boolean, indicating if services in this instance should be shared by default.
Parameters
- $config : array<string|int, mixed>
Tags
Return values
self —get()
{@inheritDoc}
public
get(mixed $name) : mixed
Parameters
- $name : mixed
Return values
mixed —getAllowOverride()
Retrieve the flag indicating immutability status.
public
getAllowOverride() : bool
Return values
bool —getServiceLocator()
Implemented for backwards compatibility with previous plugin managers only.
public
getServiceLocator() : ContainerInterface
Returns the creation context.
Tags
Return values
ContainerInterface —has()
{@inheritDoc}
public
has(mixed $name) : mixed
Parameters
- $name : mixed
Return values
mixed —mapLazyService()
Create a lazy service mapping to a class.
public
mapLazyService(string $name[, null|string $class = null ]) : mixed
Parameters
- $name : string
-
Service name to map
- $class : null|string = null
-
Class to which to map; if not provided, $name will be used for the mapping.
Return values
mixed —setAlias()
Add an alias.
public
setAlias(string $alias, string $target) : mixed
Parameters
- $alias : string
- $target : string
Return values
mixed —setAllowOverride()
Indicate whether or not the instance is immutable.
public
setAllowOverride(bool $flag) : mixed
Parameters
- $flag : bool
Return values
mixed —setFactory()
Specify a factory for a given service name.
public
setFactory(string $name, string|callable|FactoryInterface $factory) : mixed
Parameters
- $name : string
-
Service name
- $factory : string|callable|FactoryInterface
-
Factory to which to map.
Return values
mixed —setInvokableClass()
Add an invokable class mapping.
public
setInvokableClass(string $name[, null|string $class = null ]) : mixed
Parameters
- $name : string
-
Service name
- $class : null|string = null
-
Class to which to map; if omitted, $name is assumed.
Return values
mixed —setService()
Map a service.
public
setService(string $name, array<string|int, mixed>|object $service) : mixed
Parameters
- $name : string
-
Service name
- $service : array<string|int, mixed>|object
Return values
mixed —setShared()
Add a service sharing rule.
public
setShared(string $name, bool $flag) : mixed
Parameters
- $name : string
-
Service name
- $flag : bool
-
Whether or not the service should be shared.
Return values
mixed —configureAliases()
private
configureAliases(array<string|int, string> $aliases) : void
Parameters
- $aliases : array<string|int, string>
Return values
void —createAliasesForInvokables()
Create aliases for invokable classes.
private
createAliasesForInvokables(array<string|int, mixed> $invokables) : array<string|int, mixed>
If an invokable service name does not match the class it maps to, this creates an alias to the class (which will later be mapped as an invokable factory).
Parameters
- $invokables : array<string|int, mixed>
Return values
array<string|int, mixed> —createDelegatorFromName()
private
createDelegatorFromName(string $name[, null|array<string|int, mixed> $options = null ]) : object
Parameters
- $name : string
- $options : null|array<string|int, mixed> = null
Return values
object —createFactoriesForInvokables()
Create invokable factories for invokable classes.
private
createFactoriesForInvokables(array<string|int, mixed> $invokables) : array<string|int, mixed>
If an invokable service name does not match the class it maps to, this creates an invokable factory entry for the class name; otherwise, it creates an invokable factory for the entry name.
Parameters
- $invokables : array<string|int, mixed>
Return values
array<string|int, mixed> —createLazyServiceDelegatorFactory()
Create the lazy services delegator factory.
private
createLazyServiceDelegatorFactory() : LazyServiceFactory
Creates the lazy services delegator factory based on the lazy_services configuration present.
Tags
Return values
LazyServiceFactory —doCreate()
Create a new instance with an already resolved name
private
doCreate(string $resolvedName[, null|array<string|int, mixed> $options = null ]) : mixed
This is a highly performance sensitive method, do not modify if you have not benchmarked it carefully
Parameters
- $resolvedName : string
- $options : null|array<string|int, mixed> = null
Tags
Return values
mixed —getFactory()
Get a factory for the given service name
private
getFactory(string $name) : callable
Parameters
- $name : string
Tags
Return values
callable —resolveAbstractFactories()
Instantiate abstract factories for to avoid checks during service construction.
private
resolveAbstractFactories(array<string|int, string>|array<string|int, AbstractFactoryInterface> $abstractFactories) : void
Parameters
- $abstractFactories : array<string|int, string>|array<string|int, AbstractFactoryInterface>
Return values
void —resolveAliases()
Resolve aliases to their canonical service names.
private
resolveAliases(array<string|int, string> $aliases) : void
Parameters
- $aliases : array<string|int, string>
Return values
void —resolveInitializers()
Instantiate initializers for to avoid checks during service construction.
private
resolveInitializers(array<string|int, string>|array<string|int, InitializerInterface>|array<string|int, callable> $initializers) : void
Parameters
- $initializers : array<string|int, string>|array<string|int, InitializerInterface>|array<string|int, callable>
Return values
void —resolveNewAliasesWithPreviouslyResolvedAliases()
Rewrites the map of aliases by resolving the given $aliases with the existing resolved ones.
private
resolveNewAliasesWithPreviouslyResolvedAliases(array<string|int, string> $aliases) : void
This is mostly done for performance reasons.
Parameters
- $aliases : array<string|int, string>
Return values
void —validateOverrides()
Determine if one or more services already exist in the container.
private
validateOverrides(array<string|int, mixed> $config) : mixed
If the allow override flag is true or it's first time configured, this method does nothing.
Otherwise, it checks against each of the following service types, if present, and validates that none are defining services that already exist; if they do, it raises an exception indicating modification is not allowed.
Parameters
- $config : array<string|int, mixed>
Tags
Return values
mixed —validateOverrideSet()
Determine if one or more services already exist for a given type.
private
validateOverrideSet(array<string|int, string> $services, string $type) : mixed
Loops through the provided service names, checking if any have current service instances; if not, it returns, but otherwise, it raises an exception indicating modification is not allowed.
Parameters
- $services : array<string|int, string>
- $type : string
-
Type of service being checked.