Skip to content
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

[DependencyInjection] Deprecate #[MapDecorated] in favor of #[AutowireDecorated] #49642

Merged
merged 1 commit into from
Mar 10, 2023
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-6.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ DependencyInjection

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

DoctrineBridge
--------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Attribute;

#[\Attribute(\Attribute::TARGET_PARAMETER)]
class AutowireDecorated
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace Symfony\Component\DependencyInjection\Attribute;

trigger_deprecation('symfony/dependency-injection', '6.3', 'The "%s" class is deprecated, use "%s" instead.', MapDecorated::class, AutowireDecorated::class);

/**
* @deprecated since Symfony 6.3, use AutowireDecorated instead
*/
#[\Attribute(\Attribute::TARGET_PARAMETER)]
class MapDecorated
{
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CHANGELOG
* Allow extending the `Autowire` attribute
* Add `#[Exclude]` to skip autoregistering a class
* Add support for autowiring services as closures using `#[AutowireCallable]` or `#[AutowireServiceClosure]`
* Deprecate `#[MapDecorated]`, use `#[AutowireDecorated]` instead

6.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Config\Resource\ClassExistenceResource;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\AutowireCallable;
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
use Symfony\Component\DependencyInjection\Attribute\MapDecorated;
use Symfony\Component\DependencyInjection\Attribute\Target;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -83,7 +84,7 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
return $this->processValue($this->container->getParameterBag()->resolveValue($value->value));
}

if ($value instanceof MapDecorated) {
if ($value instanceof AutowireDecorated || $value instanceof MapDecorated) {
$definition = $this->container->getDefinition($this->currentId);

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

foreach ($parameter->getAttributes(AutowireDecorated::class) as $attribute) {
$arguments[$index] = $this->processAttribute($attribute->newInstance(), $parameter->allowsNull());

continue 2;
}

foreach ($parameter->getAttributes(MapDecorated::class) as $attribute) {
$arguments[$index] = $this->processAttribute($attribute->newInstance(), $parameter->allowsNull());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\MapDecorated;
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\Attribute\Target;
Expand Down Expand Up @@ -426,7 +426,7 @@ public static function getSubscribedServices(): array
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%')),
new SubscribedService('map.decorated', \stdClass::class, attributes: new MapDecorated()),
new SubscribedService('autowire.decorated', \stdClass::class, attributes: new AutowireDecorated()),
new SubscribedService('target', \stdClass::class, attributes: new Target('someTarget')),
];
}
Expand All @@ -449,7 +449,7 @@ public static function getSubscribedServices(): array
'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%')])),
'map.decorated' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'map.decorated', [new MapDecorated()])),
'autowire.decorated' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'autowire.decorated', [new AutowireDecorated()])),
'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));
Expand All @@ -462,7 +462,7 @@ public static function getSubscribedServices(): array
'autowired' => new ServiceClosureArgument(new Reference('service.id')),
'autowired.nullable' => new ServiceClosureArgument(new Reference('service.id', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
'autowired.parameter' => new ServiceClosureArgument('foobar'),
'map.decorated' => new ServiceClosureArgument(new Reference('.service_locator.EeZIdVM.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
'autowire.decorated' => new ServiceClosureArgument(new Reference('.service_locator.QVDPERh.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)),
'target' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'someTarget', [new Target('someTarget')])),
];
$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\MapDecorated;
use Symfony\Component\DependencyInjection\Attribute\AutowireDecorated;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -80,23 +80,23 @@ class AsDecoratorFoo implements AsDecoratorInterface
#[AsDecorator(decorates: AsDecoratorFoo::class, priority: 10)]
class AsDecoratorBar10 implements AsDecoratorInterface
{
public function __construct(string $arg1, #[MapDecorated] AsDecoratorInterface $inner)
public function __construct(string $arg1, #[AutowireDecorated] AsDecoratorInterface $inner)
{
}
}

#[AsDecorator(decorates: AsDecoratorFoo::class, priority: 20)]
class AsDecoratorBar20 implements AsDecoratorInterface
{
public function __construct(string $arg1, #[MapDecorated] AsDecoratorInterface $inner)
public function __construct(string $arg1, #[AutowireDecorated] AsDecoratorInterface $inner)
{
}
}

#[AsDecorator(decorates: \NonExistent::class, onInvalid: ContainerInterface::NULL_ON_INVALID_REFERENCE)]
class AsDecoratorBaz implements AsDecoratorInterface
{
public function __construct(#[MapDecorated] AsDecoratorInterface $inner = null)
public function __construct(#[AutowireDecorated] AsDecoratorInterface $inner = null)
{
}
}
Expand All @@ -106,7 +106,7 @@ class AutowireNestedAttributes implements AsDecoratorInterface
{
public function __construct(
#[Autowire([
'decorated' => new MapDecorated(),
'decorated' => new AutowireDecorated(),
'iterator' => new TaggedIterator('foo'),
'locator' => new TaggedLocator('foo'),
'service' => new Autowire(service: 'bar')
Expand Down