Skip to content

[Console] Fixed support for Kernel as command #59979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/KernelCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -58,6 +57,8 @@ public function process(ContainerBuilder $container): void
$invokableRef = null;
}

$definition->addTag('container.no_preload');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See line 54, where the $definition variable is redefined as the new command service


/** @var AsCommand|null $attribute */
$attribute = ($r->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();

Expand Down
Loading