|
| 1 | +# Dependency Injection |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +``` bash |
| 6 | +$ php composer.phar require yokai/dependency-injection |
| 7 | +``` |
| 8 | + |
| 9 | +## Compiler Pass |
| 10 | + |
| 11 | +``` php |
| 12 | +<?php |
| 13 | + |
| 14 | +namespace AppBundle; |
| 15 | + |
| 16 | +use Yokai\DependencyInjection\CompilerPass\ArgumentRegisterTaggedServicesCompilerPass; |
| 17 | +use Yokai\DependencyInjection\CompilerPass\AdderRegisterTaggedServicesCompilerPass; |
| 18 | +use Yokai\DependencyInjection\CompilerPass\SetterRegisterTaggedServicesCompilerPass; |
| 19 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 20 | +use Symfony\Component\HttpKernel\Bundle\Bundle; |
| 21 | + |
| 22 | +class AppBundle extends Bundle |
| 23 | +{ |
| 24 | + public function build(ContainerBuilder $container) |
| 25 | + { |
| 26 | + $container |
| 27 | + ->addCompilerPass( |
| 28 | + new ArgumentRegisterTaggedServicesCompilerPass( |
| 29 | + 'some_service_id', |
| 30 | + 'some_tag_name', |
| 31 | + 'An\Optional\Interface\To\Check', |
| 32 | + 0 |
| 33 | + ) |
| 34 | + ) |
| 35 | + ->addCompilerPass( |
| 36 | + new AdderRegisterTaggedServicesCompilerPass( |
| 37 | + 'some_service_id', |
| 38 | + 'some_tag_name', |
| 39 | + 'An\Optional\Interface\To\Check', |
| 40 | + 'addDependency' |
| 41 | + ) |
| 42 | + ) |
| 43 | + ->addCompilerPass( |
| 44 | + new SetterRegisterTaggedServicesCompilerPass( |
| 45 | + 'some_service_id', |
| 46 | + 'some_tag_name', |
| 47 | + 'An\Optional\Interface\To\Check', |
| 48 | + 'setDependencies' |
| 49 | + ) |
| 50 | + ) |
| 51 | + ; |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### ArgumentRegisterTaggedServicesCompilerPass |
| 57 | + |
| 58 | +This compiler pass will : |
| 59 | + |
| 60 | +- check for the service (first argument) existence (throw `LogicException` if not) |
| 61 | +- find services tagged with tag (second argument) |
| 62 | +- if provided, check every service against an interface (third argument) (throw `LogicException` if not) |
| 63 | +- sort these references base on a `priority` attribute |
| 64 | +- replace an argument (fourth argument) of your service definition with the sorted references |
| 65 | + |
| 66 | + |
| 67 | +## AdderRegisterTaggedServicesCompilerPass |
| 68 | + |
| 69 | +This compiler pass will : |
| 70 | + |
| 71 | +- check for the service (first argument) existence (throw `LogicException` if not) |
| 72 | +- find services tagged with tag (second argument) |
| 73 | +- if provided, check every service against an interface (third argument) (throw `LogicException` if not) |
| 74 | +- sort these references base on a `priority` attribute |
| 75 | +- call a method (fourth argument) for each sorted references |
| 76 | + |
| 77 | + |
| 78 | +## SetterRegisterTaggedServicesCompilerPass |
| 79 | + |
| 80 | +This compiler pass will : |
| 81 | + |
| 82 | +- check for the service (first argument) existence (throw `LogicException` if not) |
| 83 | +- find services tagged with tag (second argument) |
| 84 | +- if provided, check every service against an interface (third argument) (throw `LogicException` if not) |
| 85 | +- sort these references base on a `priority` attribute |
| 86 | +- call a method (fourth argument) with all sorted references |
| 87 | + |
| 88 | + |
| 89 | +MIT License |
| 90 | +----------- |
| 91 | + |
| 92 | +License can be found [here](https://github.com/yann-eugone/dependency-injection/blob/master/LICENSE). |
| 93 | + |
| 94 | + |
| 95 | +Authors |
| 96 | +------- |
| 97 | + |
| 98 | +The bundle was originally created by [Yann Eugoné](https://github.com/yann-eugone). |
| 99 | +See the list of [contributors](https://github.com/yann-eugone/dependency-injection/contributors). |
0 commit comments