PHPackages                             ricwein/laminas-autowire-factory - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. ricwein/laminas-autowire-factory

ActiveLibrary[Framework](/categories/framework)

ricwein/laminas-autowire-factory
================================

A Laminas factory implementation allowing for auto-wiring like in Symfony.

3.0.0(1y ago)02.4k↓41.1%GPL-3.0-or-laterPHPPHP ^8.1

Since Jan 8Pushed 1y agoCompare

[ Source](https://github.com/ricwein/laminas-autowire-factory)[ Packagist](https://packagist.org/packages/ricwein/laminas-autowire-factory)[ Docs](https://github.com/ricwein/laminas-autowire-factory)[ RSS](/packages/ricwein-laminas-autowire-factory/feed)WikiDiscussions main Synced yesterday

READMEChangelog (1)Dependencies (10)Versions (7)Used By (0)

Laminas Auto-Wire Factory
=========================

[](#laminas-auto-wire-factory)

[![GitHub release (latest SemVer)](https://camo.githubusercontent.com/3db8080939dc19f9044cf510401aa7003c0f98427a74b41592f4f289ab9509eb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f426c75655073796475636b2f6c616d696e61732d6175746f776972652d666163746f7279)](https://github.com/BluePsyduck/laminas-autowire-factory/releases)[![GitHub](https://camo.githubusercontent.com/eb49d661ef6de96a8f734adb93b06e7585c7042da4b99d0f27a60a074adc6c96/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f426c75655073796475636b2f6c616d696e61732d6175746f776972652d666163746f7279)](LICENSE.md)[![build](https://camo.githubusercontent.com/ed7f9c849ac6513b3dc12e0196cbb3ec5d14ccf6fcb796ec575dad570f44dec4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f426c75655073796475636b2f6c616d696e61732d6175746f776972652d666163746f72792f43493f6c6f676f3d676974687562)](https://github.com/BluePsyduck/laminas-autowire-factory/actions)[![Codecov](https://camo.githubusercontent.com/a21e207dbf3c7a5639b318cdb87d2b345708d4825fcdb58639150e80586bf359/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f67682f426c75655073796475636b2f6c616d696e61732d6175746f776972652d666163746f72793f6c6f676f3d636f6465636f76)](https://codecov.io/gh/BluePsyduck/laminas-autowire-factory)

This library provides few factories and attributes helping with auto-wiring service classes for the [Laminas ServiceManager](https://github.com/laminas/laminas-servicemanager/), to make writing actual factories less common.

Revolver strategies
-------------------

[](#revolver-strategies)

The library provides several strategies to resolve the parameters of a class. All have in common that only constructor parameters are resolved, and never the properties directly. Each parameter is resolved on its own, so one parameter using a certain strategy does not influence other parameters.

The resolving strategy is specified by providing an attribute on the parameter of the constructor. If no resolving attribute is specified, the AutoWireFactory will use the default strategy for resolving.

### Default strategy

[](#default-strategy)

If no other strategy is specified, then the AutoWireFactory will use the default strategy, trying to derive the service from the parameter types and names.

The default strategy is adopting [Symfony's approach](https://symfony.com/doc/current/service_container/autowiring.html)of handling auto wiring, especially [dealing with multiple implementations of the same type](https://symfony.com/doc/current/service_container/autowiring.html#dealing-with-multiple-implementations-of-the-same-type).

The following cases can be handled by the default strategy:

#### Parameter with default values

[](#parameter-with-default-values)

Example: `__construct(string $fancyParameter = 'fancy-value')`

If the parameter has a default value, that value will be used, skipping all other resolving steps.

#### Parameter with class type-hint

[](#parameter-with-class-type-hint)

Example: `__construct(FancyClass $fancy)`

If the parameter has a class name as type-hint, then the following aliases are checked in the container:

1. `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.
2. `FancyClass`: "Default" case of registering a class with its name to the container.
3. `$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

[](#parameter-with-scalar-type-hint)

Example: `__construct(array $fancyConfig)`

If the parameter is type-hinted with a scalar type, e.g. to pull config values into the service, the following aliases are checked:

1. `array $fancyConfig`: The combination of type and parameter name, the same as for class type-hints.
2. `$fancyConfig`: Fallback using only the parameter name.

Note that the type alone, `array`, is not used as alias.

#### Parameter without type-hint

[](#parameter-without-type-hint)

Example: `__construct($fancyParameter)`

In this case, only one alias can be checked due to missing information:

1. `$fancyParameter`: Fallback is the only possible alias.

### Resolve by alias

[](#resolve-by-alias)

The parameter is resolved by specifying the exact alias to request from the container. This is done by using the `Alias` attribute:

```
use BluePsyduck\LaminasAutoWireFactory\Attribute\Alias;

class ClassWithAliasParameter {
    public function __construct(
        #[Alias('alias-for-fancy-class')]
        private FancyClass $fancy,
    ) {}
}
```

In this case, the AutoWireFactory will use the service registered as "alias-for-fancy-class" from the container to resolve `$fancy`.

### Use a scalar value from the config

[](#use-a-scalar-value-from-the-config)

The AutoWireFactory is also able to inject a value from the application config to the service, using the `ReadConfig`attribute. The attribute expects the config keys.

```
use BluePsyduck\LaminasAutoWireFactory\Attribute\ReadConfig;

class ClassWithConfigValue {
    public function __construct(
        #[ReadConfig('foo', 'bar')]
        private string $timeout,
    ) {}
}
```

In this case, the value of `$config['foo']['bar']` is injected into the service. Pay attention that the types match.

As default, the resolver uses the alias `config` to fetch the application config from the container. If your config is available through another alias, set the alias to use via `ReadConfig::$configAlias = 'fancy-config'`. All config-bases resolvers will use this alias.

### Inject an array of services by their aliases

[](#inject-an-array-of-services-by-their-aliases)

There may be the case where the config specifies a list of aliases, of which the corresponding services are needed in the service. For this, the `InjectAliasArray` attribute can be used. Again, the attribute expects the config keys to read the aliases from.

```
use BluePsyduck\LaminasAutoWireFactory\Attribute\InjectAliasArray;

/*

Config: [
    'resolvers' => [
        FancyResolver::class,
        NotSoFancyResolver::class,
    ],
]

 */

class ClassWithAliasArray {
    public function __construct(
        #[InjectAliasArray('resolvers')]
        private array $resolvers,
    ) {}
}
```

In this example, the resolver will read the aliases provided in the "resolvers" config key, and will request the services from the container using these aliases, here the class names of the `FancyResolver` and `NotSoFancyResolver`. The resulting array of services is then passed to the service as `$resolvers`.

AutoWireFactory
---------------

[](#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. It will check for any of the attributes mentioned above to select the strategy, or will fall back to the default strategy if no attribute can be found.

### AutoWireFactory as AbstractFactory

[](#autowirefactory-as-abstractfactory)

Next to the `FactoryInterface` to use the `AutoWireFactory`as an explicit factory in the container configuration, the AutoWireFactory 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, except for services which still need custom factories.

### Caching

[](#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:

```
\BluePsyduck\LaminasAutoWireFactory\AutoWireFactory::setCacheFile('data/cache/autowire-factory.cache');
```

Additional Factories
--------------------

[](#additional-factories)

The library provides additional factories with which you can specify how certain parameters should be resolved. All these factories have the same functionality as their corresponding attribute mentioned above. The factories are intended to be used directly in the container configuration, instead of using an attribute on the constructor. Note though that the attributes will be preferred, as the additional configs are only used by the default strategy (when no attribute is present).

### ConfigReaderFactory

[](#configreaderfactory)

This factory can be used instead of the `ReadConfig` attribute, taking again the config keys to read the value from.

There are two ways to use the factory:

```
// dependencies.php

use BluePsyduck\LaminasAutoWireFactory\Factory\ConfigReaderFactory;
use BluePsyduck\LaminasAutoWireFactory\AutoWireUtils;

    'factories' => [
        // Either instantiate the factory directly:
        'int $timeout' => new ConfigReaderFactory('fancy-service', 'timeout'),
        // Or use the Utils class instead:
        'int $timeout' => AutoWireUtils::readConfig('fancy-service', 'timeout'),
    ]
```

In both cases, the `$config['fancy-service']['timeout']` would be registered to the container to be available for the default resolving strategy.

### AliasArrayInjectorFactory

[](#aliasarrayinjectorfactory)

This factory can be used instead of the `InjectAliasArray` attribute, taking again the config keys to read the aliases from.

There are again two ways to use the factory:

```
// dependencies.php

use BluePsyduck\LaminasAutoWireFactory\Factory\AliasArrayInjectorFactory;
use BluePsyduck\LaminasAutoWireFactory\AutoWireUtils;

    'factories' => [
        // Either instantiate the factory directly:
        'array $resolvers' => new AliasArrayInjectorFactory('resolvers'),
        // Or use the Utils class instead:
        'array $resolvers' => AutoWireUtils::injectAliasArray('resolvers'),
    ]
```

In both cases, the `$config['resolvers']` is used to as aliases for the container, for which the received instances can be used in the default resolving strategy.

Examples
--------

[](#examples)

To help better understand of how the AutoWireFactory works, two full examples shall be given. Both examples do the same thing, with the first one using attributes, and the second one using the additional factories. It is up to you to decide which variant you want to use.

While the examples use constructor property promotion to specify the properties and parameters at the same time, all features also work on non-promoted parameters as well.

### Example 1: Using Attributes

[](#example-1-using-attributes)

The following example shows how to use the `AutoWireFactory` and the attributes to auto-wire a service class.

Let's assume we have the following application config from which we want to take a value:

```
[
    'fancy-service' => [
        'fancy-property' => 'Hello World!',
        'fancy-adapters' => [
            FancyAdapterAlpha::class,
            FancyAdapterOmega::class,
        ],
    ],
]
```

We want to auto-wire the following service class:

```
use BluePsyduck\LaminasAutoWireFactory\Attribute\ReadConfig;
use BluePsyduck\LaminasAutoWireFactory\Attribute\InjectAliasArray;

class FancyService {
    public function __construct(
        private FancyComponent $component,
        #[ReadConfig('fancy-service', 'fancy-property')]
        private string $fancyProperty,
        #[InjectAliasArray('fancy-service', 'fancy-adapters')]
        private array $fancyAdapters,
    ) {}
}

class FancyComponent {}
class FancyAdapterAlpha {}
class FancyAdapterOmega {}
```

The first parameter of the constructor does not have an attribute specified, so the default type-based resolving strategy is used. For the other two parameters, an attribute is specified, so those will be resolved accordingly.

The following configuration can be used for the container without writing any factories:

```
