From 557c5164d33993965a1c0570289ca0ef82bbad30 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Tue, 23 Sep 2025 20:55:17 +0200 Subject: [PATCH] [Console] Fix invokable command running other commands --- console/calling_commands.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/console/calling_commands.rst b/console/calling_commands.rst index 875ead15d2d..72a262916d2 100644 --- a/console/calling_commands.rst +++ b/console/calling_commands.rst @@ -18,6 +18,7 @@ the returned code from the command (return value from command ``__invoke()`` method):: // ... + use Symfony\Component\Console\Application; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\OutputInterface; @@ -25,7 +26,7 @@ method):: #[AsCommand(name: 'app:create-user')] class CreateUserCommand { - public function __invoke(OutputInterface $output): int + public function __invoke(OutputInterface $output, Application $application): int { $greetInput = new ArrayInput([ // the command name is passed as first argument @@ -37,7 +38,7 @@ method):: // disable interactive behavior for the greet command $greetInput->setInteractive(false); - $returnCode = $this->getApplication()->doRun($greetInput, $output); + $returnCode = $application->doRun($greetInput, $output); // ... } @@ -54,8 +55,8 @@ method):: Using ``doRun()`` instead of ``run()`` prevents autoexiting and allows to return the exit code instead. - Also, using ``$this->getApplication()->doRun()`` instead of - ``$this->getApplication()->find('demo:greet')->run()`` will allow proper + Also, using ``$application->doRun()`` instead of + ``$application->find('demo:greet')->run()`` will allow proper events to be dispatched for that inner command as well. .. warning::