[DI] Allow tagged_locator tag to be used as an argument

Signed-off-by: Anthony MARTIN <anthony.martin@sensiolabs.com>
This commit is contained in:
Anthony MARTIN 2019-02-15 15:50:49 +01:00
parent f54c89c530
commit 250a2c8332
18 changed files with 187 additions and 19 deletions

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\DependencyInjection\Argument;
use Symfony\Component\DependencyInjection\Reference;
/**
* Represents a closure acting as a service locator.
*
@ -19,4 +21,24 @@ namespace Symfony\Component\DependencyInjection\Argument;
class ServiceLocatorArgument implements ArgumentInterface
{
use ReferenceSetArgumentTrait;
private $taggedIteratorArgument;
/**
* @param Reference[]|TaggedIteratorArgument $values
*/
public function __construct($values = [])
{
if ($values instanceof TaggedIteratorArgument) {
$this->taggedIteratorArgument = $values;
$this->values = [];
} else {
$this->setValues($values);
}
}
public function getTaggedIteratorArgument(): ?TaggedIteratorArgument
{
return $this->taggedIteratorArgument;
}
}

View File

@ -23,8 +23,6 @@ class TaggedIteratorArgument extends IteratorArgument
private $defaultIndexMethod;
/**
* 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

View File

@ -9,6 +9,7 @@ CHANGELOG
* added support for deprecating aliases
* made `ContainerParametersResource` final and not implement `Serializable` anymore
* added ability to define an index for a tagged collection
* added ability to define an index for services in an injected service locator argument
4.2.0
-----

View File

@ -27,11 +27,18 @@ use Symfony\Component\DependencyInjection\ServiceLocator;
*/
final class ServiceLocatorTagPass extends AbstractRecursivePass
{
use PriorityTaggedServiceTrait;
protected function processValue($value, $isRoot = false)
{
if ($value instanceof ServiceLocatorArgument) {
if ($value->getTaggedIteratorArgument()) {
$value->setValues($this->findAndSortTaggedServices($value->getTaggedIteratorArgument(), $this->container));
}
return self::register($this->container, $value->getValues());
}
if (!$value instanceof Definition || !$value->hasTag('container.service_locator')) {
return parent::processValue($value, $isRoot);
}

View File

@ -298,8 +298,18 @@ class XmlDumper extends Dumper
$element->setAttribute('type', 'iterator');
$this->convertParameters($value->getValues(), $type, $element, 'key');
} elseif ($value instanceof ServiceLocatorArgument) {
$element->setAttribute('type', 'service_locator');
$this->convertParameters($value->getValues(), $type, $element, 'key');
if ($value->getTaggedIteratorArgument()) {
$element->setAttribute('type', 'tagged_locator');
$element->setAttribute('tag', $value->getTaggedIteratorArgument()->getTag());
$element->setAttribute('index-by', $value->getTaggedIteratorArgument()->getIndexAttribute());
if (null !== $value->getTaggedIteratorArgument()->getDefaultIndexMethod()) {
$element->setAttribute('default-index-method', $value->getTaggedIteratorArgument()->getDefaultIndexMethod());
}
} else {
$element->setAttribute('type', 'service_locator');
$this->convertParameters($value->getValues(), $type, $element, 'key');
}
} elseif ($value instanceof Reference) {
$element->setAttribute('type', 'service');
$element->setAttribute('id', (string) $value);

View File

@ -252,6 +252,18 @@ class YamlDumper extends Dumper
$tag = 'iterator';
} elseif ($value instanceof ServiceLocatorArgument) {
$tag = 'service_locator';
if ($value->getTaggedIteratorArgument()) {
$taggedValueContent = [
'tag' => $value->getTaggedIteratorArgument()->getTag(),
'index_by' => $value->getTaggedIteratorArgument()->getIndexAttribute(),
];
if (null !== $value->getTaggedIteratorArgument()->getDefaultIndexMethod()) {
$taggedValueContent['default_index_method'] = $value->getTaggedIteratorArgument()->getDefaultIndexMethod();
}
return new TaggedValue('tagged_locator', $taggedValueContent);
}
} else {
throw new RuntimeException(sprintf('Unspecified Yaml tag for type "%s".', \get_class($value)));
}

View File

@ -121,6 +121,14 @@ function tagged(string $tag, string $indexAttribute = null, string $defaultIndex
return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod);
}
/**
* Creates a service locator by tag name.
*/
function tagged_locator(string $tag, string $indexAttribute, string $defaultIndexMethod = null): ServiceLocatorArgument
{
return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod));
}
/**
* Creates an expression.
*/

View File

@ -533,6 +533,13 @@ class XmlFileLoader extends FileLoader
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="service_locator" only accepts maps of type="service" references in "%s".', $name, $file));
}
break;
case 'tagged_locator':
if (!$arg->getAttribute('tag') || !$arg->getAttribute('index-by')) {
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="tagged_locator" has no or empty "tag" or "index-by" attributes in "%s".', $name, $file));
}
$arguments[$key] = new ServiceLocatorArgument(new TaggedIteratorArgument($arg->getAttribute('tag'), $arg->getAttribute('index-by'), $arg->getAttribute('default-index-method') ?: null));
break;
case 'tagged':
if (!$arg->getAttribute('tag')) {
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="tagged" has no or empty "tag" attribute in "%s".', $name, $file));

View File

@ -702,7 +702,9 @@ class YamlFileLoader extends FileLoader
if (!\is_array($argument)) {
throw new InvalidArgumentException(sprintf('"!service_locator" tag only accepts maps in "%s".', $file));
}
$argument = $this->resolveServices($argument, $file, $isParameter);
try {
return new ServiceLocatorArgument($argument);
} catch (InvalidArgumentException $e) {
@ -724,6 +726,17 @@ class YamlFileLoader extends FileLoader
throw new InvalidArgumentException(sprintf('"!tagged" tags only accept a non empty string or an array with a key "tag" in "%s".', $file));
}
if ('tagged_locator' === $value->getTag()) {
if (\is_array($argument) && isset($argument['tag'], $argument['index_by']) && $argument['tag'] && $argument['index_by']) {
if ($diff = array_diff(array_keys($argument), ['tag', 'index_by', 'default_index_method'])) {
throw new InvalidArgumentException(sprintf('"!tagged_locator" tag contains unsupported key "%s"; supported ones are "tag", "index_by" and "default_index_method".', implode('"", "', $diff)));
}
return new ServiceLocatorArgument(new TaggedIteratorArgument($argument['tag'], $argument['index_by'], $argument['default_index_method'] ?? null));
}
throw new InvalidArgumentException(sprintf('"!tagged_locator" tags only accept an array with at least keys "tag" and "index_by" in "%s".', $file));
}
if ('service' === $value->getTag()) {
if ($isParameter) {
throw new InvalidArgumentException(sprintf('Using an anonymous service in a parameter is not allowed in "%s".', $file));

View File

@ -264,6 +264,7 @@
<xsd:enumeration value="iterator" />
<xsd:enumeration value="service_locator" />
<xsd:enumeration value="tagged" />
<xsd:enumeration value="tagged_locator" />
</xsd:restriction>
</xsd:simpleType>

View File

@ -14,10 +14,12 @@ namespace Symfony\Component\DependencyInjection\Tests\Compiler;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
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\ServiceLocator;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass;
@ -242,15 +244,15 @@ class IntegrationTest extends TestCase
public function testTaggedServiceWithIndexAttribute()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class, BarTagClass::class)
$container->register(BarTagClass::class)
->setPublic(true)
->addTag('foo_bar', ['foo' => 'bar'])
;
$container->register(FooTagClass::class, FooTagClass::class)
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooBarTaggedClass::class, FooBarTaggedClass::class)
$container->register(FooBarTaggedClass::class)
->addArgument(new TaggedIteratorArgument('foo_bar', 'foo'))
->setPublic(true)
;
@ -266,15 +268,15 @@ class IntegrationTest extends TestCase
public function testTaggedServiceWithIndexAttributeAndDefaultMethod()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class, BarTagClass::class)
$container->register(BarTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooTagClass::class, FooTagClass::class)
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar', ['foo' => 'foo'])
;
$container->register(FooBarTaggedClass::class, FooBarTaggedClass::class)
$container->register(FooBarTaggedClass::class)
->addArgument(new TaggedIteratorArgument('foo_bar', 'foo', 'getFooBar'))
->setPublic(true)
;
@ -286,6 +288,68 @@ class IntegrationTest extends TestCase
$param = iterator_to_array($s->getParam()->getIterator());
$this->assertSame(['bar_tab_class_with_defaultmethod' => $container->get(BarTagClass::class), 'foo' => $container->get(FooTagClass::class)], $param);
}
public function testTaggedServiceLocatorWithIndexAttribute()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
->setPublic(true)
->addTag('foo_bar', ['foo' => 'bar'])
;
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooBarTaggedClass::class)
->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('foo_bar', 'foo')))
->setPublic(true)
;
$container->compile();
$s = $container->get(FooBarTaggedClass::class);
/** @var ServiceLocator $serviceLocator */
$serviceLocator = $s->getParam();
$this->assertTrue($s->getParam() instanceof ServiceLocator, sprintf('Wrong instance, should be an instance of ServiceLocator, %s given', \is_object($serviceLocator) ? \get_class($serviceLocator) : \gettype($serviceLocator)));
$same = [
'bar' => $serviceLocator->get('bar'),
'foo_tag_class' => $serviceLocator->get('foo_tag_class'),
];
$this->assertSame(['bar' => $container->get(BarTagClass::class), 'foo_tag_class' => $container->get(FooTagClass::class)], $same);
}
public function testTaggedServiceLocatorWithIndexAttributeAndDefaultMethod()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar', ['foo' => 'foo'])
;
$container->register(FooBarTaggedClass::class)
->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('foo_bar', 'foo', 'getFooBar')))
->setPublic(true)
;
$container->compile();
$s = $container->get(FooBarTaggedClass::class);
/** @var ServiceLocator $serviceLocator */
$serviceLocator = $s->getParam();
$this->assertTrue($s->getParam() instanceof ServiceLocator, sprintf('Wrong instance, should be an instance of ServiceLocator, %s given', \is_object($serviceLocator) ? \get_class($serviceLocator) : \gettype($serviceLocator)));
$same = [
'bar_tab_class_with_defaultmethod' => $serviceLocator->get('bar_tab_class_with_defaultmethod'),
'foo' => $serviceLocator->get('foo'),
];
$this->assertSame(['bar_tab_class_with_defaultmethod' => $container->get(BarTagClass::class), 'foo' => $container->get(FooTagClass::class)], $same);
}
}
class ServiceSubscriberStub implements ServiceSubscriberInterface

View File

@ -93,6 +93,6 @@ class PriorityTaggedServiceTraitImplementation
public function test($tagName, ContainerBuilder $container)
{
return $this->findAndSortTaggedServices($tagName, $container);
return self::findAndSortTaggedServices($tagName, $container);
}
}

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\DependencyInjection\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -201,13 +202,18 @@ class XmlDumperTest extends TestCase
$this->assertStringEqualsFile(self::$fixturesPath.'/xml/services_dump_load.xml', $dumper->dump());
}
public function testTaggedArgument()
public function testTaggedArguments()
{
$taggedIterator = new TaggedIteratorArgument('foo_tag', 'barfoo', 'foobar');
$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'))
->addArgument($taggedIterator)
;
$container->register('foo_tagged_locator', 'Bar')
->setPublic(true)
->addArgument(new ServiceLocatorArgument($taggedIterator))
;
$dumper = new XmlDumper($container);

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\DependencyInjection\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -96,11 +97,13 @@ class YamlDumperTest extends TestCase
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_inline.yml', $dumper->dump());
}
public function testTaggedArgument()
public function testTaggedArguments()
{
$taggedIterator = new TaggedIteratorArgument('foo', 'barfoo', 'foobar');
$container = new ContainerBuilder();
$container->register('foo_service', 'Foo')->addTag('foo');
$container->register('foo_service_tagged', 'Bar')->addArgument(new TaggedIteratorArgument('foo', 'barfoo', 'foobar'));
$container->register('foo_service_tagged_iterator', 'Bar')->addArgument($taggedIterator);
$container->register('foo_service_tagged_locator', 'Bar')->addArgument(new ServiceLocatorArgument($taggedIterator));
$dumper = new YamlDumper($container);
$this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_with_tagged_argument.yml', $dumper->dump());

View File

@ -8,6 +8,9 @@
<service id="foo_tagged_iterator" class="Bar" public="true">
<argument type="tagged" tag="foo_tag" index-by="barfoo" default-index-method="foobar"/>
</service>
<service id="foo_tagged_locator" class="Bar" public="true">
<argument type="tagged_locator" tag="foo_tag" index-by="barfoo" default-index-method="foobar"/>
</service>
<service id="Psr\Container\ContainerInterface" alias="service_container" public="false"/>
<service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" public="false"/>
</services>

View File

@ -8,9 +8,12 @@ services:
class: Foo
tags:
- { name: foo }
foo_service_tagged:
foo_service_tagged_iterator:
class: Bar
arguments: [!tagged { tag: foo, index_by: barfoo, default_index_method: foobar }]
foo_service_tagged_locator:
class: Bar
arguments: [!tagged_locator { tag: foo, index_by: barfoo, default_index_method: foobar }]
Psr\Container\ContainerInterface:
alias: service_container
public: false

View File

@ -18,6 +18,7 @@ use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Resource\GlobResource;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
@ -324,7 +325,11 @@ class XmlFileLoaderTest extends TestCase
$this->assertCount(1, $container->getDefinition('foo')->getTag('foo_tag'));
$this->assertCount(1, $container->getDefinition('foo_tagged_iterator')->getArguments());
$this->assertEquals(new TaggedIteratorArgument('foo_tag', 'barfoo', 'foobar'), $container->getDefinition('foo_tagged_iterator')->getArgument(0));
$this->assertCount(1, $container->getDefinition('foo_tagged_locator')->getArguments());
$taggedIterator = new TaggedIteratorArgument('foo_tag', 'barfoo', 'foobar');
$this->assertEquals($taggedIterator, $container->getDefinition('foo_tagged_iterator')->getArgument(0));
$this->assertEquals(new ServiceLocatorArgument($taggedIterator), $container->getDefinition('foo_tagged_locator')->getArgument(0));
}
/**

View File

@ -18,6 +18,7 @@ use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Resource\GlobResource;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
@ -287,8 +288,12 @@ class YamlFileLoaderTest extends TestCase
$loader->load('services_with_tagged_argument.yml');
$this->assertCount(1, $container->getDefinition('foo_service')->getTag('foo'));
$this->assertCount(1, $container->getDefinition('foo_service_tagged')->getArguments());
$this->assertEquals(new TaggedIteratorArgument('foo', 'barfoo', 'foobar'), $container->getDefinition('foo_service_tagged')->getArgument(0));
$this->assertCount(1, $container->getDefinition('foo_service_tagged_iterator')->getArguments());
$this->assertCount(1, $container->getDefinition('foo_service_tagged_locator')->getArguments());
$taggedIterator = new TaggedIteratorArgument('foo', 'barfoo', 'foobar');
$this->assertEquals($taggedIterator, $container->getDefinition('foo_service_tagged_iterator')->getArgument(0));
$this->assertEquals(new ServiceLocatorArgument($taggedIterator), $container->getDefinition('foo_service_tagged_locator')->getArgument(0));
}
public function testNameOnlyTagsAreAllowedAsString()