PHPackages                             fmasa/auto-di - 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. fmasa/auto-di

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

fmasa/auto-di
=============

2.0.0(6y ago)1522.9k↑252.9%3[1 issues](https://github.com/fmasa/auto-di/issues)[1 PRs](https://github.com/fmasa/auto-di/pulls)MITPHPPHP ^7.1

Since Sep 23Pushed 6y ago2 watchersCompare

[ Source](https://github.com/fmasa/auto-di)[ Packagist](https://packagist.org/packages/fmasa/auto-di)[ RSS](/packages/fmasa-auto-di/feed)WikiDiscussions 2.x Synced today

READMEChangelog (2)Dependencies (6)Versions (11)Used By (0)

Service auto registration and configuration for Nette DI
========================================================

[](#service-auto-registration-and-configuration-for-nette-di)

[![Build Status](https://camo.githubusercontent.com/6b10b3d240713bffcc97529495eb04f3aaad65892fa4ca93e78a7f0b77dc2df2/68747470733a2f2f7472617669732d63692e6f72672f666d6173612f6175746f2d64692e7376673f6272616e63683d322e78)](https://travis-ci.org/fmasa/auto-di)[![Coverage Status](https://camo.githubusercontent.com/06b7592c4046bb26db849f4c01504e54a368915ebaef172afb050008d7a87820/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f666d6173612f6175746f2d64692f62616467652e7376673f6272616e63683d322e78)](https://coveralls.io/github/fmasa/auto-di?branch=2.x)

`fmasa\autoDI` is package intended to make registration and configuration of services easier.

Future of extension
-------------------

[](#future-of-extension)

In Nette 3.0, [SearchExtension](https://doc.nette.org/en/3.0/di-builtin-extensions#toc-searchextension) was introduced. It builds on the same concept as this extension (in which I stole the concept from Symfony DependencyInjection). Unfortunately there are still a few problems that need to be fixed in order to fully replace `fmasa/auto-di` (i.e. [nette/di#215](https://github.com/nette/di/issues/215)), but it's on the other hand part of Nette DI core and supports several features, that `fmasa/auto-di` doesn't (yet). These are ways to register services implementing specific interface or extending specific class.

So long-term goal is to deprecate this extension in favor of SearchExtension, when (and if) SearchExtension reaches feature parity. Until then I plan to maintain and improve this package since I use it myself in many projects.

Installation
------------

[](#installation)

The best way to install fmasa/auto-di is using [Composer](https://getcomposer.org/):

```
$ composer require fmasa/auto-di

```

To enable auto registration register extension in your `config.neon`:

```
extensions:
    autoDI: Fmasa\AutoDI\DI\AutoDIExtension
```

Pattern based definition
------------------------

[](#pattern-based-definition)

`autoDI` registers services defined by regex:

```
autoDI:
    services:
        - class: App\Model\**\*Repository
```

This registers every class under namespace `App\Model` which name ends with `Repository`:

- App\\Model\\Eshop\\UserRepository
- App\\Model\\CMS\\Comments\\CommentsRepository

There are several simple operators that can be used in patterns:

- `*` matches class name, one namespace level or part of it (without \\)
- `**` matches any part of namespace or classname (including \\)
- `{Eshop,CMS}` options list, any item of list matches this pattern

Apart from these, any PCRE regular expression can be used.

Classes and generated factories
-------------------------------

[](#classes-and-generated-factories)

Package supports both classes and [generated factories](https://doc.nette.org/en/2.4/di-usage#toc-component-factory).

Classes are matched agains `class` field, factories againts `implement` field, which corresponds to way Nette use these fields.

When using `class` field, all matching interfaces are skipped and vice versa.

```
autoDI:
    services:
        # Repositories
        - class: App\Model\**\*Repository

        # Component factories
        - implement: App\Components\very**\I*Factory
```

Tags, autowiring, ...
---------------------

[](#tags-autowiring-)

Every option supported in DI (tags, inject, autowiring, ...) is supported with same syntax as normal service registration

```
autoDI:
    services:
        # Repositories
        - class: App\Model\Subscribers\**
          tags: [eventBus.subscriber]
```

The snippet above registers all classes in `App\Model\Subscribers` namespace with `eventBus.subscriber` tag.

Exclude services
----------------

[](#exclude-services)

Sometimes we wan't to exlude certain services from registration. For that we can use `exclude` field, that accepts pattern or list of patterns:

```
autoDI:
    services:
        - class: App\Model\**
          exclude: App\Model\{Entities,DTO}**
```

which is same as

```
autoDI:
    services:
        - class: App\Model\**
          exclude:
              - App\Model\Entities**
              - App\Model\DTO**
```

Already registered services
---------------------------

[](#already-registered-services)

When extension founds service, that is already registered (by `services` section, different extension or previous `autoDI` definition), **it's skipped**.

This allows manual registration of specific services that need specific configuration.

Defaults section
----------------

[](#defaults-section)

To specify base configuration for all services registered via `autoDI`, `defaults` section can be used:

```
autoDI:
    defaults:
        tags: [ my.auto.service ]

    services:
        # these services will have tag my.auto.service
        - class: App\Model\Repositories\**

        # these services will have only tag eventBus.subscriber
        - class: app\Model\Subscribers\**
          tags: [ eventBus.subscriber ]
```

Configuring directories
-----------------------

[](#configuring-directories)

By default extension searches for services in `%appDir%`, but other directories can be specified:

```
autoDI:
    directories:
        - %appDir%
        - %appDir%/../vendor
```

Register services on configuration
----------------------------------

[](#register-services-on-configuration)

Compiler extensions such as AutoDIExtension manipulates the DI container in two phases (configuration loading and before compilation). By default this extension registers all services before compilation. This may not be optimal if you wan't to use this extension with other extensions such as decorator.

You can enforce registration in configuration phase by setting `registerOnConfiguration` option to true.

When no service is registered for configuration entry, either because no class/interface matches the pattern or all matched services were already registered in container, exception is thrown. This check can be disabled by setting `errorOnNotMatchedDefinitions` option to false.

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~176 days

Recently: every ~221 days

Total

6

Last Release

2323d ago

Major Versions

1.x-dev → 2.0.02020-02-23

PHP version history (2 changes)1.0PHP ^5.6 || ^7.0

2.0.0PHP ^7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/23986c5d998fff583e89fc5aaa96797c37b09eb1b037512b570a03590e19e06e?d=identicon)[fmasa](/maintainers/fmasa)

---

Top Contributors

[![fmasa](https://avatars.githubusercontent.com/u/5658260?v=4)](https://github.com/fmasa "fmasa (48 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/fmasa-auto-di/health.svg)

```
[![Health](https://phpackages.com/badges/fmasa-auto-di/health.svg)](https://phpackages.com/packages/fmasa-auto-di)
```

PHPackages © 2026

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