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::