Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit84d35a2

Browse files
feature#45834 [DependencyInjection] add AsDecorator class attribute and InnerService parameter attribute (Jean-Beru)
This PR was squashed before being merged into the 6.1 branch.Discussion----------[DependencyInjection] add AsDecorator class attribute and InnerService parameter attribute| Q | A| ------------- | ---| Branch? | 6.1| Bug fix? | no| New feature? | yes| Deprecations? | no| Tickets || License | MIT| Doc PR | WIPThe aim f this PR is to allow decorator declaration with PHP8 attributes by using `AsDecorator` attribute on class and, optionally, `InnerService` parameter attribute to inject decorated service.To use it :```phpinterface FooInterface{ public function myMethod(): string;}final class Foo implements FooInterface{ public function myMethod(): string { return 'foo'; }}#[AsDecorator( decorates: Foo::class, priority: 5, onInvalid: ContainerInterface::NULL_ON_INVALID_REFERENCE,)]final class Bar implements FooInterface{ private string $arg1; private ?FooInterface $foo; public function __construct(string $arg1, #[InnerService] FooInterface $foo = null) { $this->arg1 = $arg1; $this->foo = $foo; } public function myMethod(): string { if (null === $this->foo) { return 'bar'; } return $this->foo->myMethod().' bar '; }}```Commits-------d8a4680 [DependencyInjection] add AsDecorator class attribute and InnerService parameter attribute
2 parents55f6d9d +d8a4680 commit84d35a2

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
namespaceSymfony\Component\DependencyInjection\Attribute;
13+
14+
useSymfony\Component\DependencyInjection\ContainerInterface;
15+
16+
#[\Attribute(\Attribute::TARGET_CLASS)]
17+
class AsDecorator
18+
{
19+
publicfunction__construct(
20+
publicstring$decorates,
21+
publicint$priority =0,
22+
publicint$onInvalid = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
23+
) {
24+
}
25+
}
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+
namespaceSymfony\Component\DependencyInjection\Attribute;
13+
14+
#[\Attribute(\Attribute::TARGET_PARAMETER)]
15+
class InnerService
16+
{
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
namespaceSymfony\Component\DependencyInjection\Compiler;
13+
14+
useSymfony\Component\DependencyInjection\Attribute\AsDecorator;
15+
useSymfony\Component\DependencyInjection\ContainerBuilder;
16+
useSymfony\Component\DependencyInjection\Definition;
17+
18+
/**
19+
* Reads #[AsDecorator] attributes on definitions that are autowired
20+
* and don't have the "container.ignore_attributes" tag.
21+
*/
22+
finalclass AutowireAsDecoratorPassimplements CompilerPassInterface
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
publicfunctionprocess(ContainerBuilder$container)
28+
{
29+
foreach ($container->getDefinitions()as$definition) {
30+
if ($this->accept($definition) &&$reflectionClass =$container->getReflectionClass($definition->getClass(),false)) {
31+
$this->processClass($definition,$reflectionClass);
32+
}
33+
}
34+
}
35+
36+
privatefunctionaccept(Definition$definition):bool
37+
{
38+
return !$definition->hasTag('container.ignore_attributes') &&$definition->isAutowired();
39+
}
40+
41+
privatefunctionprocessClass(Definition$definition,\ReflectionClass$reflectionClass)
42+
{
43+
foreach ($reflectionClass->getAttributes(AsDecorator::class, \ReflectionAttribute::IS_INSTANCEOF)as$attribute) {
44+
$attribute =$attribute->newInstance();
45+
46+
$definition->setDecoratedService($attribute->decorates,null,$attribute->priority,$attribute->onInvalid);
47+
}
48+
}
49+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
useSymfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1616
useSymfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1717
useSymfony\Component\DependencyInjection\Attribute\Autowire;
18+
useSymfony\Component\DependencyInjection\Attribute\InnerService;
1819
useSymfony\Component\DependencyInjection\Attribute\TaggedIterator;
1920
useSymfony\Component\DependencyInjection\Attribute\TaggedLocator;
2021
useSymfony\Component\DependencyInjection\Attribute\Target;
@@ -271,6 +272,12 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
271272

272273
break;
273274
}
275+
276+
if (InnerService::class ===$attribute->getName()) {
277+
$arguments[$index] =newReference($this->currentId.'.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE);
278+
279+
break;
280+
}
274281
}
275282

276283
if ('' !== ($arguments[$index] ??'')) {

‎src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function __construct()
4343
100 => [
4444
newResolveClassPass(),
4545
newRegisterAutoconfigureAttributesPass(),
46+
newAutowireAsDecoratorPass(),
4647
newAttributeAutoconfigurationPass(),
4748
newResolveInstanceofConditionalsPass(),
4849
newRegisterEnvVarProcessorsPass(),

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
usePsr\Log\LoggerInterface;
1616
usePsr\Log\NullLogger;
1717
useSymfony\Component\Config\FileLocator;
18+
useSymfony\Component\DependencyInjection\Compiler\AutowireAsDecoratorPass;
1819
useSymfony\Component\DependencyInjection\Compiler\AutowirePass;
1920
useSymfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
2021
useSymfony\Component\DependencyInjection\Compiler\DecoratorServicePass;
@@ -1164,4 +1165,25 @@ public function testAutowireAttribute()
11641165
$this->assertSame('@bar',$service->escapedRawValue);
11651166
$this->assertNull($service->invalid);
11661167
}
1168+
1169+
publicfunctiontestAsDecoratorAttribute()
1170+
{
1171+
$container =newContainerBuilder();
1172+
1173+
$container->register(AsDecoratorFoo::class);
1174+
$container->register(AsDecoratorBar10::class)->setAutowired(true)->setArgument(0,'arg1');
1175+
$container->register(AsDecoratorBar20::class)->setAutowired(true)->setArgument(0,'arg1');
1176+
$container->register(AsDecoratorBaz::class)->setAutowired(true);
1177+
1178+
(newResolveClassPass())->process($container);
1179+
(newAutowireAsDecoratorPass())->process($container);
1180+
(newDecoratorServicePass())->process($container);
1181+
(newAutowirePass())->process($container);
1182+
1183+
$this->assertSame(AsDecoratorBar10::class.'.inner', (string)$container->getDefinition(AsDecoratorBar10::class)->getArgument(1));
1184+
1185+
$this->assertSame(AsDecoratorBar20::class.'.inner', (string)$container->getDefinition(AsDecoratorBar20::class)->getArgument(1));
1186+
$this->assertSame(AsDecoratorBaz::class.'.inner', (string)$container->getDefinition(AsDecoratorBaz::class)->getArgument(0));
1187+
$this->assertSame(2,$container->getDefinition(AsDecoratorBaz::class)->getArgument(0)->getInvalidBehavior());
1188+
}
11671189
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespaceSymfony\Component\DependencyInjection\Tests\Compiler;
44

5+
useSymfony\Component\DependencyInjection\Attribute\AsDecorator;
56
useSymfony\Component\DependencyInjection\Attribute\Autowire;
7+
useSymfony\Component\DependencyInjection\Attribute\InnerService;
8+
useSymfony\Component\DependencyInjection\ContainerInterface;
69
useSymfony\Contracts\Service\Attribute\Required;
710

811
class AutowireSetter
@@ -50,3 +53,35 @@ public function __construct(
5053
) {
5154
}
5255
}
56+
57+
interface AsDecoratorInterface
58+
{
59+
}
60+
61+
class AsDecoratorFooimplements AsDecoratorInterface
62+
{
63+
}
64+
65+
#[AsDecorator(decorates: AsDecoratorFoo::class, priority:10)]
66+
class AsDecoratorBar10implements AsDecoratorInterface
67+
{
68+
publicfunction__construct(string$arg1, #[InnerService]AsDecoratorInterface$inner)
69+
{
70+
}
71+
}
72+
73+
#[AsDecorator(decorates: AsDecoratorFoo::class, priority:20)]
74+
class AsDecoratorBar20implements AsDecoratorInterface
75+
{
76+
publicfunction__construct(string$arg1, #[InnerService]AsDecoratorInterface$inner)
77+
{
78+
}
79+
}
80+
81+
#[AsDecorator(decorates: \NonExistent::class, onInvalid: ContainerInterface::NULL_ON_INVALID_REFERENCE)]
82+
class AsDecoratorBazimplements AsDecoratorInterface
83+
{
84+
publicfunction__construct(#[InnerService]AsDecoratorInterface$inner =null)
85+
{
86+
}
87+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp