Advanced dependency injection for zend framework
Configure dependency injection in Laminas or Mezzio using attributes (PHP 8.0+), yaml or autowiring.
Heavily inspired by https://github.com/mikemix/mxdiModule.
=======
composer require reinfi/zf-dependency-injection
.application.config.php
under modules
key:
return [
'modules' => [
'Reinfi\DependencyInjection',
// other modules
],
];
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,
],
]
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,
],
]
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.
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 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:
Also, in addition, there are several annotations to inject from plugin managers.
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.
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.
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:
Also, in addition, there are several annotations to inject from plugin managers.
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.
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.
If you want to use your own annotation, you need to implement the AnnotationInterface.
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',
],
]
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()
);
},
]
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!
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();
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]);
};
Feel free to ask any questions or open your own pull requests.