Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f712569

Browse filesBrowse files
committed
[Console] add ExecuteCommand and ExecuteCommandHandler
1 parent 5e6ea11 commit f712569
Copy full SHA for f712569

File tree

7 files changed

+172
-0
lines changed
Filter options

7 files changed

+172
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use Symfony\Component\Config\ResourceCheckerInterface;
4949
use Symfony\Component\Console\Application;
5050
use Symfony\Component\Console\Command\Command;
51+
use Symfony\Component\Console\Messenger\ExecuteCommandHandler;
5152
use Symfony\Component\DependencyInjection\Alias;
5253
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
5354
use Symfony\Component\DependencyInjection\ChildDefinition;
@@ -320,6 +321,11 @@ public function load(array $configs, ContainerBuilder $container)
320321
if (!class_exists(DebugCommand::class)) {
321322
$container->removeDefinition('console.command.dotenv_debug');
322323
}
324+
325+
if (!class_exists(ExecuteCommandHandler::class)) {
326+
$container->removeDefinition('console.messenger.application');
327+
$container->removeDefinition('console.messenger.execute_command_handler');
328+
}
323329
}
324330

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php
+15Lines 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\ExecuteCommandHandler;
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;
@@ -356,5 +358,18 @@
356358
service('secrets.local_vault')->ignoreOnInvalid(),
357359
])
358360
->tag('console.command')
361+
362+
->set('console.messenger.application', Application::class)
363+
->share(false)
364+
->call('setAutoExit', [false])
365+
->args([
366+
service('kernel'),
367+
])
368+
369+
->set('console.messenger.execute_command_handler', ExecuteCommandHandler::class)
370+
->args([
371+
service('console.messenger.application'),
372+
])
373+
->tag('messenger.message_handler')
359374
;
360375
};

‎src/Symfony/Component/Console/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Add support for choosing exit code while handling signal, or to not exit at all
88
* Add `ProgressBar::setPlaceholderFormatter` to set a placeholder attached to a instance, instead of being global.
99
* Add `ReStructuredTextDescriptor`
10+
* Add `ExecuteCommand` and `ExecuteCommandHandler`
1011

1112
6.2
1213
---
+29Lines 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 ExecuteCommand 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+
}
+37Lines 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 ExecuteCommandHandler
22+
{
23+
public function __construct(private readonly Application $application)
24+
{
25+
}
26+
27+
public function __invoke(ExecuteCommand $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+
}
+83Lines 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\ExecuteCommand;
19+
use Symfony\Component\Console\Messenger\ExecuteCommandHandler;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
22+
/**
23+
* @author Kevin Bond <kevinbond@gmail.com>
24+
*/
25+
final class ExecuteCommandHandlerTest extends TestCase
26+
{
27+
public function testExecutesCommand()
28+
{
29+
$handler = new ExecuteCommandHandler($this->createApplicationWithCommand());
30+
$output = $handler(new ExecuteCommand('test:command'))->fetch();
31+
32+
$this->assertStringContainsString('some message', $output);
33+
}
34+
35+
public function testExecutesCommandThatThrowsException()
36+
{
37+
$handler = new ExecuteCommandHandler($this->createApplicationWithCommand());
38+
39+
$this->expectException(\RuntimeException::class);
40+
$this->expectExceptionMessage('exception message');
41+
42+
$handler(new ExecuteCommand('test:command --throw'));
43+
}
44+
45+
public function testExecutesCommandThatCatchesThrownException()
46+
{
47+
$handler = new ExecuteCommandHandler($this->createApplicationWithCommand());
48+
$output = $handler(new ExecuteCommand('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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/composer.json
+1Lines 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",
2828
"symfony/dependency-injection": "^5.4|^6.0",
2929
"symfony/lock": "^5.4|^6.0",
30+
"symfony/messenger": "^6.3",
3031
"symfony/process": "^5.4|^6.0",
3132
"symfony/var-dumper": "^5.4|^6.0",
3233
"psr/log": "^1|^2|^3"

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.