Skip to content

Commit 3d02946

Browse files
committed
feature #49814 [Console][Messenger] add RunCommandMessage and RunCommandMessageHandler (kbond)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Console][Messenger] add `RunCommandMessage` and `RunCommandMessageHandler` | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | todo Similar to #49813, when using the scheduler it could be useful to execute commands. ## Usage ```php use Symfony\Component\Console\Exception\RunCommandFailedException; use Symfony\Component\Console\Messenger\RunCommandMessage; try { $context = $bus->dispatch(new RunCommandMessage('my:command')); $context->output; // string - output of command $context->exitCode; // int - the exit code catch(RunCommandFailedException $e) { // if exit code is non-zero or command threw exception $e->context->output; // string - output of command $e->context->exitCode; // int - the exit code $e->getPrevious(); // null|\Throwable exception command threw if applicable } // "never" fail $context = $bus->dispatch(new RunCommandMessage('my:command', throwOnNonSuccess: false, catchExceptions: true)); ``` TODO: - [x] wire up - [x] tests Commits ------- dd5b0b7370 [Console][Messenger] add `RunCommandMessage` and `RunCommandMessageHandler`
2 parents b0b9f31 + 05df80f commit 3d02946

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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;
@@ -261,6 +262,11 @@ public function load(array $configs, ContainerBuilder $container)
261262
if (!class_exists(DebugCommand::class)) {
262263
$container->removeDefinition('console.command.dotenv_debug');
263264
}
265+
266+
if (!class_exists(RunCommandMessageHandler::class)) {
267+
$container->removeDefinition('console.messenger.application');
268+
$container->removeDefinition('console.messenger.execute_command_handler');
269+
}
264270
}
265271

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

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
};

0 commit comments

Comments
 (0)