Skip to content

[DependencyInjection] Extend TaggedIterator and TaggedLocator Attributes with able to specify defaultIndexMethod for #[TaggerIterator] and #[TaggedLocator] #43386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class TaggedIterator
public function __construct(
public string $tag,
public ?string $indexAttribute = null,
public ?string $defaultIndexMethod = null,
public ?string $defaultPriorityMethod = null,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class TaggedLocator
public function __construct(
public string $tag,
public ?string $indexAttribute = null,
public ?string $defaultIndexMethod = null,
public ?string $defaultPriorityMethod = null,
) {
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CHANGELOG

5.4
---

* Add `$defaultIndexMethod` and `$defaultPriorityMethod` to `TaggedIterator` and `TaggedLocator` attributes
* Add `service_closure()` to the PHP-DSL
* Add support for autoconfigurable attributes on methods, properties and parameters
* Make auto-aliases private by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ private function autowireCalls(\ReflectionClass $reflectionClass, bool $isRoot,
/**
* Autowires the constructor or a method.
*
* @return array
*
* @throws AutowiringFailedException
*/
private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, array $arguments, bool $checkAttributes, int $methodIndex): array
Expand All @@ -250,13 +248,13 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
foreach ($parameter->getAttributes() as $attribute) {
if (TaggedIterator::class === $attribute->getName()) {
$attribute = $attribute->newInstance();
$arguments[$index] = new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute);
$arguments[$index] = new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, $attribute->defaultIndexMethod, false, $attribute->defaultPriorityMethod);
break;
}

if (TaggedLocator::class === $attribute->getName()) {
$attribute = $attribute->newInstance();
$arguments[$index] = new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, null, true));
$arguments[$index] = new ServiceLocatorArgument(new TaggedIteratorArgument($attribute->tag, $attribute->indexAttribute, $attribute->defaultIndexMethod, true, $attribute->defaultPriorityMethod));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedForDefaultPriorityClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooTagClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\IteratorConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\IteratorConsumerWithDefaultIndexMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\IteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\IteratorConsumerWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerConsumer;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerFactory;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultIndexMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithDefaultPriorityMethod;
use Symfony\Component\DependencyInjection\Tests\Fixtures\LocatorConsumerWithoutIndex;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService2;
Expand Down Expand Up @@ -354,30 +360,85 @@ public function testTaggedServiceWithIndexAttributeAndDefaultMethodConfiguredVia
$this->assertSame(['bar_tab_class_with_defaultmethod' => $container->get(BarTagClass::class), 'foo' => $container->get(FooTagClass::class)], $param);
}

public function testTaggedIteratorWithMultipleIndexAttribute()
/**
* @requires PHP 8
*/
public function testTaggedIteratorWithDefaultIndexMethodConfiguredViaAttribute()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
->setPublic(true)
->addTag('foo_bar', ['foo' => 'bar'])
->addTag('foo_bar', ['foo' => 'bar_duplicate'])
->addTag('foo_bar')
;
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(IteratorConsumerWithDefaultIndexMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

$s = $container->get(IteratorConsumerWithDefaultIndexMethod::class);

$param = iterator_to_array($s->getParam()->getIterator());
$this->assertSame(['bar_tag_class' => $container->get(BarTagClass::class), 'foo_tag_class' => $container->get(FooTagClass::class)], $param);
}

/**
* @requires PHP 8
*/
public function testTaggedIteratorWithDefaultPriorityMethodConfiguredViaAttribute()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooBarTaggedClass::class)
->addArgument(new TaggedIteratorArgument('foo_bar', 'foo'))
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(IteratorConsumerWithDefaultPriorityMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

$s = $container->get(FooBarTaggedClass::class);
$s = $container->get(IteratorConsumerWithDefaultPriorityMethod::class);

$param = iterator_to_array($s->getParam()->getIterator());
$this->assertSame([0 => $container->get(FooTagClass::class), 1 => $container->get(BarTagClass::class)], $param);
}

/**
* @requires PHP 8
*/
public function testTaggedIteratorWithDefaultIndexMethodAndWithDefaultPriorityMethodConfiguredViaAttribute()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(IteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

$s = $container->get(IteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class);

$param = iterator_to_array($s->getParam()->getIterator());
$this->assertSame(['bar' => $container->get(BarTagClass::class), 'bar_duplicate' => $container->get(BarTagClass::class), 'foo_tag_class' => $container->get(FooTagClass::class)], $param);
$this->assertSame(['foo_tag_class' => $container->get(FooTagClass::class), 'bar_tag_class' => $container->get(BarTagClass::class)], $param);
}

/**
Expand Down Expand Up @@ -438,6 +499,103 @@ public function testTaggedLocatorConfiguredViaAttributeWithoutIndex()
self::assertSame($container->get(FooTagClass::class), $locator->get(FooTagClass::class));
}

/**
* @requires PHP 8
*/
public function testTaggedLocatorWithDefaultIndexMethodConfiguredViaAttribute()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(LocatorConsumerWithDefaultIndexMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

/** @var LocatorConsumerWithoutIndex $s */
$s = $container->get(LocatorConsumerWithDefaultIndexMethod::class);

$locator = $s->getLocator();
self::assertSame($container->get(BarTagClass::class), $locator->get('bar_tag_class'));
self::assertSame($container->get(FooTagClass::class), $locator->get('foo_tag_class'));
}

/**
* @requires PHP 8
*/
public function testTaggedLocatorWithDefaultPriorityMethodConfiguredViaAttribute()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(LocatorConsumerWithDefaultPriorityMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

/** @var LocatorConsumerWithoutIndex $s */
$s = $container->get(LocatorConsumerWithDefaultPriorityMethod::class);

$locator = $s->getLocator();

// We need to check priority of instances in the factories
$factories = (new \ReflectionClass($locator))->getProperty('factories');
$factories->setAccessible(true);

self::assertSame([FooTagClass::class, BarTagClass::class], array_keys($factories->getValue($locator)));
}

/**
* @requires PHP 8
*/
public function testTaggedLocatorWithDefaultIndexMethodAndWithDefaultPriorityMethodConfiguredViaAttribute()
{
$container = new ContainerBuilder();
$container->register(BarTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(FooTagClass::class)
->setPublic(true)
->addTag('foo_bar')
;
$container->register(LocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class)
->setAutowired(true)
->setPublic(true)
;

$container->compile();

/** @var LocatorConsumerWithoutIndex $s */
$s = $container->get(LocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod::class);

$locator = $s->getLocator();

// We need to check priority of instances in the factories
$factories = (new \ReflectionClass($locator))->getProperty('factories');
$factories->setAccessible(true);

self::assertSame(['foo_tag_class', 'bar_tag_class'], array_keys($factories->getValue($locator)));
self::assertSame($container->get(BarTagClass::class), $locator->get('bar_tag_class'));
self::assertSame($container->get(FooTagClass::class), $locator->get('foo_tag_class'));
}

/**
* @requires PHP 8
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

final class IteratorConsumerWithDefaultIndexMethod
{
public function __construct(
#[TaggedIterator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName')]
private iterable $param,
) {
}

public function getParam(): iterable
{
return $this->param;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

final class IteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod
{
public function __construct(
#[TaggedIterator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName', defaultPriorityMethod: 'getPriority')]
private iterable $param,
) {
}

public function getParam(): iterable
{
return $this->param;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

final class IteratorConsumerWithDefaultPriorityMethod
{
public function __construct(
#[TaggedIterator(tag: 'foo_bar', defaultPriorityMethod: 'getPriority')]
private iterable $param,
) {
}

public function getParam(): iterable
{
return $this->param;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;

final class LocatorConsumerWithDefaultIndexMethod
{
public function __construct(
#[TaggedLocator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName')]
private ContainerInterface $locator,
) {
}

public function getLocator(): ContainerInterface
{
return $this->locator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;

final class LocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod
{
public function __construct(
#[TaggedLocator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName', defaultPriorityMethod: 'getPriority')]
private ContainerInterface $locator,
) {
}

public function getLocator(): ContainerInterface
{
return $this->locator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;

final class LocatorConsumerWithDefaultPriorityMethod
{
public function __construct(
#[TaggedLocator(tag: 'foo_bar', defaultPriorityMethod: 'getPriority')]
private ContainerInterface $locator,
) {
}

public function getLocator(): ContainerInterface
{
return $this->locator;
}
}