diff --git a/src/Symfony/Component/DependencyInjection/Compiler/DecoratorServicePass.php b/src/Symfony/Component/DependencyInjection/Compiler/DecoratorServicePass.php index 9c1d7e218e2fc..a457a224a5eb4 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/DecoratorServicePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/DecoratorServicePass.php @@ -27,6 +27,11 @@ */ class DecoratorServicePass extends AbstractRecursivePass { + /** + * @var array + */ + public array $decoratedServices = []; + /** * @return void */ @@ -42,6 +47,7 @@ public function process(ContainerBuilder $container) $definitions->insert([$id, $definition], [$decorated[2], --$order]); } $decoratingDefinitions = []; + $this->decoratedServices = []; $tagsToKeep = $container->hasParameter('container.behavior_describing_tags') ? $container->getParameter('container.behavior_describing_tags') @@ -111,9 +117,18 @@ public function process(ContainerBuilder $container) } $container->setAlias($inner, $id)->setPublic($public); + $this->decoratedServices[$inner] = $id; } } + /** + * @return array + */ + public function getDecoratedServices(): array + { + return $this->decoratedServices; + } + protected function processValue(mixed $value, bool $isRoot = false): mixed { if ($value instanceof Reference && '.inner' === (string) $value) { diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php b/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php index 16b24cc7106dd..cc7c9db2f97a4 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php @@ -64,13 +64,13 @@ public function __construct() new AutowireRequiredPropertiesPass(), new ResolveBindingsPass(), new ServiceLocatorTagPass(), - new DecoratorServicePass(), + $decoratorPass = new DecoratorServicePass(), new CheckDefinitionValidityPass(), new AutowirePass(false), new ServiceLocatorTagPass(), new ResolveTaggedIteratorArgumentPass(), new ResolveServiceSubscribersPass(), - new ResolveReferencesToAliasesPass(), + new ResolveReferencesToAliasesPass($decoratorPass), new ResolveInvalidReferencesPass(), new AnalyzeServiceReferencesPass(true), new CheckCircularReferencesPass(), diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php index 3176d405f8eff..cc98e6d463ab8 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php @@ -22,11 +22,23 @@ */ class ResolveReferencesToAliasesPass extends AbstractRecursivePass { + public function __construct( + private ?DecoratorServicePass $decoratorPass = null, + ) { + } + /** * @return void */ public function process(ContainerBuilder $container) { + foreach ($this->decoratorPass?->getDecoratedServices() ?? [] as $inner => $id) { + $definition = $container->getDefinition($id); + $alias = $container->getAlias($inner); + $container->setAlias($id, $inner)->setPublic($definition->isPublic()); + $container->setDefinition($inner, $definition)->setPublic($alias->isPublic()); + } + parent::process($container); foreach ($container->getAliases() as $id => $alias) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/child.expected.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/child.expected.yml index d9537a05e4c34..76edfd4ca317b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/child.expected.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/child.expected.yml @@ -4,12 +4,12 @@ services: class: Symfony\Component\DependencyInjection\ContainerInterface public: true synthetic: true - foo: + bar: class: Class2 public: true file: file.php lazy: true arguments: [!service { class: Class1 }] - bar: - alias: foo + foo: + alias: bar public: true diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt index 1897adcc09e47..2a5d64db3fd0f 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_as_files.txt @@ -165,33 +165,33 @@ class getConfiguredServiceSimpleService extends ProjectServiceContainer } } - [Container%s/getDecoratorServiceService.php] => services['decorator_service'] = new \stdClass(); + return $container->services['decorated'] = new \stdClass(); } } - [Container%s/getDecoratorServiceWithNameService.php] => services['decorator_service_with_name'] = new \stdClass(); + return $container->services['decorator_service'] = new \stdClass(); } } @@ -585,8 +585,8 @@ class ProjectServiceContainer extends Container 'baz' => 'getBazService', 'configured_service' => 'getConfiguredServiceService', 'configured_service_simple' => 'getConfiguredServiceSimpleService', + 'decorated' => 'getDecoratedService', 'decorator_service' => 'getDecoratorServiceService', - 'decorator_service_with_name' => 'getDecoratorServiceWithNameService', 'deprecated_service' => 'getDeprecatedServiceService', 'factory_service' => 'getFactoryServiceService', 'factory_service_simple' => 'getFactoryServiceSimpleService', @@ -608,7 +608,7 @@ class ProjectServiceContainer extends Container $this->aliases = [ 'alias_for_alias' => 'foo', 'alias_for_foo' => 'foo', - 'decorated' => 'decorator_service_with_name', + 'decorator_service_with_name' => 'decorated', ]; } @@ -753,8 +753,8 @@ require __DIR__.'/Container%s/getFoo_BazService.php'; require __DIR__.'/Container%s/getFooService.php'; require __DIR__.'/Container%s/getFactoryServiceSimpleService.php'; require __DIR__.'/Container%s/getFactoryServiceService.php'; -require __DIR__.'/Container%s/getDecoratorServiceWithNameService.php'; require __DIR__.'/Container%s/getDecoratorServiceService.php'; +require __DIR__.'/Container%s/getDecoratedService.php'; require __DIR__.'/Container%s/getConfiguredServiceSimpleService.php'; require __DIR__.'/Container%s/getConfiguredServiceService.php'; require __DIR__.'/Container%s/getBazService.php'; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index 765cc4a54b587..18d2c69ba225d 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -36,8 +36,8 @@ public function __construct() 'baz' => 'getBazService', 'configured_service' => 'getConfiguredServiceService', 'configured_service_simple' => 'getConfiguredServiceSimpleService', + 'decorated' => 'getDecoratedService', 'decorator_service' => 'getDecoratorServiceService', - 'decorator_service_with_name' => 'getDecoratorServiceWithNameService', 'deprecated_service' => 'getDeprecatedServiceService', 'factory_service' => 'getFactoryServiceService', 'factory_service_simple' => 'getFactoryServiceSimpleService', @@ -57,7 +57,7 @@ public function __construct() $this->aliases = [ 'alias_for_alias' => 'foo', 'alias_for_foo' => 'foo', - 'decorated' => 'decorator_service_with_name', + 'decorator_service_with_name' => 'decorated', ]; } @@ -203,23 +203,23 @@ protected static function getConfiguredServiceSimpleService($container) } /** - * Gets the public 'decorator_service' shared service. + * Gets the public 'decorated' shared service. * * @return \stdClass */ - protected static function getDecoratorServiceService($container) + protected static function getDecoratedService($container) { - return $container->services['decorator_service'] = new \stdClass(); + return $container->services['decorated'] = new \stdClass(); } /** - * Gets the public 'decorator_service_with_name' shared service. + * Gets the public 'decorator_service' shared service. * * @return \stdClass */ - protected static function getDecoratorServiceWithNameService($container) + protected static function getDecoratorServiceService($container) { - return $container->services['decorator_service_with_name'] = new \stdClass(); + return $container->services['decorator_service'] = new \stdClass(); } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt index c83530a598fe4..2899ac246b680 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_inlined_factories.txt @@ -60,8 +60,8 @@ class ProjectServiceContainer extends Container 'baz' => 'getBazService', 'configured_service' => 'getConfiguredServiceService', 'configured_service_simple' => 'getConfiguredServiceSimpleService', + 'decorated' => 'getDecoratedService', 'decorator_service' => 'getDecoratorServiceService', - 'decorator_service_with_name' => 'getDecoratorServiceWithNameService', 'deprecated_service' => 'getDeprecatedServiceService', 'factory_service' => 'getFactoryServiceService', 'factory_service_simple' => 'getFactoryServiceSimpleService', @@ -83,7 +83,7 @@ class ProjectServiceContainer extends Container $this->aliases = [ 'alias_for_alias' => 'foo', 'alias_for_foo' => 'foo', - 'decorated' => 'decorator_service_with_name', + 'decorator_service_with_name' => 'decorated', ]; $this->privates['service_container'] = static function ($container) { @@ -222,23 +222,23 @@ class ProjectServiceContainer extends Container } /** - * Gets the public 'decorator_service' shared service. + * Gets the public 'decorated' shared service. * * @return \stdClass */ - protected static function getDecoratorServiceService($container) + protected static function getDecoratedService($container) { - return $container->services['decorator_service'] = new \stdClass(); + return $container->services['decorated'] = new \stdClass(); } /** - * Gets the public 'decorator_service_with_name' shared service. + * Gets the public 'decorator_service' shared service. * * @return \stdClass */ - protected static function getDecoratorServiceWithNameService($container) + protected static function getDecoratorServiceService($container) { - return $container->services['decorator_service_with_name'] = new \stdClass(); + return $container->services['decorator_service'] = new \stdClass(); } /** diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php index b6595bb34a7b7..4a4c980c34c32 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_errored_definition.php @@ -36,8 +36,8 @@ public function __construct() 'baz' => 'getBazService', 'configured_service' => 'getConfiguredServiceService', 'configured_service_simple' => 'getConfiguredServiceSimpleService', + 'decorated' => 'getDecoratedService', 'decorator_service' => 'getDecoratorServiceService', - 'decorator_service_with_name' => 'getDecoratorServiceWithNameService', 'deprecated_service' => 'getDeprecatedServiceService', 'factory_service' => 'getFactoryServiceService', 'factory_service_simple' => 'getFactoryServiceSimpleService', @@ -57,7 +57,7 @@ public function __construct() $this->aliases = [ 'alias_for_alias' => 'foo', 'alias_for_foo' => 'foo', - 'decorated' => 'decorator_service_with_name', + 'decorator_service_with_name' => 'decorated', ]; } @@ -203,23 +203,23 @@ protected static function getConfiguredServiceSimpleService($container) } /** - * Gets the public 'decorator_service' shared service. + * Gets the public 'decorated' shared service. * * @return \stdClass */ - protected static function getDecoratorServiceService($container) + protected static function getDecoratedService($container) { - return $container->services['decorator_service'] = new \stdClass(); + return $container->services['decorated'] = new \stdClass(); } /** - * Gets the public 'decorator_service_with_name' shared service. + * Gets the public 'decorator_service' shared service. * * @return \stdClass */ - protected static function getDecoratorServiceWithNameService($container) + protected static function getDecoratorServiceService($container) { - return $container->services['decorator_service_with_name'] = new \stdClass(); + return $container->services['decorator_service'] = new \stdClass(); } /**