Zend Framework  3.0
Public Member Functions | Protected Attributes | List of all members
ServiceManager Class Reference

Service Manager. More...

Public Member Functions

 __construct (array $config=[])
 Constructor.
 
 getServiceLocator ()
 Implemented for backwards compatibility with previous plugin managers only.
 
 get ($name)
 
 
 build ($name, array $options=null)
 Build a service by its name, using optional options (such services are NEVER cached).
Parameters
string$name
null | array$options
Returns
mixed
Exceptions
Exception\ServiceNotFoundExceptionIf no factory/abstract factory could be found to create the instance.
Exception\ServiceNotCreatedExceptionIf factory/delegator fails to create the instance.
ContainerExceptionif any other error occurs

 
 has ($name)
 
 
 setAllowOverride ($flag)
 Indicate whether or not the instance is immutable.
 
 getAllowOverride ()
 Retrieve the flag indicating immutability status.
 
 configure (array $config)
 Configure the service manager.
 
 setAlias ($alias, $target)
 Add an alias.
 
 setInvokableClass ($name, $class=null)
 Add an invokable class mapping.
 
 setFactory ($name, $factory)
 Specify a factory for a given service name.
 
 mapLazyService ($name, $class=null)
 Create a lazy service mapping to a class.
 
 addAbstractFactory ($factory)
 Add an abstract factory for resolving services.
 
 addDelegator ($name, $factory)
 Add a delegator for a given service.
 
 addInitializer ($initializer)
 Add an initializer.
 
 setService ($name, $service)
 Map a service.
 
 setShared ($name, $flag)
 Add a service sharing rule.
 

Protected Attributes

 $abstractFactories = []
 
 $aliases = []
 
 $allowOverride = false
 Whether or not changes may be made to this instance.
 
 $creationContext
 
 $delegators = []
 
 $factories = []
 
 $initializers = []
 
 $lazyServices = []
 
 $services = []
 
 $shared = []
 
 $sharedByDefault = true
 
 $configured = false
 

Detailed Description

Service Manager.

Default implementation of the ServiceLocatorInterface, providing capabilities for object creation via:

It also provides the ability to inject specific service instances and to define aliases.

Constructor & Destructor Documentation

__construct ( array  $config = [])

Constructor.

See {

See Also
::configure()} for details on what $config accepts.
Parameters
array$config

Member Function Documentation

addAbstractFactory (   $factory)

Add an abstract factory for resolving services.

Parameters
string | Factory\AbstractFactoryInterface$factoryService name
addDelegator (   $name,
  $factory 
)

Add a delegator for a given service.

Parameters
string$nameService name
string | callable | Factory\DelegatorFactoryInterface$factoryDelegator factory to assign.
addInitializer (   $initializer)

Add an initializer.

Parameters
string | callable | InitializerInterface$initializer
build (   $name,
array  $options = null 
)

Build a service by its name, using optional options (such services are NEVER cached).

Parameters
string$name
null | array$options
Returns
mixed
Exceptions
Exception\ServiceNotFoundExceptionIf no factory/abstract factory could be found to create the instance.
Exception\ServiceNotCreatedExceptionIf factory/delegator fails to create the instance.
ContainerExceptionif any other error occurs

Implements ServiceLocatorInterface.

configure ( array  $config)

Configure the service manager.

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
array$config
Returns
self
Exceptions
ContainerModificationsNotAllowedExceptionif the allow override flag has been toggled off, and a service instance exists for a given service.
get (   $name)

getAllowOverride ( )

Retrieve the flag indicating immutability status.

Returns
bool
getServiceLocator ( )

Implemented for backwards compatibility with previous plugin managers only.

Returns the creation context.

Deprecated:
since 3.0.0. Factories using 3.0 should use the container instance passed to the factory instead.
Returns
ContainerInterface
has (   $name)

mapLazyService (   $name,
  $class = null 
)

Create a lazy service mapping to a class.

Parameters
string$nameService name to map
null | string$classClass to which to map; if not provided, $name will be used for the mapping.
setAlias (   $alias,
  $target 
)

Add an alias.

Parameters
string$alias
string$target
setAllowOverride (   $flag)

Indicate whether or not the instance is immutable.

Parameters
bool$flag
setFactory (   $name,
  $factory 
)

Specify a factory for a given service name.

Parameters
string$nameService name
string | callable | Factory\FactoryInterface$factoryFactory to which to map.
setInvokableClass (   $name,
  $class = null 
)

Add an invokable class mapping.

Parameters
string$nameService name
null | string$classClass to which to map; if omitted, $name is assumed.
setService (   $name,
  $service 
)

Map a service.

Parameters
string$nameService name
array | object$service
setShared (   $name,
  $flag 
)

Add a service sharing rule.

Parameters
string$nameService name
boolean$flagWhether or not the service should be shared.

Member Data Documentation

$abstractFactories = []
protected
$aliases = []
protected
$allowOverride = false
protected

Whether or not changes may be made to this instance.

Parameters
bool
$configured = false
protected
$creationContext
protected
$delegators = []
protected
$factories = []
protected
$initializers = []
protected
$lazyServices = []
protected
$services = []
protected
$shared = []
protected
$sharedByDefault = true
protected