diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 07cc6d6749b48..98168e18c9585 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -48,6 +48,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\ConsoleOutputInterface; +use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\SignalRegistry\SignalRegistry; use Symfony\Component\Console\Style\SymfonyStyle; @@ -145,7 +146,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null } $input ??= new ArgvInput(); - $output ??= new ConsoleOutput(); + $output ??= $this->isDirectOutputDisabled() ? new NullOutput() : new ConsoleOutput(); $renderException = function (\Throwable $e) use ($output) { if ($output instanceof ConsoleOutputInterface) { @@ -934,7 +935,7 @@ protected function configureIO(InputInterface $input, OutputInterface $output): break; } - if (true === $input->hasParameterOption(['--quiet', '-q'], true)) { + if (true === $input->hasParameterOption(['--quiet', '-q'], true) || $this->isDirectOutputDisabled()) { $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); $shellVerbosity = -1; } else { @@ -1281,4 +1282,11 @@ private function init(): void $this->add($command); } } + + private function isDirectOutputDisabled(): bool + { + $output = $_ENV['SYMFONY_CONSOLE_OUTPUT'] ?? $_SERVER['SYMFONY_CONSOLE_OUTPUT'] ?? getenv('SYMFONY_CONSOLE_OUTPUT'); + + return false === filter_var(false === $output ? '1' : $output, FILTER_VALIDATE_BOOL); + } } diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index ca85c24b1f754..5293d87f10b85 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -67,6 +67,9 @@ protected function tearDown(): void putenv('SHELL_VERBOSITY'); unset($_ENV['SHELL_VERBOSITY']); unset($_SERVER['SHELL_VERBOSITY']); + putenv('SYMFONY_CONSOLE_OUTPUT'); + unset($_ENV['SYMFONY_CONSOLE_OUTPUT']); + unset($_SERVER['SYMFONY_CONSOLE_OUTPUT']); if (\function_exists('pcntl_signal')) { // We reset all signals to their default value to avoid side effects @@ -1128,6 +1131,22 @@ public function testRunWithGlobalOptionAndNoCommand() $this->assertSame(0, $application->run($input, $output)); } + public function testDisableOutput() + { + $_ENV['SYMFONY_CONSOLE_OUTPUT'] = '0'; + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $application->add($command = new \Foo1Command()); + $_SERVER['argv'] = ['cli.php', 'foo:bar1']; + + ob_start(); + $application->run(); + ob_end_clean(); + + $this->assertInstanceOf(NullOutput::class, $command->output); + } + /** * Issue #9285. *