Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DependencyInjection] Deprecate #[TaggedIterator] and #[TaggedLocator] #54371

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
May 2, 2024
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
1 change: 1 addition & 0 deletions UPGRADE-7.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ DependencyInjection
-------------------

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

DoctrineBridge
--------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* Autowires an iterator of services based on a tag name.
*
* @deprecated since Symfony 7.1, use {@see AutowireIterator} instead.
*/
#[\Attribute(\Attribute::TARGET_PARAMETER)]
class TaggedIterator extends AutowireIterator
Expand All @@ -33,6 +35,8 @@ public function __construct(
public string|array $exclude = [],
public bool $excludeSelf = true,
) {
trigger_deprecation('symfony/dependency-injection', '7.1', 'The "%s" attribute is deprecated, use "%s" instead.', self::class, AutowireIterator::class);

parent::__construct($tag, $indexAttribute, $defaultIndexMethod, $defaultPriorityMethod, $exclude, $excludeSelf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* Autowires a locator of services based on a tag name.
*
* @deprecated since Symfony 7.1, use {@see AutowireLocator} instead.
*/
#[\Attribute(\Attribute::TARGET_PARAMETER)]
class TaggedLocator extends AutowireLocator
Expand All @@ -33,6 +35,8 @@ public function __construct(
public string|array $exclude = [],
public bool $excludeSelf = true,
) {
trigger_deprecation('symfony/dependency-injection', '7.1', 'The "%s" attribute is deprecated, use "%s" instead.', self::class, AutowireLocator::class);

parent::__construct($tag, $indexAttribute, $defaultIndexMethod, $defaultPriorityMethod, $exclude, $excludeSelf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService3Configurator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService4;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TaggedService5;
use Symfony\Contracts\Service\Attribute\SubscribedService;
use Symfony\Contracts\Service\ServiceProviderInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

Expand Down Expand Up @@ -395,10 +394,6 @@ public function testTaggedServiceWithIndexAttributeAndDefaultMethod()

public function testLocatorConfiguredViaAttribute()
{
if (!property_exists(SubscribedService::class, 'type')) {
$this->markTestSkipped('Requires symfony/service-contracts >= 3.2');
}

$container = new ContainerBuilder();
$container->setParameter('some.parameter', 'foo');
$container->register(BarTagClass::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,18 +411,12 @@ public static function getSubscribedServices(): array

public function testSubscribedServiceWithAttributes()
{
if (!property_exists(SubscribedService::class, 'attributes')) {
$this->markTestSkipped('Requires symfony/service-contracts 3.2+');
}

$container = new ContainerBuilder();

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

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

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

/**
* @group legacy
*/
public function testSubscribedServiceWithLegacyAttributes()
{
$container = new ContainerBuilder();

$subscriber = new class() implements ServiceSubscriberInterface {
public static function getSubscribedServices(): array
{
return [
new SubscribedService('tagged.iterator', 'iterable', attributes: new TaggedIterator('tag')),
new SubscribedService('tagged.locator', PsrContainerInterface::class, attributes: new TaggedLocator('tag')),
];
}
};

$container->setParameter('parameter.1', 'foobar');
$container->register('foo', $subscriber::class)
->addMethodCall('setContainer', [new Reference(PsrContainerInterface::class)])
->addTag('container.service_subscriber');

(new RegisterServiceSubscribersPass())->process($container);
(new ResolveServiceSubscribersPass())->process($container);

$foo = $container->getDefinition('foo');
$locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]);

$expected = [
'tagged.iterator' => new ServiceClosureArgument(new TypedReference('iterable', 'iterable', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'tagged.iterator', [new TaggedIterator('tag')])),
'tagged.locator' => new ServiceClosureArgument(new TypedReference(PsrContainerInterface::class, PsrContainerInterface::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'tagged.locator', [new TaggedLocator('tag')])),
];
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));

(new AutowirePass())->process($container);

$expected = [
'tagged.iterator' => new ServiceClosureArgument(new TaggedIteratorArgument('tag')),
'tagged.locator' => new ServiceClosureArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('tag', 'tag', needsIndexes: true))),
];
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
}

public function testBinding()
{
$container = new ContainerBuilder();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;

final class TaggedConsumerWithExclude implements AutoconfiguredInterface2
{
public function __construct(
#[TaggedIterator(AutoconfiguredInterface2::class, exclude: self::class)]
#[AutowireIterator(AutoconfiguredInterface2::class, exclude: self::class)]
public iterable $items,
#[TaggedLocator(AutoconfiguredInterface2::class, exclude: self::class)]
#[AutowireLocator(AutoconfiguredInterface2::class, exclude: self::class)]
public ContainerInterface $locator,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;

final class TaggedIteratorConsumerWithDefaultIndexMethod
{
public function __construct(
#[TaggedIterator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName')]
#[AutowireIterator('foo_bar', defaultIndexMethod: 'getDefaultFooName')]
private iterable $param,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;

final class TaggedIteratorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod
{
public function __construct(
#[TaggedIterator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName', defaultPriorityMethod: 'getPriority')]
#[AutowireIterator('foo_bar', defaultIndexMethod: 'getDefaultFooName', defaultPriorityMethod: 'getPriority')]
private iterable $param,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;

final class TaggedIteratorConsumerWithDefaultPriorityMethod
{
public function __construct(
#[TaggedIterator(tag: 'foo_bar', defaultPriorityMethod: 'getPriority')]
#[AutowireIterator('foo_bar', defaultPriorityMethod: 'getPriority')]
private iterable $param,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

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

final class TaggedLocatorConsumerFactory
{
public function __invoke(
#[TaggedLocator('foo_bar', indexAttribute: 'key')]
#[AutowireLocator('foo_bar', indexAttribute: 'key')]
ContainerInterface $locator
): TaggedLocatorConsumer {
return new TaggedLocatorConsumer($locator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

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

final class TaggedLocatorConsumerWithDefaultIndexMethod
{
public function __construct(
#[TaggedLocator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName')]
#[AutowireLocator('foo_bar', defaultIndexMethod: 'getDefaultFooName')]
private ContainerInterface $locator,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

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

final class TaggedLocatorConsumerWithDefaultIndexMethodAndWithDefaultPriorityMethod
{
public function __construct(
#[TaggedLocator(tag: 'foo_bar', defaultIndexMethod: 'getDefaultFooName', defaultPriorityMethod: 'getPriority')]
#[AutowireLocator('foo_bar', defaultIndexMethod: 'getDefaultFooName', defaultPriorityMethod: 'getPriority')]
private ContainerInterface $locator,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

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

final class TaggedLocatorConsumerWithDefaultPriorityMethod
{
public function __construct(
#[TaggedLocator(tag: 'foo_bar', defaultPriorityMethod: 'getPriority')]
#[AutowireLocator('foo_bar', defaultPriorityMethod: 'getPriority')]
private ContainerInterface $locator,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

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

public function __construct(
#[TaggedLocator('foo_bar', indexAttribute: 'key')]
#[AutowireLocator('foo_bar', indexAttribute: 'key')]
private ContainerInterface $locator,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

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

final class TaggedLocatorConsumerWithoutIndex
{
public function __construct(
#[TaggedLocator('foo_bar')]
#[AutowireLocator('foo_bar')]
private ContainerInterface $locator,
) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
use Symfony\Component\DependencyInjection\Attribute\Lazy;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Contracts\Service\Attribute\Required;

Expand Down Expand Up @@ -119,8 +119,8 @@ class AutowireNestedAttributes implements AsDecoratorInterface
public function __construct(
#[Autowire([
'decorated' => new AutowireDecorated(),
'iterator' => new TaggedIterator('foo'),
'locator' => new TaggedLocator('foo'),
'iterator' => new AutowireIterator('foo'),
'locator' => new AutowireLocator('foo'),
'service' => new Autowire(service: 'bar')
])] array $options)
{
Expand Down
Loading