PHPackages                             reinfi/zf-dependency-injection - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. reinfi/zf-dependency-injection

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

reinfi/zf-dependency-injection
==============================

A Laminas Framework module for loading dependencies via annotation or config entries.

7.3.1(4mo ago)21115.4k↓27.5%5[1 issues](https://github.com/reinfi/zf-dependency-injection/issues)[8 PRs](https://github.com/reinfi/zf-dependency-injection/pulls)1MITPHPPHP ~8.3.0 || ~8.4.0CI passing

Since Aug 19Pushed 1mo ago5 watchersCompare

[ Source](https://github.com/reinfi/zf-dependency-injection)[ Packagist](https://packagist.org/packages/reinfi/zf-dependency-injection)[ RSS](/packages/reinfi-zf-dependency-injection/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (22)Versions (81)Used By (1)

Configure dependency injection in Laminas or Mezzio using attributes (PHP 8.0+), yaml or autowiring.

Heavily inspired by .

=======

1. [Installation](#installation)
2. [AutoWiring](#autowiring)
3. [Attributes](#attributes)
4. [Annotations (Deprecated)](#annotations)
5. [YAML (Deprecated)](#yaml)
6. [Caching](#caching)
7. [PHPStan Extension](#phpstan-extension)
8. [Rector Sets](#rector-sets)
9. [Console commands](#console-commands)

### Installation

[](#installation)

1. Install with Composer: `composer require reinfi/zf-dependency-injection`.
2. Enable the module via config in `application.config.php` under `modules` key:

```
    return [
        'modules' => [
            'Reinfi\DependencyInjection',
            // other modules
        ],
    ];
```

### AutoWiring

[](#autowiring)

To use autowiring for your service, you need to specify the 'AutoWiringFactory' within the service manager configuration.

```
'service_manager' => [
    'factories' => [
        YourService::class => \Reinfi\DependencyInjection\Factory\AutoWiringFactory::class,
    ],
]
```

#### Fallback AutoWiring

[](#fallback-autowiring)

If you are migrating your existing project to use zend service manager and don't want to register all autowired services by hand, you can register the abstract `FallbackAutoWiringFactory` factory.

Please make sure that you don't use the fallback mechanism for everything. You should try to write and register explicit factories for your services.

```
'service_manager' => [
    'abstract_factories' => [
        \Reinfi\DependencyInjection\AbstractFactory\FallbackAutoWiringFactory::class,
    ],
]
```

##### What can be autowired?

[](#what-can-be-autowired)

Every service registered within the service manager can be autowired. Plugins within the plugin manager can also be autowired. If you need to register another mapping, you can add the following:

```
PluginManagerResolver::addMapping('MyInterfaceClass', 'MyPluginManager');
```

If your service needs the container as a dependency, this can also be autowired.

##### Add another resolver

[](#add-another-resolver)

If you like to add another resolver, you can add one through the configuration.

```
'reinfi.dependencyInjection' => [
    'autowire_resolver' => [
        AnotherResolver::class,
    ],
]
```

It needs to implement the ResolverInterface.

### Attributes

[](#attributes)

Attributes are activated if you are using a php version 8.0 or higher. To use attributes for your dependencies, you need to specify the 'InjectionFactory' within the service manager configuration.

```
'service_manager' => [
    'factories' => [
        YourService::class => \Reinfi\DependencyInjection\Factory\InjectionFactory::class,
    ],
]
```

The following attributes are supported:

- Inject (directly injects a service from the service locator)
- InjectParent (must be used if you inject a service from a plugin manager)
- InjectConfig (dot-separated path to a config value, e.g., service\_manager.factories)
- InjectContainer (directly inject container interface)

Also, in addition, there are several annotations to inject from plugin managers.

- InjectViewHelper
- InjectFilter
- InjectInputFilter
- InjectValidator
- InjectHydrator
- InjectFormElement

You can either pass directly the required service name, or if you need options, you can pass them as follows:

```
#[InjectFormElement(name="Service", options={"field": "value"}]
```

If you need a doctrine repository, there is also an attribute.

- InjectDoctrineRepository

It is only constructor injection supported, if you need di from setters, you need to use delegator factories.

You can add the attributes at properties or at the \_\_construct method.

```
#[Inject("Namespace\MyService")]
private MyService $service;

public function __construct(MyService $service)
{
    $this->service = $service;
}
```

or

```
private MyService $service;

#[Inject("Namespace\MyService")]
public function __construct(MyService $service)
{
    $this->service = $service;
}
```

The order is important and you should decide between constructor or property annotations.

### Annotations (Deprecated)

[](#annotations-deprecated)

> **Note**: Annotations are deprecated and will be removed in a future version. Please use Attributes instead, which provide the same functionality with a more modern syntax.

To use annotations for your dependencies you need to specify the 'InjectionFactory' within the service manager configuration.

```
'service_manager' => [
    'factories' => [
        YourService::class => \Reinfi\DependencyInjection\Factory\InjectionFactory::class,
    ],
]
```

The following annotations are supported:

- Inject (directly injects a service from the service locator)
- InjectParent (must be used if you inject a service from a plugin manager)
- InjectConfig (dot-separated path to a config value, e.g., service\_manager.factories)
- InjectContainer (directly inject container interface)

Also, in addition, there are several annotations to inject from plugin managers.

- InjectViewHelper
- InjectFilter
- InjectInputFilter
- InjectValidator
- InjectHydrator
- InjectFormElement

You can either pass directly the required service name, or if you need options, you can pass them as follows:

```
@InjectFormElement(name="Service", options={"field": "value"})
```

If you need a doctrine repository, there is also an annotation.

- InjectDoctrineRepository

It is only constructor injection supported, if you need di from setters, you need to use delegator factories.

You can add the annotations at properties or at the \_\_construct method.

```
use Reinfi\DependencyInjection\Annotation\Inject;

/**
 * @Inject("Namespace\MyService")
 */
private MyService $service;

/**
 * @param MyService $service
 */
public function __construct(
    MyService $service,
) {
    $this->service = $service;
}
```

or

```
use Reinfi\DependencyInjection\Annotation\Inject;

/**
 * @Inject("Namespace\MyService")
 */
public function __construct(MyService $service)
{
    $this->service = $service;
}
```

The order is important, and you should decide between constructor or property annotations.

##### Adding own annotations

[](#adding-own-annotations)

If you want to use your own annotation, you need to implement the AnnotationInterface.

### YAML (Deprecated)

[](#yaml-deprecated)

> **Note**: YAML configuration is deprecated and will be removed in a future version. Please use Attributes or Autowiring instead, which provide the same functionality with a more modern approach.

You can specify your dependencies within a yaml file.

```
YourService:
  - {type: Inject, value: AnotherService}
```

To enable YAML usage, you need to specify the following configuration

```
'reinfi.dependencyInjection' => [
    'extractor' => YamlExtractor::class,
    'extractor_options => [
        'file' => 'path_to_your_yaml_file.yml',
    ],
]
```

### Caching

[](#caching)

Parsing mapping sources is very heavy. You *should* enable the cache on production servers. You can set up caching easily with any custom or pre-existing PSR-16 cache adapter.

You can provide a string which will be resolved by the container. The factory must return a PSR-16 cache adapter.

```
'reinfi.dependencyInjection' => [
    'cache' => \Laminas\Cache\Storage\Adapter\Memory::class,
]
```

or you provide a factory for a cache adapter.

```
'reinfi.dependencyInjection' => [
    'cache' => function() {
       return new \Laminas\Cache\Psr\SimpleCache\SimpleCacheDecorator(
           new \Laminas\Cache\Storage\Adapter\Memory()
       );
    },
]
```

### PHPStan Extension

[](#phpstan-extension)

As "autowiring" is always kind of magic, this library ships with a PHPStan extension to solve that problem.

If you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) then you're all set!

 Manual installationIf you don't want to use `phpstan/extension-installer`, include phpstan-extension.neon in your project's PHPStan config:

```
includes:
    - vendor/reinfi/zf-dependency-injection/phpstan-extension.neon

```

The extension requires knowing your service manager to find all the classes you configured for autowiring.

If you do not provide it, the PHPStan extension will simply do nothing.

```
parameters:
    reinfiLaminasDependencyInjection:
       serviceManagerLoader: tests/service-manager.php
```

For example, `tests/service-manager.php` would look something like this:

```
$app = \Laminas\Mvc\Application::init($config);
return $app->getServiceManager();
```

### Rector Sets

[](#rector-sets)

If you want to migrate your codebase to use attributes instead of annotations, you can use the provided rector sets.

You can use the following rector set to migrate your codebase:

```
use Rector\Config\RectorConfig;
use Reinfi\DependencyInjection\Extension\Rector\Set\ReinfiDependencyInjectionSetList;

return static function (RectorConfig $rectorConfig): void {
  $rectorConfig->sets([ReinfiDependencyInjectionSetList::REINFI_DEPENDENCY_INJECTION_70]);
};
```

### Console commands

[](#console-commands)

- Warmup script for Laminas: php bin/zf-dependency-injection-cache-warmup Fills the cache with every injection required by a class. This can either be via AutoWiringFactory or InjectionFactory.

### FAQ

[](#faq)

Feel free to ask any questions or open your own pull requests.

###  Health Score

65

—

FairBetter than 99% of packages

Maintenance83

Actively maintained with recent releases

Popularity40

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity96

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 86.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~53 days

Recently: every ~75 days

Total

58

Last Release

129d ago

Major Versions

3.6.1 → 4.0.12020-02-07

3.6.3 → 4.0.22020-04-16

4.4.0 → 5.0.02022-01-16

5.5.0 → 6.0.02023-10-21

6.3.0 → 7.0.02025-03-13

PHP version history (10 changes)1.0PHP &gt;=7.0

2.0.0PHP &gt;=7.1

4.1.0PHP &gt;=7.2

4.2.0-betaPHP &gt;=7.3

4.3.0PHP &gt;=7.3 | &gt;=8.0

5.0.0PHP ~7.4.0 || ~8.0.0 || ~8.1.0

5.4.0PHP ~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0

6.0.0PHP ~8.1.0 || ~8.2.0

6.1.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0

7.0.0PHP ~8.3.0 || ~8.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/cd12a82b28a65cedc8e06642dabfbfd70fd512287bbaed49aaac0072fe898e41?d=identicon)[reinfi](/maintainers/reinfi)

---

Top Contributors

[![reinfi](https://avatars.githubusercontent.com/u/9324423?v=4)](https://github.com/reinfi "reinfi (391 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (22 commits)")[![R4c00n](https://avatars.githubusercontent.com/u/3935098?v=4)](https://github.com/R4c00n "R4c00n (10 commits)")[![annadamm-check24](https://avatars.githubusercontent.com/u/167064895?v=4)](https://github.com/annadamm-check24 "annadamm-check24 (8 commits)")[![gennadigennadigennadi](https://avatars.githubusercontent.com/u/3352744?v=4)](https://github.com/gennadigennadigennadi "gennadigennadigennadi (5 commits)")[![LeanderCS](https://avatars.githubusercontent.com/u/79199855?v=4)](https://github.com/LeanderCS "LeanderCS (3 commits)")[![icanhazstring](https://avatars.githubusercontent.com/u/883543?v=4)](https://github.com/icanhazstring "icanhazstring (3 commits)")[![1blankz7](https://avatars.githubusercontent.com/u/5745388?v=4)](https://github.com/1blankz7 "1blankz7 (2 commits)")[![froschdesign](https://avatars.githubusercontent.com/u/103362?v=4)](https://github.com/froschdesign "froschdesign (2 commits)")[![s0h311](https://avatars.githubusercontent.com/u/113988347?v=4)](https://github.com/s0h311 "s0h311 (2 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")[![renovate-bot](https://avatars.githubusercontent.com/u/25180681?v=4)](https://github.com/renovate-bot "renovate-bot (1 commits)")

---

Tags

annotationsautowiredependency-injectionlaminasyamlzend-frameworkAutowiringdependencyinjectionyamlzendannotation

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StyleECS

Type Coverage Yes

### Embed Badge

![Health badge](/badges/reinfi-zf-dependency-injection/health.svg)

```
[![Health](https://phpackages.com/badges/reinfi-zf-dependency-injection/health.svg)](https://phpackages.com/packages/reinfi-zf-dependency-injection)
```

###  Alternatives

[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[nette/neon

🍸 Nette NEON: encodes and decodes NEON file format.

93562.1M333](/packages/nette-neon)[mustangostang/spyc

A simple YAML loader/dumper class for PHP

73440.3M172](/packages/mustangostang-spyc)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[yiisoft/di

Yii DI container

2351.2M100](/packages/yiisoft-di)[spatie/yaml-front-matter

A to the point yaml front matter parser

3411.8M68](/packages/spatie-yaml-front-matter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
