reinfi / zf-dependency-injection

Advanced dependency injection for zend framework


Build Status Code Climate Coverage Status

Configure dependency injection in Zend Framework 2 using annotations, yaml or autowiring.

Heavily inspired by https://github.com/mikemix/mxdiModule.

  1. Installation
  2. AutoWiring
  3. Annotations
  4. YAML
  5. Caching
  6. Console commands

Installation

  1. Install with Composer: composer require reinfi/zf-dependency-injection.
  2. Enable the module via ZF2 config in appliation.config.php under modules 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,
    ],
]
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 dependecy 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.

Annotations

To use annotation 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)

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

  • InjectViewHelper
  • InjectFilter
  • InjectInputFilter
  • InjectValidator
  • InjectHydrator
  • InjectFormElement

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.

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

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

or

    /**
     * @Inject("Namespace\MyService")
     *
     * @param MyService $service
     */
    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 ZF2 cache adapter.

'reinfi.dependencyInjection' => [
    'cache'         => \Zend\Cache\Storage\Adapter\Memory::class,
    'cache_options' => [],
    'cache_plugins' => [],
]

You can find more information about available out-of-the-box adapters at the ZF2 docs site.

Console commands

  • Warmup cache: php public/index.php reinfi:di 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.

Copyright (c) 2017 Reinfi <reinfandt@z-em.de>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
php composer.phar require "reinfi/zf-dependency-injection"

Require

  • php: >=7.0
  • doctrine/annotations: ~1.2
  • zendframework/zend-servicemanager: ~2.7
  • zendframework/zend-modulemanager: ~2.7
  • zendframework/zend-mvc: ~2.7
  • zendframework/zend-console: ~2.6
  • zendframework/zend-cache: ~2.5
  • symfony/yaml: ^3.3

Require Dev

  • phpunit/phpunit: ^5.7
  • doctrine/orm: ^2.5
  • satooshi/php-coveralls: ^1.0
  • phpunit/phpcov: ^3.0