8000 feature #54371 [DependencyInjection] Deprecate `#[TaggedIterator]` an… · symfony/symfony@0302e1d · GitHub
[go: up one dir, main page]

Skip to content

Commit 0302e1d

Browse files
committed
feature #54371 [DependencyInjection] Deprecate #[TaggedIterator] and #[TaggedLocator] (GromNaN)
This PR was merged into the 7.1 branch. Discussion ---------- [DependencyInjection] Deprecate `#[TaggedIterator]` and `#[TaggedLocator]` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | yes | Issues | Planned in #51832 | License | MIT `#[TaggedIterator]` and `#[TaggedLocator]` attributes are replaced by `#[AutowireIterator]` and `#[AutowireLocator]`, for naming consistency with all `#[Autowire*]` attributes. Commits ------- 4bc6bed Deprecate #[TaggedIterator] and #[TaggedLocator]
2 parents fa77dc6 + 4bc6bed commit 0302e1d

18 files changed

+123
-64
lines changed

UPGRADE-7.1.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ DependencyInjection
4747
-------------------
4848

4949
* [BC BREAK] When used in the `prependExtension()` method, the `ContainerConfigurator::import()` method now prepends the configuration instead of appending it
50+
* Deprecate `#[TaggedIterator]` and `#[TaggedLocator]` attributes, use `#[AutowireIterator]` and `#[AutowireLocator]` instead
5051

5152
DoctrineBridge
5253
--------------

src/Symfony/Component/DependencyInjection/Attribute/TaggedIterator.php

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
/**
1515
* Autowires an iterator of services based on a tag name.
16+
*
17+
* @deprecated since Symfony 7.1, use {@see AutowireIterator} instead.
1618
*/
1719
#[\Attribute(\Attribute::TARGET_PARAMETER)]
1820
class TaggedIterator extends AutowireIterator
@@ -33,6 +35,8 @@ public function __construct(
3335
public string|array $exclude = [],
3436
public bool $excludeSelf = true,
3537
) {
38+
trigger_deprecation('symfony/dependency-injection', '7.1', 'The "%s" attribute is deprecated, use "%s" instead.', self::class, AutowireIterator::class);
39+
3640
parent::__construct($tag, $indexAttribute, $defaultIndexMethod, $defaultPriorityMethod, $exclude, $excludeSelf);
3741
}
3842
}

src/Symfony/Component/DependencyInjection/Attribute/TaggedLocator.php

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
/**
1515
* Autowires a locator of services based on a tag name.
16+
*
17+
* @deprecated since Symfony 7.1, use {@see AutowireLocator} instead.
1618
*/
1719
#[\Attribute(\Attribute::TARGET_PARAMETER)]
1820
class TaggedLocator extends AutowireLocator
@@ -33,6 +35,8 @@ public function __construct(
3335
public string|array $exclude = [],
3436
public bool $excludeSelf = true,
3537
) {
38+
trigger_deprecation('symfony/dependency-injection', '7.1', 'The "%s" attribute is deprecated, use "%s" instead.', self::class, AutowireLocator::class);
39+
3640
parent::__construct($tag, $indexAttribute, $defaultIndexMethod, $defaultPriorityMethod, $exclude, $excludeSelf);
3741
}
3842
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3Configurator;
5959
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService4;
6060
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService5;
61-
use Symfony\Contracts\Service\Attribute\SubscribedService;
6261
use Symfony\Contracts\Service\ServiceProviderInterface;
6362
use Symfony\Contracts\Service\ServiceSubscriberInterface;
6463

@@ -395,10 +394,6 @@ public function testTaggedServiceWithIndexAttributeAndDefaultMethod()
395394

396395
public function testLocatorConfiguredViaAttribute()
397396
{
398-
if (!property_exists(SubscribedService::class, 'type')) {
399-
$this->markTestSkipped('Requires symfony/service-contracts >= 3.2');
400-
}
401-
402397
$container = new ContainerBuilder();
403398
$container->setParameter('some.parameter', 'foo');
404399
$container->register(BarTagClass::class)

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterServiceSubscribersPassTest.php

+44-11
Original file line numberDiff line numberDiff line change
@@ -411,18 +411,12 @@ public static function getSubscribedServices(): array
411411

412412
public function testSubscribedServiceWithAttributes()
413413
{
414-
if (!property_exists(SubscribedService::class, 'attributes')) {
415-
$this->markTestSkipped('Requires symfony/service-contracts 3.2+');
416-
}
417-
418414
$container = new ContainerBuilder();
419415

420416
$subscriber = new class() implements ServiceSubscriberInterface {
421417
public static function getSubscribedServices(): array
422418
{
423419
return [
424-
new SubscribedService('tagged.iterator', 'iterable', attributes: new TaggedIterator('tag')),
425-
new SubscribedService('tagged.locator', PsrContainerInterface::class, attributes: new TaggedLocator('tag')),
426420
new SubscribedService('autowired', 'stdClass', attributes: new Autowire(service: 'service.id')),
427421
new SubscribedService('autowired.nullable', 'stdClass', nullable: true, attributes: new Autowire(service: 'service.id')),
428422
new SubscribedService('autowired.parameter', 'string', attributes: new Autowire('%parameter.1%')),
@@ -444,8 +438,6 @@ public static function getSubscribedServices(): array
444438
$locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]);
445439

446440
$expected = [
447-
'tagged.iterator' => new ServiceClosureArgument(new TypedReference('iterable', 'iterable', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'tagged.iterator', [new TaggedIterator('tag')])),
448-
'tagged.locator' => new ServiceClosureArgument(new TypedReference(PsrContainerInterface::class, PsrContainerInterface::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'tagged.locator', [new TaggedLocator('tag')])),
449441
'autowired' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'autowired', [new Autowire(service: 'service.id')])),
450442
'autowired.nullable' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'autowired.nullable', [new Autowire(service: 'service.id')])),
451443
'autowired.parameter' => new ServiceClosureArgument(new TypedReference('string', 'string', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'autowired.parameter', [new Autowire(service: '%parameter.1%')])),
@@ -457,17 +449,58 @@ public static function getSubscribedServices(): array
457449
(new AutowirePass())->process($container);
458450

459451
$expected = [
460-
'tagged.iterator' => new ServiceClosureArgument(new TaggedIteratorArgument('tag')),
461-
'tagged.locator' => new ServiceClosureArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('tag', 'tag', needsIndexes: true))),
462452
'autowired' => new ServiceClosureArgument(new TypedReference('service.id', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'autowired', [new Autowire(service: 'service.id')])),
463453
'autowired.nullable' => new ServiceClosureArgument(new TypedReference('service.id', 'stdClass', ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'autowired.nullable', [new Autowire(service: 'service.id')])),
464454
'autowired.parameter' => new ServiceClosureArgument('foobar'),
465-
'autowire.decorated' => new ServiceClosureArgument(new Reference('.service_locator.420ES7z.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
455+
'autowire.decorated' => new ServiceClosureArgument(new Reference('.service_locator.4qmCWv..inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
466456
'target' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'target', [new Target('someTarget')])),
467457
];
468458
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
469459
}
470460

461+
/**
462+
* @group legacy
463+
*/
464+
public function testSubscribedServiceWithLegacyAttributes()
465+
{
466+
$container = new ContainerBuilder();
467+
468+
$subscriber = new class() implements ServiceSubscriberInterface {
469+
public static function getSubscribedServices(): array
470+
{
471+
return [
472+
new SubscribedService('tagged.iterator', 'iterable', attributes: new TaggedIterator('tag')),
473+
new SubscribedService('tagged.locator', PsrContainerInterface::class, attributes: new TaggedLocator('tag')),
474+
];
475+
}
476+
};
477+
478+
$container->setParameter('parameter.1', 'foobar');
479+
$container->register('foo', $subscriber::class)
480+
->addMethodCall('setContainer', [new Reference(PsrContainerInterface::class)])
481+
->addTag('container.service_subscriber');
482+
483+
(new RegisterServiceSubscribersPass())->process($container);
484+
(new ResolveServiceSubscribersPass())->process($container);
485+
486+
$foo = $container->getDefinition('foo');
487+
$locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]);
488+
489+
$expected = [
490+
'tagged.iterator' => new ServiceClosureArgument(new TypedReference('iterable', 'iterable', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'tagged.iterator', [new TaggedIterator('tag')])),
491+
'tagged.locator' => new ServiceClosureArgument(new TypedReference(PsrContainerInterface::class, PsrContainerInterface::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'tagged.locator', [new TaggedLocator('tag')])),
492+
];
493+
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
494+
495+
(new AutowirePass())->process($container);
496+
497+
$expected = [
498+
'tagged.iterator' => new ServiceClosureArgument(new TaggedIteratorArgument('tag')),
499+
'tagged.locator' => new ServiceClosureArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('tag', 'tag', needsIndexes: true))),
500+
];
501+
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
502+
}
503+
471504
public function testBinding()
472505
{
473506
$container = new ContainerBuilder();

src/Symfony/Component/DependencyInjection/Tests/Fixtures/MultipleArgumentBindings.php

-15
This file was deleted.

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedConsumerWithExclude.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
1313

1414
use Psr\Container\ContainerInterface;
15-
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
16-
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
15+
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
16+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
1717

1818
final class TaggedConsumerWithExclude implements AutoconfiguredInterface2
1919
{
2020
public function __construct(
21-
#[TaggedIterator(AutoconfiguredInterface2::class, exclude: self::class)]
21+
#[AutowireIterator(AutoconfiguredInterface2::class, exclude: self::class)]
2222
public iterable $items,
23-
#[TaggedLocator(AutoconfiguredInterface2::class, exclude: self::class)]
23+
#[AutowireLocator(AutoconfiguredInterface2::class, exclude: self::class)]
2424
public ContainerInterface $locator,
2525
) {
2626
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedIteratorConsumerWithDefaultIndexMethod.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
44

5-
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
5+
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
66

77
final class TaggedIteratorConsumerWithDefaultIndexMethod
88
{
99
public function __construct(
10-
#[TaggedIterator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName')]
10+
#[AutowireIterator('foo_bar', defaultIndexMethod: 'getDefaultFooName')]
1111
private iterable $param,
1212
) {
1313
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedIteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
44

5-
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
5+
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
66

77
final class TaggedIteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod
88
{
99
public function __construct(
10-
#[TaggedIterator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName', defaultPriorityMethod: 'getPriority')]
10+
#[AutowireIterator('foo_bar', defaultIndexMethod: 'getDefaultFooName', defaultPriorityMethod: 'getPriority')]
1111
private iterable $param,
1212
) {
1313
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedIteratorConsumerWithDefaultPriorityMethod.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
44

5-
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
5+
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
66

77
final class TaggedIteratorConsumerWithDefaultPriorityMethod
88
{
99
public function __construct(
10-
#[TaggedIterator(tag: 'foo_bar', defaultPriorityMethod: 'getPriority')]
10+
#[AutowireIterator('foo_bar', defaultPriorityMethod: 'getPriority')]
1111
private iterable $param,
1212
) {
1313
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedLocatorConsumerFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
1313

1414
use Psr\Container\ContainerInterface;
15-
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
15+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
1616

1717
final class TaggedLocatorConsumerFactory
1818
{
1919
public function __invoke(
20-
#[TaggedLocator('foo_bar', indexAttribute: 'key')]
20+
#[AutowireLocator('foo_bar', indexAttribute: 'key')]
2121
ContainerInterface $locator
2222
): TaggedLocatorConsumer {
2323
return new TaggedLocatorConsumer($locator);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedLocatorConsumerWithDefaultIndexMethod.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
44

55
use Psr\Container\ContainerInterface;
6+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
67
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
78

89
final class TaggedLocatorConsumerWithDefaultIndexMethod
910
{
1011
public function __construct(
11-
#[TaggedLocator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName')]
12+
#[AutowireLocator('foo_bar', defaultIndexMethod: 'getDefaultFooName')]
1213
private ContainerInterface $locator,
1314
) {
1415
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedLocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
44

55
use Psr\Container\ContainerInterface;
6-
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
6+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
77

88
final class TaggedLocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod
99
{
1010
public function __construct(
11-
#[TaggedLocator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName', defaultPriorityMethod: 'getPriority')]
11+
#[AutowireLocator('foo_bar', defaultIndexMethod: 'getDefaultFooName', defaultPriorityMethod: 'getPriority')]
1212
private ContainerInterface $locator,
1313
) {
1414
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedLocatorConsumerWithDefaultPriorityMethod.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
44

55
use Psr\Container\ContainerInterface;
6+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
67
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
78

89
final class TaggedLocatorConsumerWithDefaultPriorityMethod
910
{
1011
public function __construct(
11-
#[TaggedLocator(tag: 'foo_bar', defaultPriorityMethod: 'getPriority')]
12+
#[AutowireLocator('foo_bar', defaultPriorityMethod: 'getPriority')]
1213
private ContainerInterface $locator,
1314
) {
1415
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedLocatorConsumerWithServiceSubscriber.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
1313

1414
use Psr\Container\ContainerInterface;
15-
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
15+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
1616
use Symfony\Contracts\Service\Attribute\Required;
1717
use Symfony\Contracts\Service\ServiceSubscriberInterface;
1818

@@ -21,7 +21,7 @@ final class TaggedLocatorConsumerWithServiceSubscriber implements ServiceSubscri
2121
private ?ContainerInterface $container = null;
2222

2323
public function __construct(
24-
#[TaggedLocator('foo_bar', indexAttribute: 'key')]
24+
#[AutowireLocator('foo_bar', indexAttribute: 'key')]
2525
private ContainerInterface $locator,
2626
) {
2727
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TaggedLocatorConsumerWithoutIndex.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
1313

1414
use Psr\Container\ContainerInterface;
15-
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
15+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
1616

1717
final class TaggedLocatorConsumerWithoutIndex
1818
{
1919
public function __construct(
20-
#[TaggedLocator('foo_bar')]
20+
#[AutowireLocator('foo_bar')]
2121
private ContainerInterface $locator,
2222
) {
2323
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
66
use Symfony\Component\DependencyInjection\Attribute\Autowire;
77
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
8+
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
9+
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
810
use Symfony\Component\DependencyInjection\Attribute\Lazy;
9-
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
10-
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
1111
use Symfony\Component\DependencyInjection\ContainerInterface;
1212
use Symfony\Contracts\Service\Attribute\Required;
1313

@@ -130,8 +130,8 @@ class AutowireNestedAttributes implements AsDecoratorInterface
130130
public function __construct(
131131
#[Autowire([
132132
'decorated' => new AutowireDecorated(),
133-
'iterator' => new TaggedIterator('foo'),
134-
'locator' => new TaggedLocator('foo'),
133+
'iterator' => new AutowireIterator('foo'),
134+
'locator' => new AutowireLocator('foo'),
135135
'service' => new Autowire(service: 'bar')
136136
])] array $options)
137137
{

0 commit comments

Comments
 (0)
0