Skip to content

[Console] Add env var to disable direct output #53126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same code path can be triggered using SHELL_VERBOSITY=-1 isnt it?

https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/Console/Application.php#L919-L921

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was more as a shortcut so SYMFONY_CONSOLE_OUTPUT=0 also implies verbosity -1

I would also be fine with not rendering throwables/errors when SHELL_VERBOSITY=-1 in the Application class if that's preferred and not considered a BC break.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd like to avoid SYMFONY_CONSOLE_OUTPUT vs SHELL_VERBOSITY yes :)

Copy link
Contributor

@ro0NL ro0NL Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i understand the BC concern, perhaps we need to reconsider what --quiet really means 😅

SHELL_VERBOSITY=-2 maybe?

Copy link
Member

@chalasr chalasr Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that expanding SHELL_VERBOSITY modes would be better than a new env var.

SHELL_VERBOSITY=-2 maybe?

👍 and --silent maybe

Copy link
Contributor

@ro0NL ro0NL Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd consider making -q really quiet

i see no reason to preserve just error output with -q (assuming error logging is setup)

relying on humans to look at output, is error prone anyway

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've submitted the alternative in a new PR (to not mix up the discussions): #53632

$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
$shellVerbosity = -1;
} else {
Expand Down Expand Up @@ -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);
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down