Download the PHP package bluepsyduck/zend-autowire-factory without Composer
On this page you can find all versions of the php package bluepsyduck/zend-autowire-factory. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bluepsyduck/zend-autowire-factory
More information about bluepsyduck/zend-autowire-factory
Files in bluepsyduck/zend-autowire-factory
Package zend-autowire-factory
Short Description A Zend factory implementation allowing for auto-wiring like in Symfony.
License GPL-3.0-or-later
Homepage https://github.com/BluePsyduck/zend-autowire-factory
Informations about the package zend-autowire-factory
Zend Auto-Wire Factory
DEPRECATED: Use package bluepsyduck/laminas-autowire-factory instead.
bluepsyduck/laminas-autowire-factory contains the same functionality as this package, with the only difference being the namespace. Except for the imports no changes are required.
This library provides few factories helping with auto-wiring service classes to make writing actual factories less common.
AutoWireFactory
The AutoWireFactory
uses reflection on the constructor of the actual service class to determine how to resolve the
dependencies and creating the actual service. The factory is adopting
Symfony's approach of handling auto wiring,
especially dealing with multiple implementations of the same type.
Resolving strategies
The factory uses the following strategies to resolve a parameter of the constructor, depending on how it is type-hinted. The first alias available in the container will be used to resolve the dependency. If no alias is available, an exception gets triggered.
Each parameter is resolved on its own, so they can be combined in any way.
Parameter with class type-hint
Example:
If the parameter has a class name as type-hint, then the following aliases are checked in the container:
FancyClass $fancy
: The combination of class name and parameter name. This allows for multiple implementations of the same interface as stated in the Symphony documentation.FancyClass
: "Default" case of registering a class with its name to the container.$fancy
: Fallback of using the parameter name alone, mostly to make the aliases uniform between cases.
The first alias which can be provided by the container will be used.
Parameter with scalar type-hint
Example:
If the parameter is type-hinted with a scalar type, e.g. to pull config values into the service, the following aliases are checked:
array $fancyConfig
: The combination of type and parameter name, the same as for class type-hints.$fancyConfig
: Fallback using only the parameter name.
Note that the type alone, array
, is not used as alias.
Parameter without type-hint
Example:
In this case, only one alias can be checked due to missing information:
$fancyParameter
: Fallback is the only possible alias.
AutoWireFactory as AbstractFactory
Next to the FactoryInterface
to use the AutoWireFactory
as an explicit factory in the container configuration,
it also implements the AbstractFactoryInterface
: If you add this factory as an abstract factory, it will try
to auto-wire everything it can. This will make configuring the container mostly obsolete, with the exception of
parameters using scalar values or multiple implementations (where the parameter name is part of the container alias).
Caching
The AutoWireFactory
uses reflections to resolve dependencies. To make things faster, the factory offers building up
a cache on the filesystem to avoid using reflections on each script call. To enable the cache, add the following line
e.g. in the config/container.php
file:
ConfigReaderFactory
To help further with making self-written factories obsolete, the ConfigReaderFactory
is able to provide values from
the application config to the container to be e.g. used together with auto-wiring.
Usage
The ConfigReaderFactory
requires the application config to be added as array to the container. If the alias for the
config differs from the default "config", call ConfigReaderFactory::setConfigAlias('yourAlias')
to set the
alias.
Then, use the readConfig(string ...$keys)
function (or new ConfigReaderFactory(string ...$keys)
) to read a config
value for the container, where $keys
are the array keys to reach your desired value in the config. Note that if a key
is not set, an exception will be triggered by the factory.
AliasArrayInjectorFactory
The AliasArrayInjectorFactory
reads an array of aliases from the config (using the ConfigReaderFactory
), and creates
all instances to these aliases and returns them to be injected into other services. All aliases must be known to the
container.
To use this factory, simply call injectAliasArray(string ...$configKeys)
(or
new AliasArrayInjectorFactory(string ...$configKeys)
) within the container config.
Example
The following example should show how to use both the AutoWireFactory
and the ConfigReaderFactory
to auto-wire a
service class.
Let's assume we have the following application config from which we want to take a value:
We want to auto-wire the following service class:
The following configuration can be used for the container without writing any factories:
This configuration can be made even shorter if we use the AutoWireFactory
as an abstract factory:
Of course it is always possible to add a concrete factory to any service if auto-wiring is not possible due to more complex initialization requirements.