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 Laminas 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 laminas-mvc.
Table of Contents
Interfaces
- AbstractFactoryInterface
- Interface for an abstract factory.
Properties
- $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.
Methods
- __construct() : 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.
- __invoke() : DispatchableInterface
- Create an object
- canCreate() : bool
- Can the factory create an instance for the service?
- canCallConstructor() : bool
- 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:
[
\Laminas\Filter\FilterPluginManager::class => 'FilterManager',
\Laminas\Validator\ValidatorPluginManager::class => 'ValidatorManager',
]
Methods
__construct()
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.
public
__construct([array<string|int, string> $aliases = [] ]) : mixed
Parameters
- $aliases : array<string|int, string> = []
__invoke()
Create an object
public
__invoke(ContainerInterface $container, mixed $requestedName[, array<string|int, mixed>|null $options = null ]) : DispatchableInterface
Parameters
- $container : ContainerInterface
- $requestedName : mixed
- $options : array<string|int, mixed>|null = null
Return values
DispatchableInterfacecanCreate()
Can the factory create an instance for the service?
public
canCreate(ContainerInterface $container, mixed $requestedName) : bool
Parameters
- $container : ContainerInterface
- $requestedName : mixed
Return values
boolcanCallConstructor()
private
canCallConstructor(string $requestedName) : bool
Parameters
- $requestedName : string
Return values
boolresolveParameter()
Logic common to all parameter resolution.
private
resolveParameter(ReflectionParameter $parameter, ContainerInterface $container, string $requestedName) : mixed
Parameters
- $parameter : ReflectionParameter
- $container : ContainerInterface
- $requestedName : string
Tags
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
callableresolveParameterWithoutConfigService()
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