From c9ef38cf64630768568e9edd0f9cfd86cf2242c9 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Fri, 14 Mar 2025 19:20:21 -0400 Subject: [PATCH] Fixed support for Kernel as command --- .../Tests/Kernel/KernelCommand.php | 26 +++++++++++++++++++ .../Tests/Kernel/MicroKernelTraitTest.php | 21 +++++++++++++++ .../AddConsoleCommandPass.php | 3 ++- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/KernelCommand.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/KernelCommand.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/KernelCommand.php new file mode 100644 index 0000000000000..4c9a5d85adcc2 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/KernelCommand.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Kernel; + +use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Output\OutputInterface; + +#[AsCommand(name: 'kernel:hello')] +final class KernelCommand extends MinimalKernel +{ + public function __invoke(OutputInterface $output): int + { + $output->write('Hello Kernel!'); + + return 0; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php index a9d2ae7209efe..5c7161124bda5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php @@ -13,7 +13,11 @@ use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; +use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; +use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; use Symfony\Component\DependencyInjection\Loader\ClosureLoader; @@ -152,6 +156,23 @@ public function testSimpleKernel() $this->assertSame('Hello World!', $response->getContent()); } + public function testKernelCommand() + { + if (!property_exists(AsCommand::class, 'help')) { + $this->markTestSkipped('Invokable command no available.'); + } + + $kernel = $this->kernel = new KernelCommand('kernel_command'); + $application = new Application($kernel); + + $input = new ArrayInput(['command' => 'kernel:hello']); + $output = new BufferedOutput(); + + $this->assertTrue($application->has('kernel:hello')); + $this->assertSame(0, $application->doRun($input, $output)); + $this->assertSame('Hello Kernel!', $output->fetch()); + } + public function testDefaultKernel() { $kernel = $this->kernel = new DefaultKernel('test', false); diff --git a/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php b/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php index a90fb8f04606e..562627f4b6114 100644 --- a/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php +++ b/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php @@ -39,7 +39,6 @@ public function process(ContainerBuilder $container): void foreach ($commandServices as $id => $tags) { $definition = $container->getDefinition($id); - $definition->addTag('container.no_preload'); $class = $container->getParameterBag()->resolveValue($definition->getClass()); if (!$r = $container->getReflectionClass($class)) { @@ -58,6 +57,8 @@ public function process(ContainerBuilder $container): void $invokableRef = null; } + $definition->addTag('container.no_preload'); + /** @var AsCommand|null $attribute */ $attribute = ($r->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();