Skip to content

Instantly share code, notes, and snippets.

@xphere
Created July 11, 2012 18:55
Show Gist options
  • Save xphere/3092354 to your computer and use it in GitHub Desktop.
Save xphere/3092354 to your computer and use it in GitHub Desktop.
Mark a service as a consumer of others via tags and extension
  1. Mark your service as a consumer with the tag "xphere.tag_collection"
  2. Define the apply-to attribute, which defines the tag to be consumed
  3. Declare the method attribute, is the one that will be called on every tag found
  4. Mark your services to be consumed with the tag defined in the apply-to attribute
  5. Automatical injection! :D
<?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>
<?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());
}
}
<?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)));
}
}
}
}
}
@xphere
Copy link
Author

xphere commented Oct 16, 2015

This has evolved into its own bundle.
Go see xphere/tag-bundle for more features.
composer require xphere/tag-bundle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment