8000 feature #49642 [DependencyInjection] Deprecate `#[MapDecorated]` in f… · symfony/symfony@a7e0b05 · GitHub
[go: up one dir, main page]

Skip to content

Commit a7e0b05

Browse files
committed
feature #49642 [DependencyInjection] Deprecate #[MapDecorated] in favor of #[AutowireDecorated] (nicolas-grekas)
This PR was merged into the 6.3 branch. Discussion ---------- [DependencyInjection] Deprecate `#[MapDecorated]` in favor of `#[AutowireDecorated]` | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | - | License | MIT | Doc PR | - `#[Map*]` are for controller argument resolvers. `#[Autowire*]` should be used for autowiring (see #49628). We could also rename `#[TaggedLocator]` and `#[TaggedIterator]` but I feel like the need is less obvious and the cost more important. Commits ------- b653adf [DependencyInjection] Deprecate `#[MapDecorated]` in favor of `#[AutowireDecorated]`
2 parents 550b92f + b653adf commit a7e0b05

File tree

7 files changed

+42
-10
lines changed

7 files changed

+42
-10
lines changed

UPGRADE-6.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ DependencyInjection
1212

1313
* Deprecate `PhpDumper` options `inline_factories_parameter` and `inline_class_loader_parameter`, use `inline_factories` and `inline_class_loader` instead
1414
* Deprecate undefined and numeric keys with `service_locator` config, use string aliases instead
15+
* Deprecate `#[MapDecorated]`, use `#[AutowireDecorated]` instead
1516

1617
DoctrineBridge
1718
--------------
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Attribute;
13+
14+
#[\Attribute(\Attribute::TARGET_PARAMETER)]
15+
class AutowireDecorated
16+
{
17+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Attribute;
1313

14+
trigger_deprecation('symfony/dependency-injection', '6.3', 'The "%s" class is deprecated, use "%s" instead.', MapDecorated::class, AutowireDecorated::class);
15+
16+
/**
17+
* @deprecated since Symfony 6.3, use AutowireDecorated instead
18+
*/
1419
#[\Attribute(\Attribute::TARGET_PARAMETER)]
1520
class MapDecorated
1621
{

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CHANGELOG
1616
* Allow extending the `Autowire` attribute
1717
* Add `#[Exclude]` to skip autoregistering a class
1818
* Add support for autowiring services as closures using `#[AutowireCallable]` or `#[AutowireServiceClosure]`
19+
* Deprecate `#[MapDecorated]`, use `#[AutowireDecorated]` instead
1920

2021
6.2
2122
---

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Config\Resource\ClassExistenceResource;
1515
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1616
use Symfony\Component\DependencyInjection\Attribute\AutowireCallable;
17+
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
1718
use Symfony\Component\DependencyInjection\Attribute\MapDecorated;
1819
use Symfony\Component\DependencyInjection\Attribute\Target;
1920
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -83,7 +84,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
8384
return $this->processValue($this->container->getParameterBag()->resolveValue($value->value));
8485
}
8586

86-
if ($value instanceof MapDecorated) {
87+
if ($value instanceof AutowireDecorated || $value instanceof MapDecorated) {
8788
$definition = $this->container->getDefinition($this->currentId);
8889

8990
return new Reference($definition->innerServiceId ?? $this->currentId.'.inner', $definition->decorationOnInvalid ?? ContainerInterface::NULL_ON_INVALID_REFERENCE);
@@ -180,6 +181,7 @@ private function processAttribute(object $attribute, bool $isOptional = false):
180181
return new Reference($attribute->value, ContainerInterface::NULL_ON_INVALID_REFERENCE);
181182
}
182183
// no break
184+
case $attribute instanceof AutowireDecorated:
183185
case $attribute instanceof MapDecorated:
184186
return $this->processValue($attribute);
185187
}
@@ -296,6 +298,12 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
296298
continue 2;
297299
}
298300

301+
foreach ($parameter->getAttributes(AutowireDecorated::class) as $attribute) {
302+
$arguments[$index] = $this->processAttribute($attribute->newInstance(), $parameter->allowsNull());
303+
304+
continue 2;
305+
}
306+
299307
foreach ($parameter->getAttributes(MapDecorated::class) as $attribute) {
300308
$arguments[$index] = $this->processAttribute($attribute->newInstance(), $parameter->allowsNull());
301309

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1818
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1919
use Symfony\Component\DependencyInjection\Attribute\Autowire;
20-
use Symfony\Component\DependencyInjection\Attribute\MapDecorated;
20+
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
2121
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
2222
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
2323
use Symfony\Component\DependencyInjection\Attribute\Target;
@@ -426,7 +426,7 @@ public static function getSubscribedServices(): array
426426
new SubscribedService('autowired', 'stdClass', attributes: new Autowire(service: 'service.id')),
427427
new SubscribedService('autowired.nullable', 'stdClass', nullable: true, attributes: new Autowire(service: 'service.id')),
428428
new SubscribedService('autowired.parameter', 'string', attributes: new Autowire('%parameter.1%')),
429-
new SubscribedService('map.decorated', \stdClass::class, attributes: new MapDecorated()),
429+
new SubscribedService('autowire.decorated', \stdClass::class, attributes: new AutowireDecorated()),
430430
new SubscribedService('target', \stdClass::class, attributes: new Target('someTarget')),
431431
];
432432
}
@@ -449,7 +449,7 @@ public static function getSubscribedServices(): array
449449
'autowired' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'autowired', [new Autowire(service: 'service.id')])),
450450
'autowired.nullable' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'autowired.nullable', [new Autowire(service: 'service.id')])),
451451
'autowired.parameter' => new ServiceClosureArgument(new TypedReference('string', 'string', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'autowired.parameter', [new Autowire(service: '%parameter.1%')])),
452-
'map.decorated' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'map.decorated', [new MapDecorated()])),
452+
'autowire.decorated' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'autowire.decorated', [new AutowireDecorated()])),
453453
'target' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'target', [new Target('someTarget')])),
454454
];
455455
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
@@ -462,7 +462,7 @@ public static function getSubscribedServices(): array
462462
'autowired' => new ServiceClosureArgument(new Reference('service.id')),
463463
'autowired.nullable' => new ServiceClosureArgument(new Reference('service.id', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
464464
'autowired.parameter' => new ServiceClosureArgument('foobar'),
465-
'map.decorated' => new ServiceClosureArgument(new Reference('.service_locator.EeZIdVM.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
465+
'autowire.decorated' => new ServiceClosureArgument(new Reference('.service_locator.QVDPERh.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
466466
'target' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'someTarget', [new Target('someTarget')])),
467467
];
468468
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
66
use Symfony\Component\DependencyInjection\Attribute\Autowire;
7-
use Symfony\Component\DependencyInjection\Attribute\MapDecorated;
7+
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
88
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
99
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
1010
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -80,23 +80,23 @@ class AsDecoratorFoo implements AsDecoratorInterface
8080
#[AsDecorator(decorates: AsDecoratorFoo::class, priority: 10)]
8181
class AsDecoratorBar10 implements AsDecoratorInterface
8282
{
83-
public function __construct(string $arg1, #[MapDecorated] AsDecoratorInterface $inner)
83+
public function __construct(string $arg1, #[AutowireDecorated] AsDecoratorInterface $inner)
8484
{
8585
}
8686
}
8787

8888
#[AsDecorator(decorates: AsDecoratorFoo::class, priority: 20)]
8989
class AsDecoratorBar20 implements AsDecoratorInterface
9090
{
91-
public function __construct(string $arg1, #[MapDecorated] AsDecoratorInterface $inner)
91+
public function __construct(string $arg1, #[AutowireDecorated] AsDecoratorInterface $inner)
9292
{
9393
}
9494
}
9595

9696
#[AsDecorator(decorates: \NonExistent::class, onInvalid: ContainerInterface::NULL_ON_INVALID_REFERENCE)]
9797
class AsDecoratorBaz implements AsDecoratorInterface
9898
{
99-
public function __construct(#[MapDecorated] AsDecoratorInterface $inner = null)
99+
public function __construct(#[AutowireDecorated] AsDecoratorInterface $inner = null)
100100
{
101101
}
102102
}
@@ -106,7 +106,7 @@ class AutowireNestedAttributes implements AsDecoratorInterface
106106
{
107107
public function __construct(
108108
#[Autowire([
109-
'decorated' => new MapDecorated(),
109+
'decorated' => new AutowireDecorated(),
110110
'iterator' => new TaggedIterator('foo'),
111111
'locator' => new TaggedLocator('foo'),
112112
'service' => new Autowire(service: 'bar')

0 commit comments

Comments
 (0)
0