8000 [Console] add `RunCommandMessage` and `RunCommandMessageHandler` · symfony/symfony@b1384cc · GitHub
[go: up one dir, main page]

Skip to content

Commit b1384cc

Browse files
committed
[Console] add RunCommandMessage and RunCommandMessageHandler
1 parent 510e51f commit b1384cc

File tree

7 files changed

+172
-0
lines changed

7 files changed

+172
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use Symfony\Component\Config\ResourceCheckerInterface;
5454
use Symfony\Component\Console\Application;
5555
use Symfony\Component\Console\Command\Command;
56+
use Symfony\Component\Console\Messenger\RunCommandMessageHandler;
5657
use Symfony\Component\DependencyInjection\Alias;
5758
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
5859
use Symfony\Component\DependencyInjection\ChildDefinition;
@@ -253,6 +254,11 @@ public function load(array $configs, ContainerBuilder $container)
253254
if (!class_exists(DebugCommand::class)) {
254255
$container->removeDefinition('console.command.dotenv_debug');
255256
}
257+
258+
if (!class_exists(RunCommandMessageHandler::class)) {
259+
$container->removeDefinition('console.messenger.application');
260+
$container->removeDefinition('console.messenger.execute_command_handler');
261+
}
256262
}
257263

258264
// Load Cache configuration first as it is used by other components

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
use Symfony\Bundle\FrameworkBundle\Command\TranslationUpdateCommand;
3939
use Symfony\Bundle\FrameworkBundle\Command\WorkflowDumpCommand;
4040
use Symfony\Bundle\FrameworkBundle\Command\YamlLintCommand;
41+
use Symfony\Bundle\FrameworkBundle\Console\Application;
4142
use Symfony\Bundle\FrameworkBundle\EventListener\SuggestMissingPackageSubscriber;
4243
use Symfony\Component\Console\EventListener\ErrorListener;
44+
use Symfony\Component\Console\Messenger\RunCommandMessageHandler;
4345
use Symfony\Component\Dotenv\Command\DebugCommand as DotenvDebugCommand;
4446
use Symfony\Component\Messenger\Command\ConsumeMessagesCommand;
4547
use Symfony\Component\Messenger\Command\DebugCommand as MessengerDebugCommand;
@@ -364,5 +366,18 @@
364366
service('secrets.local_vault')->ignoreOnInvalid(),
365367
])
366368
->tag('console.command')
369+
370+
->set('console.messenger.application', Application::class)
371+
->share(false)
372+
->call('setAutoExit', [false])
373+
->args([
374+
service('kernel'),
375+
])
376+
377+
->set('console.messenger.execute_command_handler', RunCommandMessageHandler::class)
378+
->args([
379+
service('console.messenger.application'),
380+
])
381+
->tag('messenger.message_handler')
367382
;
368383
};

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add `SignalMap` to map signal value to its name
88
* Multi-line text in vertical tables is aligned properly
99
* The application can also catch errors with `Application::setCatchErrors(true)`
10+
* Add `RunCommandMessage` and `RunCommandMessageHandler`
1011

1112
6.3
1213
---
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\Component\Console\Messenger;
13+
14+
/**
15+
* @author Kevin Bond <kevinbond@gmail.com>
16+
*/
17+
final class RunCommandMessage implements \Stringable
18+
{
19+
public function __construct(
20+
public readonly string $input,
21+
public readonly bool $catchExceptions = false,
22+
) {
23+
}
24+
25+
public function __toString(): string
26+
{
27+
return $this->input;
28+
}
29+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\Component\Console\Messenger;
13+
14+
use Symfony\Component\Console\Application;
15+
use Symfony\Component\Console\Input\StringInput;
16+
use Symfony\Component\Console\Output\BufferedOutput;
17+
18+
/**
19+
* @author Kevin Bond <kevinbond@gmail.com>
20+
*/
21+
final class RunCommandMessageHandler
22+
{
23+
public function __construct(private readonly Application $application)
24+
{
25+
}
26+
27+
public function __invoke(RunCommandMessage $message): BufferedOutput
28+
{
29+
$input = new StringInput($message->input);
30+
$output = new BufferedOutput();
31+
32+
$this->application->setCatchExceptions($message->catchExceptions);
33+
$this->application->run($input, $output);
34+
35+
return $output;
36+
}
37+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\Component\Console\Tests\Messenger;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Messenger\RunCommandMessage;
19+
use Symfony\Component\Console\Messenger\RunCommandMessageHandler;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
22+
/**
23+
* @author Kevin Bond <kevinbond@gmail.com>
24+
*/
25+
final class RunCommandMessageHandlerTest extends TestCase
26+
{
27+
public function testExecutesCommand()
28+
{
29+
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());
30+
$output = $handler(new RunCommandMessage('test:command'))->fetch();
31+
32+
$this->assertStringContainsString('some message', $output);
33+
}
34+
35+
public function testExecutesCommandThatThrowsException()
36+
{
37+
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());
38+
39+
$this->expectException(\RuntimeException::class);
40+
$this->expectExceptionMessage('exception message');
41+
42+
$handler(new RunCommandMessage('test:command --throw'));
43+
}
44+
45+
public function testExecutesCommandThatCatchesThrownException()
46+
{
47+
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());
48+
$output = $handler(new RunCommandMessage('test:command --throw -v', catchExceptions: true))->fetch();
49+
50+
$this->assertStringContainsString('[RuntimeException]', $output);
51+
$this->assertStringContainsString('exception message', $output);
52+
}
53+
54+
private function createApplicationWithCommand(): Application
55+
{
56+
$application = new Application();
57+
$application->setAutoExit(false);
58+
$application->addCommands([
59+
new class() extends Command {
60+
public function configure(): void
61+
{
62+
$this
63+
->setName('test:command')
64+
->addOption('throw')
65+
;
66+
}
67+
68+
protected function execute(InputInterface $input, OutputInterface $output): int
69+
{
70+
if ($input->getOption('throw')) {
71+
throw new \RuntimeException('exception message');
72+
}
73+
74+
$output->write('some message');
75+
76+
return Command::SUCCESS;
77+
}
78+
},
79+
]);
80+
81+
return $application;
82+
}
83+
}

src/Symfony/Component/Console/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"symfony/event-dispatcher": "^5.4|^6.0|^7.0",
2828
"symfony/dependency-injection": "^5.4|^6.0|^7.0",
2929
"symfony/lock": "^5.4|^6.0|^7.0",
30+
"symfony/messenger": "^5.4|^6.0|^7.0",
3031
"symfony/process": "^5.4|^6.0|^7.0",
3132
"symfony/var-dumper": "^5.4|^6.0|^7.0",
3233
"psr/log": "^1|^2|^3"

0 commit comments

Comments
 (0)
0