Auto registration and configuration of services for Nette DI
, (*1)
fmasa\autoDI
is package intended to make registration and configuration
of services easier., (*2)
Installation
The best way to install fmasa/auto-di is using Composer:, (*3)
$ composer require fmasa/auto-di
To enable auto registration register extension in your config.neon
:, (*4)
extensions:
autoDI: Fmasa\AutoDI\DI\AutoDIExtension
Pattern based definition
autoDI
registers services defined by regex:, (*5)
autoDI:
services:
- class: App\Model\**\*Repository
This registers every class under namespace App\Model
which name ends with Repository
:, (*6)
- App\Model\Eshop\UserRepository
- App\Model\CMS\Comments\CommentsRepository
There are several simple operators that can be used in patterns:, (*7)
-
*
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., (*8)
Classes and generated factories
Package supports both classes and generated factories., (*9)
Classes are matched agains class
field, factories againts implement
field,
which corresponds to way Nette use these fields., (*10)
When using class
field, all matching interfaces are skipped and vice versa., (*11)
autoDI:
services:
# Repositories
- class: App\Model\**\*Repository
# Component factories
- implement: App\Components\very**\I*Factory
Every option supported in DI (tags, inject, autowiring, ...) is supported with same syntax
as normal service registration, (*12)
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., (*13)
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:, (*14)
autoDI:
services:
- class: App\Model\**
exclude: App\Model\{Entities,DTO}**
which is same as, (*15)
autoDI:
services:
- class: App\Model\**
exclude:
- App\Model\Entities**
- App\Model\DTO**
Already registered services
When extension founds service, that is already registered
(by services
section, different extension or previous autoDI
definition), it's skipped., (*16)
This allows manual registration of specific services that need specific configuration., (*17)
Defaults section
To specify base configuration for all services registered via autoDI
, defaults
section
can be used:, (*18)
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
By default extension searches for services in %appDir%
, but other directories can be specified:, (*19)
autoDI:
directories:
- %appDir%
- %appDir%/../vendor
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., (*20)
You can enforce registration in configuration phase
by setting registerOnConfiguration
option to true., (*21)