From 92889931fb41b5d7b4814c01f8f8d5794c48465a Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sun, 16 Mar 2025 15:56:09 +0100 Subject: [PATCH] [FrameworkBundle] Auto-exclude DI extensions, test cases, entities and messenger messages --- .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../FrameworkExtension.php | 29 +++++++++++++++++-- .../Kernel/MicroKernelTrait.php | 3 +- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 2337f5ae0bd87..9d3b3eae45fc5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -14,6 +14,7 @@ CHANGELOG * Add support for signal plain name in the `messenger.stop_worker_on_signals` configuration * Deprecate the `framework.validation.cache` option * Add `--method` option to the `debug:router` command + * Auto-exclude DI extensions, test cases, entities and messenger messages 7.2 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index e2d0888dbd7e6..9ee0f0f43441a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -12,12 +12,16 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection; use Composer\InstalledVersions; +use Doctrine\ORM\Mapping\Embeddable; +use Doctrine\ORM\Mapping\Entity; +use Doctrine\ORM\Mapping\MappedSuperclass; use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; use phpDocumentor\Reflection\DocBlockFactoryInterface; use phpDocumentor\Reflection\Types\ContextFactory; use PhpParser\Parser; use PHPStan\PhpDocParser\Parser\PhpDocParser; +use PHPUnit\Framework\TestCase; use Psr\Cache\CacheItemPoolInterface; use Psr\Clock\ClockInterface as PsrClockInterface; use Psr\Container\ContainerInterface as PsrContainerInterface; @@ -57,6 +61,7 @@ use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; use Symfony\Component\DependencyInjection\ChildDefinition; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -117,6 +122,7 @@ use Symfony\Component\Mailer\EventListener\SmimeSignedMessageListener; use Symfony\Component\Mailer\Mailer; use Symfony\Component\Mercure\HubRegistry; +use Symfony\Component\Messenger\Attribute\AsMessage; use Symfony\Component\Messenger\Attribute\AsMessageHandler; use Symfony\Component\Messenger\Bridge as MessengerBridge; use Symfony\Component\Messenger\Handler\BatchHandlerInterface; @@ -757,12 +763,29 @@ static function (ChildDefinition $definition, AsPeriodicTask|AsCronTask $attribu } ); } - $container->registerAttributeForAutoconfiguration(JsonStreamable::class, static function (ChildDefinition $definition, JsonStreamable $attribute): void { - $definition->addTag('json_streamer.streamable', [ + + $container->registerForAutoconfiguration(CompilerPassInterface::class) + ->addExcludeTag('container.excluded.compiler_pass'); + $container->registerForAutoconfiguration(TestCase::class) + ->addExcludeTag('container.excluded.test_case'); + $container->registerAttributeForAutoconfiguration(AsMessage::class, static function (ChildDefinition $definition) { + $definition->addExcludeTag('container.excluded.messenger.message'); + }); + $container->registerAttributeForAutoconfiguration(Entity::class, static function (ChildDefinition $definition) { + $definition->addExcludeTag('container.excluded.doctrine.entity'); + }); + $container->registerAttributeForAutoconfiguration(Embeddable::class, static function (ChildDefinition $definition) { + $definition->addExcludeTag('container.excluded.doctrine.embeddable'); + }); + $container->registerAttributeForAutoconfiguration(MappedSuperclass::class, static function (ChildDefinition $definition) { + $definition->addExcludeTag('container.excluded.doctrine.mapped_superclass'); + }); + + $container->registerAttributeForAutoconfiguration(JsonStreamable::class, static function (ChildDefinition $definition, JsonStreamable $attribute) { + $definition->addExcludeTag('json_streamer.streamable', [ 'object' => $attribute->asObject, 'list' => $attribute->asList, ]); - $definition->addTag('container.excluded'); }); if (!$container->getParameter('kernel.debug')) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php b/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php index f40373a302b45..28d616c13e1c1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php +++ b/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php @@ -165,6 +165,7 @@ public function registerContainerConfiguration(LoaderInterface $loader): void ->setPublic(true) ; } + $container->setAlias($kernelClass, 'kernel')->setPublic(true); $kernelDefinition = $container->getDefinition('kernel'); $kernelDefinition->addTag('routing.route_loader'); @@ -197,8 +198,6 @@ public function registerContainerConfiguration(LoaderInterface $loader): void $kernelLoader->registerAliasesForSinglyImplementedInterfaces(); AbstractConfigurator::$valuePreProcessor = $valuePreProcessor; } - - $container->setAlias($kernelClass, 'kernel')->setPublic(true); }); }