PHPackages                             bluepsyduck/zend-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. bluepsyduck/zend-autowire-factory

Abandoned → [bluepsyduck/laminas-autowire-factory](/?search=bluepsyduck%2Flaminas-autowire-factory)ArchivedLibrary[Utility &amp; Helpers](/categories/utility)

bluepsyduck/zend-autowire-factory
=================================

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

1.1.0(6y ago)0999↓50%GPL-3.0-or-laterPHPPHP &gt;=7.1

Since May 22Pushed 5y ago1 watchersCompare

[ Source](https://github.com/BluePsyduck/zend-autowire-factory)[ Packagist](https://packagist.org/packages/bluepsyduck/zend-autowire-factory)[ Docs](https://github.com/BluePsyduck/zend-autowire-factory)[ RSS](/packages/bluepsyduck-zend-autowire-factory/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (10)Versions (4)Used By (0)

Zend Auto-Wire Factory
======================

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

[![GitHub release (latest SemVer)](https://camo.githubusercontent.com/e6f70003ef8b90120d51be5855f3c1ae096d2137d9fb4d26a8c706ce73f6c96a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f426c75655073796475636b2f7a656e642d6175746f776972652d666163746f7279)](https://github.com/BluePsyduck/zend-autowire-factory/releases)[![GitHub](https://camo.githubusercontent.com/fd8049ba4ab08308ae8eed5e250c10f7e694bfec330b6f3808ddb2b7e6605a04/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f426c75655073796475636b2f7a656e642d6175746f776972652d666163746f7279)](LICENSE.md)[![Codecov](https://camo.githubusercontent.com/1cb4a2e288b1aabc2a0a0b0bd3d62369d8284b7678ed9a5d56ce6478b16fe6f0/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f67682f426c75655073796475636b2f7a656e642d6175746f776972652d666163746f72793f6c6f676f3d636f6465636f76)](https://codecov.io/gh/BluePsyduck/zend-autowire-factory)

### DEPRECATED: Use package [bluepsyduck/laminas-autowire-factory](https://github.com/BluePsyduck/laminas-autowire-factory) instead.

[](#deprecated-use-package-bluepsyducklaminas-autowire-factory-instead)

[bluepsyduck/laminas-autowire-factory](https://github.com/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
---------------

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

### Resolving strategies

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

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

### AutoWireFactory as AbstractFactory

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

[](#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\ZendAutoWireFactory\AutoWireFactory::setCacheFile('data/cache/autowire-factory.cache.php');
```

ConfigReaderFactory
-------------------

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

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

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

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

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

We want to auto-wire the following service class:

```
class FancyService {
    public function __construct(FancyComponent $component, string $fancyProperty, array $fancyAdapters) {
    }
}

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

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

```
