chilldev/dependency-injection-extra

Commonly used patterns for Symfony2 DI provided out of the box.

This package's canonical repository appears to be gone and the package has been frozen as a result.

0.0.1 2013-09-12 18:44 UTC

This package is not auto-updated.

Last update: 2019-03-08 14:40:41 UTC


README

ChillDevDependencyInjectionExtra is a library which offers implementation of some commonly used patterns used in Symfony2 DI.

Build Status Scrutinizer Quality Score Coverage Status Dependency Status SensioLabsInsight

Installation

This library is provided as Composer package. To install it simply add following dependency definition to your composer.json file:

"chilldev/dependency-injection-extra": "dev-master"

Replace dev-master with different constraint if you want to use specific version.

Note: This library requires PHP 5.4.

Note: This is not a bundle; you don't need to adapt your kernel or any other part of project to use it's goods - just use it directly in your code.

Usage

You can use ChillDev\DependencyInjection\Compiler\TagGrabbingPass to automate grabbing all tagged services into container. Consider following DI services:

services:
    repository:
        class: "YourAdaptersRepository"
    first:
        class: "YourFirstAdapter"
        tags:
            - { name: "your.adapter.tag", adapter: "first" }
    second:
        class: "YourSecondAdapter"
        tags:
            - { name: "your.adapter.tag", adapter: "second" }
    third:
        class: "YourThirdAdapter"
        tags:
            - { name: "your.adapter.tag", adapter: "third" }

Let's say your repository class looks as follows:

class YourAdaptersRepository extends ArrayObject
{
    public function registerAdapter($key, $adapter)
    {
        $this[$key] = $adapter;
    }
}

It is a very common setup - now you want to add all services tagged with your.adapter.tag to repository service. Instead of writing your own custom DI compiler pass, you can use ready implementation that will do exactly what you want. It's as easy as:

use ChillDev\DependencyInjection\Compiler\TagGrabbingPass;

use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class YourBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        $container->addCompilerPass(
            new TagGrabbingPass(
                // this is tag name for which you want to look
                'your.adapter.tag',
                // this is ID of container service
                'repository',
                // this is name of method for registering new adapters
                'registerAdapter',
                // this is tag attribute name to be used as key identifier
                'adapter'
            ),
            PassConfig::TYPE_OPTIMIZE
        );
    }
}

For more advanced aspects see advanced usage documentation or even internals description.

Resources

Contributing

Do you want to help improving this project? Simply fork it and post a pull request. You can do everything on your own, you don't need to ask if you can, just do all the awesome things you want!

This project is published under MIT license.

Authors

ChillDevDependencyInjectionExtra is brought to you by Chillout Development.

List of contributors: