reinfi / zf-dependency-injection
A Laminas Framework module for loading dependencies via annotation or config entries.
Installs: 104 494
Dependents: 1
Suggesters: 0
Security: 0
Stars: 19
Watchers: 6
Forks: 3
Open Issues: 7
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- laminas/laminas-modulemanager: ^2.7
- laminas/laminas-servicemanager: ^2.7 | ^3.0
- psr/simple-cache: ^1.0 | ^2.0 | ^3.0
Requires (Dev)
- bnf/phpstan-psr-container: ^1.0
- doctrine/annotations: ^2.0
- doctrine/orm: ^2.5
- laminas/laminas-config-aggregator: ^1.0
- laminas/laminas-filter: ^2.11
- laminas/laminas-form: ^3.0
- laminas/laminas-hydrator: ^4.2
- laminas/laminas-i18n: ^2.7
- laminas/laminas-inputfilter: ^2.12
- laminas/laminas-log: ^2.9
- laminas/laminas-mvc: ^2.7 | ^3.0
- laminas/laminas-serializer: ^2.6
- laminas/laminas-translator: ^1.0
- laminas/laminas-validator: ^2.14
- laminas/laminas-view: ^2.6
- php-coveralls/php-coveralls: ^2.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^1.3
- phpunit/phpunit: ^10.0
- symfony/yaml: ^6.0 | ^7.0
- symplify/easy-coding-standard: ^12.0
Suggests
- doctrine/annotations: To use annotation injections
- symfony/console: To use console warmup command script
- symfony/yaml: To use the yaml dependency definition
- dev-master
- 6.2.0
- 6.1.0
- 6.0.0
- 5.5.0
- 5.4.0
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.0
- 4.4.0
- 4.3.0
- 4.2.0
- 4.2.0-beta
- 4.1.0
- 4.0.2
- 4.0.1
- 4.0.0
- 3.6.3
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.0
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.0
- 3.0.1
- 3.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.0
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0
- dev-renovate/major-phpstan-packages
- dev-renovate/laminas-laminas-servicemanager-4.x
- dev-renovate/laminas-laminas-validator-3.x
- dev-renovate/laminas-laminas-serializer-3.x
- dev-renovate/ubuntu-24.x
- dev-renovate/doctrine-orm-3.x
- dev-renovate/phpunit-phpunit-11.x
- dev-master-6.x
- dev-master-5.x
- dev-inject-as-parameter-attribute
- dev-master-4.x
- dev-master-1.0
This package is auto-updated.
Last update: 2024-11-11 10:37:56 UTC
README
Configure dependency injection in Laminas or Mezzio using annotations, yaml or autowiring.
Heavily inspired by https://github.com/mikemix/mxdiModule.
=======
Installation
- Install with Composer:
composer require reinfi/zf-dependency-injection
. - Enable the module via config in
application.config.php
undermodules
key:
return [ 'modules' => [ 'Reinfi\DependencyInjection', // other modules ], ];
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
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?
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 simply add the following:
PluginManagerResolver::addMapping('MyInterfaceClass', 'MyPluginManager');
If your service needs the container as dependency this can also be autowired.
Add another resolver
If you like to add another resolver you can simply add one through the configuration.
'reinfi.dependencyInjection' => [ 'autowire_resolver' => [ AnotherResolver::class, ], ]
It needs to implement the ResolverInterface.
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, ], ]
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 a 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 following:
#[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
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, ], ]
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 a 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 following:
@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
If you want to use your own annotation you just need to implement the AnnotationInterface.
YAML
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
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
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 then you're all set!
Manual installation
If 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 to know 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();
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
Feel free to ask any questions or open own pull requests.