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

Skip to content

Commit 09bc95a

Browse files
[DependencyInjection] Preserve the id of decorated services
1 parent ae390e7 commit 09bc95a

8 files changed

+67
-40
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
*/
2828
class DecoratorServicePass extends AbstractRecursivePass
2929
{
30+
/**
31+
* @var array<string, string>
32+
*/
33+
public array $decoratedServices = [];
34+
3035
/**
3136
* @return void
3237
*/
@@ -42,6 +47,7 @@ public function process(ContainerBuilder $container)
4247
$definitions->insert([$id, $definition], [$decorated[2], --$order]);
4348
}
4449
$decoratingDefinitions = [];
50+
$this->decoratedServices = [];
4551

4652
$tagsToKeep = $container->hasParameter('container.behavior_describing_tags')
4753
? $container->getParameter('container.behavior_describing_tags')
@@ -111,9 +117,18 @@ public function process(ContainerBuilder $container)
111117
}
112118

113119
$container->setAlias($inner, $id)->setPublic($public);
120+
$this->decoratedServices[$inner] = $id;
114121
}
115122
}
116123

124+
/**
125+
* @return array<string, string>
126+
*/
127+
public function getDecoratedServices(): array
128+
{
129+
return $this->decoratedServices;
130+
}
131+
117132
protected function processValue(mixed $value, bool $isRoot = false): mixed
118133
{
119134
if ($value instanceof Reference && '.inner' === (string) $value) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ public function __construct()
6464
new AutowireRequiredPropertiesPass(),
6565
new ResolveBindingsPass(),
6666
new ServiceLocatorTagPass(),
67-
new DecoratorServicePass(),
67+
$decoratorPass = new DecoratorServicePass(),
6868
new CheckDefinitionValidityPass(),
6969
new AutowirePass(false),
7070
new ServiceLocatorTagPass(),
7171
new ResolveTaggedIteratorArgumentPass(),
7272
new ResolveServiceSubscribersPass(),
73-
new ResolveReferencesToAliasesPass(),
73+
new ResolveReferencesToAliasesPass($decoratorPass),
7474
new ResolveInvalidReferencesPass(),
7575
new AnalyzeServiceReferencesPass(true),
7676
new CheckCircularReferencesPass(),

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,23 @@
2222
*/
2323
class ResolveReferencesToAliasesPass extends AbstractRecursivePass
2424
{
25+
public function __construct(
26+
private ?DecoratorServicePass $decoratorPass = null,
27+
) {
28+
}
29+
2530
/**
2631
* @return void
2732
*/
2833
public function process(ContainerBuilder $container)
2934
{
35+
foreach ($this->decoratorPass?->getDecoratedServices() ?? [] as $inner => $id) {
36+
$definition = $container->getDefinition($id);
37+
$alias = $container->getAlias($inner);
38+
$container->setAlias($id, $inner)->setPublic($definition->isPublic());
39+
$container->setDefinition($inner, $definition)->setPublic($alias->isPublic());
40+
}
41+
3042
parent::process($container);
3143

3244
foreach ($container->getAliases() as $id => $alias) {

src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/child.expected.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ services:
44
class: Symfony\Component\DependencyInjection\ContainerInterface
55
public: true
66
synthetic: true
7-
foo:
7+
bar:
88
class: Class2
99
public: true
1010
file: file.php
1111
lazy: true
1212
arguments: [!service { class: Class1 }]
13-
bar:
14-
alias: foo
13+
foo:
14+
alias: bar
1515
public: true

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,33 +165,33 @@ class getConfiguredServiceSimpleService extends ProjectServiceContainer
165165
}
166166
}
167167

168-
[Container%s/getDecoratorServiceService.php] => <?php
168+
[Container%s/getDecoratedService.php] => <?php
169169
%A
170-
class getDecoratorServiceService extends ProjectServiceContainer
170+
class getDecoratedService extends ProjectServiceContainer
171171
{
172172
/**
173-
* Gets the public 'decorator_service' shared service.
173+
* Gets the public 'decorated' shared service.
174174
*
175175
* @return \stdClass
176176
*/
177177
public static function do($container, $lazyLoad = true)
178178
{
179-
return $container->services['decorator_service'] = new \stdClass();
179+
return $container->services['decorated'] = new \stdClass();
180180
}
181181
}
182182

183-
[Container%s/getDecoratorServiceWithNameService.php] => <?php
183+
[Container%s/getDecoratorServiceService.php] => <?php
184184
%A
185-
class getDecoratorServiceWithNameService extends ProjectServiceContainer
185+
class getDecoratorServiceService extends ProjectServiceContainer
186186
{
187187
/**
188-
* Gets the public 'decorator_service_with_name' shared service.
188+
* Gets the public 'decorator_service' shared service.
189189
*
190190
* @return \stdClass
191191
*/
192192
public static function do($container, $lazyLoad = true)
193193
{
194-
return $container->services['decorator_service_with_name'] = new \stdClass();
194+
return $container->services['decorator_service'] = new \stdClass();
195195
}
196196
}
197197

@@ -585,8 +585,8 @@ class ProjectServiceContainer extends Container
585585
'baz' => 'getBazService',
586586
'configured_service' => 'getConfiguredServiceService',
587587
'configured_service_simple' => 'getConfiguredServiceSimpleService',
588+
'decorated' => 'getDecoratedService',
588589
'decorator_service' => 'getDecoratorServiceService',
589-
'decorator_service_with_name' => 'getDecoratorServiceWithNameService',
590590
'deprecated_service' => 'getDeprecatedServiceService',
591591
'factory_service' => 'getFactoryServiceService',
592592
'factory_service_simple' => 'getFactoryServiceSimpleService',
@@ -608,7 +608,7 @@ class ProjectServiceContainer extends Container
608608
$this->aliases = [
609609
'alias_for_alias' => 'foo',
610610
'alias_for_foo' => 'foo',
611-
'decorated' => 'decorator_service_with_name',
611+
'decorator_service_with_name' => 'decorated',
612612
];
613613
}
614614

@@ -753,8 +753,8 @@ require __DIR__.'/Container%s/getFoo_BazService.php';
753753
require __DIR__.'/Container%s/getFooService.php';
754754
require __DIR__.'/Container%s/getFactoryServiceSimpleService.php';
755755
require __DIR__.'/Container%s/getFactoryServiceService.php';
756-
require __DIR__.'/Container%s/getDecoratorServiceWithNameService.php';
757756
require __DIR__.'/Container%s/getDecoratorServiceService.php';
757+
require __DIR__.'/Container%s/getDecoratedService.php';
758758
require __DIR__.'/Container%s/getConfiguredServiceSimpleService.php';
759759
require __DIR__.'/Container%s/getConfiguredServiceService.php';
760760
require __DIR__.'/Container%s/getBazService.php';

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public function __construct()
3636
'baz' => 'getBazService',
3737
'configured_service' => 'getConfiguredServiceService',
3838
'configured_service_simple' => 'getConfiguredServiceSimpleService',
39+
'decorated' => 'getDecoratedService',
3940
'decorator_service' => 'getDecoratorServiceService',
40-
'decorator_service_with_name' => 'getDecoratorServiceWithNameService',
4141
'deprecated_service' => 'getDeprecatedServiceService',
4242
'factory_service' => 'getFactoryServiceService',
4343
'factory_service_simple' => 'getFactoryServiceSimpleService',
@@ -57,7 +57,7 @@ public function __construct()
5757
$this->aliases = [
5858
'alias_for_alias' => 'foo',
5959
'alias_for_foo' => 'foo',
60-
'decorated' => 'decorator_service_with_name',
60+
'decorator_service_with_name' => 'decorated',
6161
];
6262
}
6363

@@ -203,23 +203,23 @@ protected static function getConfiguredServiceSimpleService($container)
203203
}
204204

205205
/**
206-
* Gets the public 'decorator_service' shared service.
206+
* Gets the public 'decorated' shared service.
207207
*
208208
* @return \stdClass
209209
*/
210-
protected static function getDecoratorServiceService($container)
210+
protected static function getDecoratedService($container)
211211
{
212-
return $container->services['decorator_service'] = new \stdClass();
212+
return $container->services['decorated'] = new \stdClass();
213213
}
214214

215215
/**
216-
* Gets the public 'decorator_service_with_name' shared service.
216+
* Gets the public 'decorator_service' shared service.
217217
*
218218
* @return \stdClass
219219
*/
220-
protected static function getDecoratorServiceWithNameService($container)
220+
protected static function getDecoratorServiceService($container)
221221
{
222-
return $container->services['decorator_service_with_name'] = new \stdClass();
222+
return $container->services['decorator_service'] = new \stdClass();
223223
}
224224

225225
/**

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class ProjectServiceContainer extends Container
6060
'baz' => 'getBazService',
6161
'configured_service' => 'getConfiguredServiceService',
6262
'configured_service_simple' => 'getConfiguredServiceSimpleService',
63+
'decorated' => 'getDecoratedService',
6364
'decorator_service' => 'getDecoratorServiceService',
64-
'decorator_service_with_name' => 'getDecoratorServiceWithNameService',
6565
'deprecated_service' => 'getDeprecatedServiceService',
6666
'factory_service' => 'getFactoryServiceService',
6767
'factory_service_simple' => 'getFactoryServiceSimpleService',
@@ -83,7 +83,7 @@ class ProjectServiceContainer extends Container
8383
$this->aliases = [
8484
'alias_for_alias' => 'foo',
8585
'alias_for_foo' => 'foo',
86-
'decorated' => 'decorator_service_with_name',
86+
'decorator_service_with_name' => 'decorated',
8787
];
8888

8989
$this->privates['service_container'] = static function ($container) {
@@ -222,23 +222,23 @@ class ProjectServiceContainer extends Container
222222
}
223223

224224
/**
225-
* Gets the public 'decorator_service' shared service.
225+
* Gets the public 'decorated' shared service.
226226
*
227227
* @return \stdClass
228228
*/
229-
protected static function getDecoratorServiceService($container)
229+
protected static function getDecoratedService($container)
230230
{
231-
return $container->services['decorator_service'] = new \stdClass();
231+
return $container->services['decorated'] = new \stdClass();
232232
}
233233

234234
/**
235-
* Gets the public 'decorator_service_with_name' shared service.
235+
* Gets the public 'decorator_service' shared service.
236236
*
237237
* @return \stdClass
238238
*/
239-
protected static function getDecoratorServiceWithNameService($container)
239+
protected static function getDecoratorServiceService($container)
240240
{
241-
return $container->services['decorator_service_with_name'] = new \stdClass();
241+
return $container->services['decorator_service'] = new \stdClass();
242242
}
243243

244244
/**

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public function __construct()
3636
'baz' => 'getBazService',
3737
'configured_service' => 'getConfiguredServiceService',
3838
'configured_service_simple' => 'getConfiguredServiceSimpleService',
39+
'decorated' => 'getDecoratedService',
3940
'decorator_service' => 'getDecoratorServiceService',
40-
'decorator_service_with_name' => 'getDecoratorServiceWithNameService',
4141
'deprecated_service' => 'getDeprecatedServiceService',
4242
'factory_service' => 'getFactoryServiceService',
4343
'factory_service_simple' => 'getFactoryServiceSimpleService',
@@ -57,7 +57,7 @@ public function __construct()
5757
$this->aliases = [
5858
'alias_for_alias' => 'foo',
5959
'alias_for_foo' => 'foo',
60-
'decorated' => 'decorator_service_with_name',
60+
'decorator_service_with_name' => 'decorated',
6161
];
6262
}
6363

@@ -203,23 +203,23 @@ protected static function getConfiguredServiceSimpleService($container)
203203
}
204204

205205
/**
206-
* Gets the public 'decorator_service' shared service.
206+
* Gets the public 'decorated' shared service.
207207
*
208208
* @return \stdClass
209209
*/
210-
protected static function getDecoratorServiceService($container)
210+
protected static function getDecoratedService($container)
211211
{
212-
return $container->services['decorator_service'] = new \stdClass();
212+
return $container->services['decorated'] = new \stdClass();
213213
}
214214

215215
/**
216-
* Gets the public 'decorator_service_with_name' shared service.
216+
* Gets the public 'decorator_service' shared service.
217217
*
218218
* @return \stdClass
219219
*/
220-
protected static function getDecoratorServiceWithNameService($container)
220+
protected static function getDecoratorServiceService($container)
221221
{
222-
return $container->services['decorator_service_with_name'] = new \stdClass();
222+
return $container->services['decorator_service'] = new \stdClass();
223223
}
224224

225225
/**

0 commit comments

Comments
 (0)