- Mark your service as a consumer with the tag "xphere.tag_collection"
- Define the apply-to attribute, which defines the tag to be consumed
- Declare the method attribute, is the one that will be called on every tag found
- Mark your services to be consumed with the tag defined in the apply-to attribute
- Automatical injection! :D
Created
July 11, 2012 18:55
-
-
Save xphere/3092354 to your computer and use it in GitHub Desktop.
Mark a service as a consumer of others via tags and extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<parameters> | |
<parameter key="xphere.chain.class">xPheRe\Bundle\TestBundle\Model\Chain</parameter> | |
<parameter key="xphere.link.class">xPheRe\Bundle\TestBundle\Model\Link</parameter> | |
</parameters> | |
<services> | |
<service id="xphere.chain" class="%xphere.chain.class%"> | |
<argument>Root</argument> | |
<tag name="xphere.tag_collection" apply-to="xphere.tag.chain_to_root" method="addLink" /> | |
</service> | |
<service id="xphere.chain.link_1" class="%xphere.link.class%"> | |
<argument>First</argument> | |
<tag name="xphere.tag.chain_to_root" /> | |
</service> | |
<service id="xphere.chain.link_2" class="%xphere.chain.class%"> | |
<argument>Second</argument> | |
<tag name="xphere.tag.chain_to_root" /> | |
<tag name="xphere.tag_collection" apply-to="xphere.tag.chain_to_second" method="addLink" /> | |
</service> | |
<service id="xphere.chain.link_3" class="%xphere.link.class%"> | |
<argument>Third</argument> | |
<tag name="xphere.tag.chain_to_root" /> | |
</service> | |
<service id="xphere.chain.link_4" class="%xphere.link.class%"> | |
<argument>Fourth</argument> | |
<tag name="xphere.tag.chain_to_root" /> | |
<tag name="xphere.tag.chain_to_second" /> | |
</service> | |
</services> | |
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace xPheRe\Bundle\TagBundle; | |
use Symfony\Component\HttpKernel\Bundle\Bundle; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use xPheRe\Bundle\TagBundle\DependencyInjection\Compiler\TagCollectionPass; | |
class xPheReTagBundle extends Bundle | |
{ | |
public function build(ContainerBuilder $container) | |
{ | |
parent::build($container); | |
$container->addCompilerPass(new TagCollectionPass()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace xPheRe\Bundle\TagBundle\DependencyInjection\Compiler; | |
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\DependencyInjection\Reference; | |
class TagCollectionPass implements CompilerPassInterface | |
{ | |
public function process(ContainerBuilder $container) | |
{ | |
foreach ($container->findTaggedServiceIds('xphere.tag_collection') as $service => $tags) { | |
$definition = $container->getDefinition($service); | |
foreach ($tags as $attributes) { | |
$tag = $attributes['apply-to']; | |
$method = $attributes['method']; | |
foreach ($container->findTaggedServiceIds($tag) as $subservice => $subtags) { | |
$definition->addMethodCall($method, array(new Reference($subservice))); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has evolved into its own bundle.
Go see xphere/tag-bundle for more features.
composer require xphere/tag-bundle