Skip to content
Permalink
Browse files

feature #30257 [DependencyInjection] Allow to choose an index for tag…

…ged collection (deguif, XuruDragon)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[DependencyInjection] Allow to choose an index for tagged collection

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #29203
| License       | MIT
| Doc PR        | symfony/symfony-docs#11009

This is the continuity of the PR #29598

Add a way to specify an index based on a tag attribute when injecting a tag collection into services, but also a a way to fallback to a static method on the service class.

```yaml
services:
  foo_service:
    class: Foo
    tags:
      - foo
  foo_service_tagged:
    class: Bar
    arguments:
      - !tagged
          tag: 'foo'
          index_by: 'tag_attribute_name'
          default_index_method: 'static_method'
```
```xml
<?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">
  <services>
    <service id="foo" class="Foo">
        <tag name="foo_tag" />
    </service>
    <service id="foo_tagged_iterator" class="Bar" public="true">
      <argument type="tagged" tag="foo_tag" index-by="tag_attribute_name" default-index-method="static_method" />
    </service>
  </services>
</container>
```

Tasks

* [x]  Support PHP loader/dumper
* [x]  Support YAML loader/dumper
* [x]  Support XML loader/dumper (and update XSD too)
* [x]  Add tests
* [x]  Documentation

Commits
-------

101bfd7 [DI] change name to tag + add XMl support + adding yaml/xml tests
845d3a6 Allow to choose an index for tagged collection
  • Loading branch information...
nicolas-grekas committed Feb 22, 2019
2 parents fec0475 + 101bfd7 commit b4f6c345addc0bf82b71a22139171a642b793d08
Showing with 297 additions and 18 deletions.
  1. +25 −1 src/Symfony/Component/DependencyInjection/Argument/TaggedIteratorArgument.php
  2. +1 −0 src/Symfony/Component/DependencyInjection/CHANGELOG.md
  3. +46 −3 src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php
  4. +1 −1 src/Symfony/Component/DependencyInjection/Compiler/ResolveTaggedIteratorArgumentPass.php
  5. +8 −0 src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php
  6. +13 −0 src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php
  7. +2 −2 src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php
  8. +3 −2 src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
  9. +17 −9 src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
  10. +2 −0 src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd
  11. +52 −0 src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php
  12. +14 −0 src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php
  13. +11 −0 src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php
  14. +16 −0 src/Symfony/Component/DependencyInjection/Tests/Fixtures/BarTagClass.php
  15. +18 −0 src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooBarTaggedClass.php
  16. +11 −0 src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooTagClass.php
  17. +14 −0 src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_tagged_arguments.xml
  18. +19 −0 src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_tagged_argument.yml
  19. +12 −0 src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php
  20. +12 −0 src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php
@@ -19,16 +19,40 @@
class TaggedIteratorArgument extends IteratorArgument
{
private $tag;
private $indexAttribute;
private $defaultIndexMethod;
public function __construct(string $tag)
/**
* TaggedIteratorArgument constructor.
*
* @param string $tag The name of the tag identifying the target services
* @param string|null $indexAttribute The name of the attribute that defines the key referencing each service in the tagged collection
* @param string|null $defaultIndexMethod The static method that should be called to get each service's key when their tag doesn't define the previous attribute
*/
public function __construct(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null)
{
parent::__construct([]);
$this->tag = $tag;
if ($indexAttribute) {
$this->indexAttribute = $indexAttribute;
$this->defaultIndexMethod = $defaultIndexMethod ?: ('getDefault'.str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $indexAttribute))).'Name');
}
}
public function getTag()
{
return $this->tag;
}
public function getIndexAttribute(): ?string
{
return $this->indexAttribute;
}
public function getDefaultIndexMethod(): ?string
{
return $this->defaultIndexMethod;
}
}
@@ -9,6 +9,7 @@ CHANGELOG
* added `%env(nullable:...)%` processor to allow empty variables to be processed as null values
* added support for deprecating aliases
* made `ContainerParametersResource` final and not implement `Serializable` anymore
* added ability to define an index for a tagged collection

4.2.0
-----
@@ -11,7 +11,9 @@
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
/**
@@ -31,18 +33,59 @@ trait PriorityTaggedServiceTrait
* @see https://bugs.php.net/bug.php?id=53710
* @see https://bugs.php.net/bug.php?id=60926
*
* @param string $tagName
* @param ContainerBuilder $container
* @param string|TaggedIteratorArgument $tagName
* @param ContainerBuilder $container
*
* @return Reference[]
*/
private function findAndSortTaggedServices($tagName, ContainerBuilder $container)
{
$indexAttribute = $defaultIndexMethod = null;
if ($tagName instanceof TaggedIteratorArgument) {
$indexAttribute = $tagName->getIndexAttribute();
$defaultIndexMethod = $tagName->getDefaultIndexMethod();
$tagName = $tagName->getTag();
}
$services = [];
foreach ($container->findTaggedServiceIds($tagName, true) as $serviceId => $attributes) {
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
$services[$priority][] = new Reference($serviceId);
if (null === $indexAttribute) {
$services[$priority][] = new Reference($serviceId);
continue;
}
if (isset($attributes[0][$indexAttribute])) {
$services[$priority][$attributes[0][$indexAttribute]] = new Reference($serviceId);
continue;
}
if (!$r = $container->getReflectionClass($class = $container->getDefinition($serviceId)->getClass())) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId));
}
if (!$r->hasMethod($defaultIndexMethod)) {
throw new InvalidArgumentException(sprintf('Method "%s::%s()" not found: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
}
if (!($rm = $r->getMethod($defaultIndexMethod))->isStatic()) {
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be static: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
}
if (!$rm->isPublic()) {
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be public: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
}
$key = $rm->invoke(null);
if (!\is_string($key)) {
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should return a string, got %s: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, \gettype($key), $tagName, $serviceId, $indexAttribute));
}
$services[$priority][$key] = new Reference($serviceId);
}
if ($services) {
@@ -31,7 +31,7 @@ protected function processValue($value, $isRoot = false)
return parent::processValue($value, $isRoot);
}
$value->setValues($this->findAndSortTaggedServices($value->getTag(), $this->container));
$value->setValues($this->findAndSortTaggedServices($value, $this->container));
return $value;
}
@@ -286,6 +286,14 @@ private function convertParameters(array $parameters, $type, \DOMElement $parent
} elseif ($value instanceof TaggedIteratorArgument) {
$element->setAttribute('type', 'tagged');
$element->setAttribute('tag', $value->getTag());
if (null !== $value->getIndexAttribute()) {
$element->setAttribute('index-by', $value->getIndexAttribute());
}
if (null !== $value->getDefaultIndexMethod()) {
$element->setAttribute('default-index-method', $value->getDefaultIndexMethod());
}
} elseif ($value instanceof IteratorArgument) {
$element->setAttribute('type', 'iterator');
$this->convertParameters($value->getValues(), $type, $element, 'key');
@@ -233,6 +233,19 @@ private function dumpValue($value)
}
if ($value instanceof ArgumentInterface) {
if ($value instanceof TaggedIteratorArgument) {
if (null !== $value->getIndexAttribute()) {
$taggedValueContent = [
'tag' => $value->getTag(),
'index_by' => $value->getIndexAttribute(),
];
if (null !== $value->getDefaultIndexMethod()) {
$taggedValueContent['default_index_method'] = $value->getDefaultIndexMethod();
}
return new TaggedValue('tagged', $taggedValueContent);
}
return new TaggedValue('tagged', $value->getTag());
}
if ($value instanceof IteratorArgument) {
@@ -116,9 +116,9 @@ function iterator(array $values): IteratorArgument
/**
* Creates a lazy iterator by tag name.
*/
function tagged(string $tag): TaggedIteratorArgument
function tagged(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null): TaggedIteratorArgument
{
return new TaggedIteratorArgument($tag);
return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod);
}
/**
@@ -353,7 +353,7 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults)
continue;
}
if (false !== strpos($name, '-') && false === strpos($name, '_') && !array_key_exists($normalizedName = str_replace('-', '_', $name), $parameters)) {
if (false !== strpos($name, '-') && false === strpos($name, '_') && !\array_key_exists($normalizedName = str_replace('-', '_', $name), $parameters)) {
$parameters[$normalizedName] = XmlUtils::phpize($node->nodeValue);
}
// keep not normalized key
@@ -537,7 +537,8 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $file, $lowercase =
if (!$arg->getAttribute('tag')) {
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="tagged" has no or empty "tag" attribute in "%s".', $name, $file));
}
$arguments[$key] = new TaggedIteratorArgument($arg->getAttribute('tag'));
$arguments[$key] = new TaggedIteratorArgument($arg->getAttribute('tag'), $arg->getAttribute('index-by') ?: null, $arg->getAttribute('default-index-method') ?: null);
break;
case 'binary':
if (false === $value = base64_decode($arg->nodeValue)) {
@@ -203,7 +203,7 @@ private function parseDefinitions(array $content, string $file)
throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
}
if (array_key_exists('_instanceof', $content['services'])) {
if (\array_key_exists('_instanceof', $content['services'])) {
$instanceof = $content['services']['_instanceof'];
unset($content['services']['_instanceof']);
@@ -235,7 +235,7 @@ private function parseDefinitions(array $content, string $file)
*/
private function parseDefaults(array &$content, string $file): array
{
if (!array_key_exists('_defaults', $content['services'])) {
if (!\array_key_exists('_defaults', $content['services'])) {
return [];
}
$defaults = $content['services']['_defaults'];
@@ -342,7 +342,7 @@ private function parseDefinition($id, $service, $file, array $defaults)
if (isset($service['alias'])) {
$this->container->setAlias($id, $alias = new Alias($service['alias']));
if (array_key_exists('public', $service)) {
if (\array_key_exists('public', $service)) {
$alias->setPublic($service['public']);
} elseif (isset($defaults['public'])) {
$alias->setPublic($defaults['public']);
@@ -430,7 +430,7 @@ private function parseDefinition($id, $service, $file, array $defaults)
$definition->setAbstract($service['abstract']);
}
if (array_key_exists('deprecated', $service)) {
if (\array_key_exists('deprecated', $service)) {
$definition->setDeprecated(true, $service['deprecated']);
}
@@ -545,11 +545,11 @@ private function parseDefinition($id, $service, $file, array $defaults)
}
}
if (array_key_exists('namespace', $service) && !array_key_exists('resource', $service)) {
if (\array_key_exists('namespace', $service) && !\array_key_exists('resource', $service)) {
throw new InvalidArgumentException(sprintf('A "resource" attribute must be set when the "namespace" attribute is set for service "%s" in %s. Check your YAML syntax.', $id, $file));
}
if (array_key_exists('resource', $service)) {
if (\array_key_exists('resource', $service)) {
if (!\is_string($service['resource'])) {
throw new InvalidArgumentException(sprintf('A "resource" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
}
@@ -710,11 +710,19 @@ private function resolveServices($value, $file, $isParameter = false)
}
}
if ('tagged' === $value->getTag()) {
if (!\is_string($argument) || !$argument) {
throw new InvalidArgumentException(sprintf('"!tagged" tag only accepts non empty string in "%s".', $file));
if (\is_string($argument) && $argument) {
return new TaggedIteratorArgument($argument);
}
return new TaggedIteratorArgument($argument);
if (\is_array($argument) && isset($argument['tag']) && $argument['tag']) {
if ($diff = array_diff(array_keys($argument), ['tag', 'index_by', 'default_index_method'])) {
throw new InvalidArgumentException(sprintf('"!tagged" tag contains unsupported key "%s"; supported ones are "tag", "index_by" and "default_index_method".', implode('"", "', $diff)));
}
return new TaggedIteratorArgument($argument['tag'], $argument['index_by'] ?? null, $argument['default_index_method'] ?? null);
}
throw new InvalidArgumentException(sprintf('"!tagged" tags only accept a non empty string or an array with a key "tag" in "%s".', $file));
}
if ('service' === $value->getTag()) {
if ($isParameter) {
@@ -234,6 +234,8 @@
<xsd:attribute name="index" type="xsd:integer" />
<xsd:attribute name="on-invalid" type="invalid_sequence" />
<xsd:attribute name="tag" type="xsd:string" />
<xsd:attribute name="index-by" type="xsd:string" />
<xsd:attribute name="default-index-method" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="call">
@@ -14,10 +14,14 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooTagClass;
/**
* This class tests the integration of the different compiler passes.
@@ -234,6 +238,54 @@ public function getYamlCompileTests()
$container,
];
}
public function testTaggedServiceWithIndexAttribute()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class, BarTagClass::class)
->setPublic(true)
->addTag('foo_bar', ['foo' => 'bar'])
;
$container->register(FooTagClass::class, FooTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooBarTaggedClass::class, FooBarTaggedClass::class)
->addArgument(new TaggedIteratorArgument('foo_bar', 'foo'))
->setPublic(true)
;
$container->compile();
$s = $container->get(FooBarTaggedClass::class);
$param = iterator_to_array($s->getParam()->getIterator());
$this->assertSame(['bar' => $container->get(BarTagClass::class), 'foo_tag_class' => $container->get(FooTagClass::class)], $param);
}
public function testTaggedServiceWithIndexAttributeAndDefaultMethod()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class, BarTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooTagClass::class, FooTagClass::class)
->setPublic(true)
->addTag('foo_bar', ['foo' => 'foo'])
;
$container->register(FooBarTaggedClass::class, FooBarTaggedClass::class)
->addArgument(new TaggedIteratorArgument('foo_bar', 'foo', 'getFooBar'))
->setPublic(true)
;
$container->compile();
$s = $container->get(FooBarTaggedClass::class);
$param = iterator_to_array($s->getParam()->getIterator());
$this->assertSame(['bar_tab_class_with_defaultmethod' => $container->get(BarTagClass::class), 'foo' => $container->get(FooTagClass::class)], $param);
}
}
class ServiceSubscriberStub implements ServiceSubscriberInterface
@@ -13,6 +13,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
@@ -200,6 +201,19 @@ public function testDumpLoad()
$this->assertStringEqualsFile(self::$fixturesPath.'/xml/services_dump_load.xml', $dumper->dump());
}
public function testTaggedArgument()
{
$container = new ContainerBuilder();
$container->register('foo', 'Foo')->addTag('foo_tag');
$container->register('foo_tagged_iterator', 'Bar')
->setPublic(true)
->addArgument(new TaggedIteratorArgument('foo_tag', 'barfoo', 'foobar'))
;
$dumper = new XmlDumper($container);
$this->assertStringEqualsFile(self::$fixturesPath.'/xml/services_with_tagged_arguments.xml', $dumper->dump());
}
public function testDumpAbstractServices()
{
$container = include self::$fixturesPath.'/containers/container_abstract.php';
Oops, something went wrong.

0 comments on commit b4f6c34

Please sign in to comment.
You can’t perform that action at this time.