<!---
# This file is part of the ChillDev DependencyInjection Extra library.
#
# @author Rafał Wrzeszcz <rafal.wrzeszcz@wrzasq.pl>
# @copyright 2013 © by Rafał Wrzeszcz - Wrzasq.pl.
# @version 0.0.1
# @since 0.0.1
# @package ChillDev\DependencyInjection
-->

# ChillDev DependencyInjection extras

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

[![Build Status](https://travis-ci.org/chilloutdevelopment/ChillDevDependencyInjectionExtra.png)](https://travis-ci.org/chilloutdevelopment/ChillDevDependencyInjectionExtra)
[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/chilloutdevelopment/ChillDevDependencyInjectionExtra/badges/quality-score.png?s=2095f8b5092f22c4963caf61c607a0cec10cfafc)](https://scrutinizer-ci.com/g/chilloutdevelopment/ChillDevDependencyInjectionExtra/)
[![Coverage Status](https://coveralls.io/repos/chilloutdevelopment/ChillDevDependencyInjectionExtra/badge.png?branch=develop)](https://coveralls.io/r/chilloutdevelopment/ChillDevDependencyInjectionExtra)
[![Dependency Status](http://www.versioneye.com/user/projects/523176d5632bac6991000eb1/badge.png)](http://www.versioneye.com/user/projects/523176d5632bac6991000eb1)

# Installation

This library is provided as [Composer package](https://packagist.org/packages/chilldev/dependency-injection-extra). 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:

```yaml
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:

```php
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:

```php
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](https://github.com/chilloutdevelopment/ChillDevDependencyInjectionExtra/tree/master/Resources/doc/usage.md) or even [internals description](https://github.com/chilloutdevelopment/ChillDevDependencyInjectionExtra/tree/master/Resources/doc/internals.md).

# Resources

-   [Source documentation](https://github.com/chilloutdevelopment/ChillDevDependencyInjectionExtra/tree/master/Resources/doc/index.md)
-   [GitHub page with API documentation](http://chilloutdevelopment.github.io/ChillDevDependencyInjectionExtra)
-   [Issues tracker](https://github.com/chilloutdevelopment/ChillDevDependencyInjectionExtra/issues)
-   [Packagist package](https://packagist.org/packages/chilldev/dependency-injection-extra)
-   [Chillout Development @ GitHub](https://github.com/chilloutdevelopment)
-   [Chillout Development @ Facebook](http://www.facebook.com/chilldev)

# 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](https://github.com/chilloutdevelopment/ChillDevDependencyInjectionExtra/LICENSE).

# Authors

**ChillDevDependencyInjectionExtra** is brought to you by [Chillout Development](http://chilldev.pl).

List of contributors:

-   [Rafał "Wrzasq" Wrzeszcz](https://github.com/rafalwrzeszcz) ([wrzasq.pl](http://wrzasq.pl)).
