Skip to content

Commit d768193

Browse files
committed
bug #59979 [Console] Fixed support for Kernel as command (yceruto)
This PR was merged into the 7.3 branch. Discussion ---------- [Console] Fixed support for Kernel as command | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT Currently, registering the Kernel as a command (see the example here: #59340 (comment)) results in an error: ``` Undefined array key "kernel" ``` I added the test case that highlights the issue and the fix (adding the `'container.no_preload'` tag to the invokable service is incorrect, as it is not the command service). Commits ------- c9ef38c Fixed support for Kernel as command
2 parents cf5270c + c9ef38c commit d768193

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Kernel;
13+
14+
use Symfony\Component\Console\Attribute\AsCommand;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
17+
#[AsCommand(name: 'kernel:hello')]
18+
final class KernelCommand extends MinimalKernel
19+
{
20+
public function __invoke(OutputInterface $output): int
21+
{
22+
$output->write('Hello Kernel!');
23+
24+
return 0;
25+
}
26+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Log\NullLogger;
16+
use Symfony\Bundle\FrameworkBundle\Console\Application;
1617
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
18+
use Symfony\Component\Console\Attribute\AsCommand;
19+
use Symfony\Component\Console\Input\ArrayInput;
20+
use Symfony\Component\Console\Output\BufferedOutput;
1721
use Symfony\Component\DependencyInjection\ContainerBuilder;
1822
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
1923
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
@@ -152,6 +156,23 @@ public function testSimpleKernel()
152156
$this->assertSame('Hello World!', $response->getContent());
153157
}
154158

159+
public function testKernelCommand()
160+
{
161+
if (!property_exists(AsCommand::class, 'help')) {
162+
$this->markTestSkipped('Invokable command no available.');
163+
}
164+
165+
$kernel = $this->kernel = new KernelCommand('kernel_command');
166+
$application = new Application($kernel);
167+
168+
$input = new ArrayInput(['command' => 'kernel:hello']);
169+
$output = new BufferedOutput();
170+
171+
$this->assertTrue($application->has('kernel:hello'));
172+
$this->assertSame(0, $application->doRun($input, $output));
173+
$this->assertSame('Hello Kernel!', $output->fetch());
174+
}
175+
155176
public function testDefaultKernel()
156177
{
157178
$kernel = $this->kernel = new DefaultKernel('test', false);

src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public function process(ContainerBuilder $container): void
3939

4040
foreach ($commandServices as $id => $tags) {
4141
$definition = $container->getDefinition($id);
42-
$definition->addTag('container.no_preload');
4342
$class = $container->getParameterBag()->resolveValue($definition->getClass());
4443

4544
if (!$r = $container->getReflectionClass($class)) {
@@ -58,6 +57,8 @@ public function process(ContainerBuilder $container): void
5857
$invokableRef = null;
5958
}
6059

60+
$definition->addTag('container.no_preload');
61+
6162
/** @var AsCommand|null $attribute */
6263
$attribute = ($r->getAttributes(AsCommand::class)[0] ?? null)?->newInstance();
6364

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy