ReflectionBasedAbstractFactory
in package
implements
AbstractFactoryInterface
Reflection-based factory.
To ease development, this factory may be used for classes with type-hinted arguments that resolve to services in the application container; this allows omitting the step of writing a factory for each controller.
You may use it as either an abstract factory:
'service_manager' => [
'abstract_factories' => [
ReflectionBasedAbstractFactory::class,
],
],
Or as a factory, mapping a class name to it:
'service_manager' => [
'factories' => [
MyClassWithDependencies::class => ReflectionBasedAbstractFactory::class,
],
],
The latter approach is more explicit, and also more performant.
The factory has the following constraints/features:
- A parameter named
$config
typehinted as an array will receive the application "config" service (i.e., the merged configuration). - Parameters type-hinted against array, but not named
$config
will be injected with an empty array. - Scalar parameters will result in an exception being thrown, unless a default value is present; if the default is present, that will be used.
- If a service cannot be found for a given typehint, the factory will raise an exception detailing this.
- Some services provided by Zend Framework components do not have entries based on their class name (for historical reasons); the factory allows defining a map of these class/interface names to the corresponding service name to allow them to resolve.
$options
passed to the factory are ignored in all cases, as we cannot
make assumptions about which argument(s) they might replace.
Based on the LazyControllerAbstractFactory from zend-mvc.
Interfaces, Classes, Traits and Enums
- AbstractFactoryInterface
- Interface for an abstract factory.
Table of Contents
- $aliases : array<string|int, string>
- Maps known classes/interfaces to the service that provides them; only required for those services with no entry based on the class/interface name.
- __construct() : mixed
- Constructor.
- __invoke() : DispatchableInterface
- {@inheritDoc}
- canCreate() : bool
- Can the factory create an instance for the service?
- canCallConstructor() : mixed
- resolveParameter() : mixed
- Logic common to all parameter resolution.
- resolveParameterWithConfigService() : callable
- Returns a callback for resolving a parameter to a value, including mapping 'config' arguments.
- resolveParameterWithoutConfigService() : callable
- Resolve a parameter to a value.
Properties
$aliases
Maps known classes/interfaces to the service that provides them; only required for those services with no entry based on the class/interface name.
protected
array<string|int, string>
$aliases
= []
Extend the class if you wish to add to the list.
Example:
[
\Zend\Filter\FilterPluginManager::class => 'FilterManager',
\Zend\Validator\ValidatorPluginManager::class => 'ValidatorManager',
]
Methods
__construct()
Constructor.
public
__construct([array<string|int, string> $aliases = [] ]) : mixed
Allows overriding the internal list of aliases. These should be of the
form class name => well-known service name
; see the documentation for
the $aliases
property for details on what is accepted.
Parameters
- $aliases : array<string|int, string> = []
Return values
mixed —__invoke()
{@inheritDoc}
public
__invoke(ContainerInterface $container, mixed $requestedName[, array<string|int, mixed> $options = null ]) : DispatchableInterface
Parameters
- $container : ContainerInterface
- $requestedName : mixed
- $options : array<string|int, mixed> = null
Return values
DispatchableInterface —canCreate()
Can the factory create an instance for the service?
public
canCreate(ContainerInterface $container, mixed $requestedName) : bool
Parameters
- $container : ContainerInterface
- $requestedName : mixed
Return values
bool —canCallConstructor()
private
canCallConstructor(mixed $requestedName) : mixed
Parameters
- $requestedName : mixed
Return values
mixed —resolveParameter()
Logic common to all parameter resolution.
private
resolveParameter(ReflectionParameter $parameter, ContainerInterface $container, string $requestedName) : mixed
Parameters
- $parameter : ReflectionParameter
- $container : ContainerInterface
- $requestedName : string
Tags
Return values
mixed —resolveParameterWithConfigService()
Returns a callback for resolving a parameter to a value, including mapping 'config' arguments.
private
resolveParameterWithConfigService(ContainerInterface $container, string $requestedName) : callable
Unlike resolveParameter(), this version will detect $config
array
arguments and have them return the 'config' service.
Parameters
- $container : ContainerInterface
- $requestedName : string
Return values
callable —resolveParameterWithoutConfigService()
Resolve a parameter to a value.
private
resolveParameterWithoutConfigService(ContainerInterface $container, string $requestedName) : callable
Returns a callback for resolving a parameter to a value, but without
allowing mapping array $config
arguments to the config
service.
Parameters
- $container : ContainerInterface
- $requestedName : string