8000 [Console][HttpKernel] Handle new SHELL_VERBOSITY, also configures the… · symfony/symfony@de0838b · GitHub
[go: up one dir, main page]

Skip to content

Commit de0838b

Browse files
[Console][HttpKernel] Handle new SHELL_VERBOSITY, also configures the default logger
1 parent 2abe788 commit de0838b

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,18 +895,37 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
895895
}
896896
}
897897

898+
switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) {
899+
case -1: $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); break;
900+
case 1: $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); break;
901+
case 2: $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); break;
902+
case 3: $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); break;
903+
default: $shellVerbosity = 0; break;
904+
}
905+
898906
if (true === $input->hasParameterOption(array('--quiet', '-q'), true)) {
899907
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
900-
$input->setInteractive(false);
908+
$shellVerbosity = -1;
901909
} else {
902910
if ($input->hasParameterOption('-vvv', true) || $input->hasParameterOption('--verbose=3', true) || 3 === $input->getParameterOption('--verbose', false, true)) {
903911
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
912+
$shellVerbosity = 3;
904913
} elseif ($input->hasParameterOption('-vv', true) || $input->hasParameterOption('--verbose=2', true) || 2 === $input->getParameterOption('--verbose', false, true)) {
905914
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
915+
$shellVerbosity = 2;
906916
} elseif ($input->hasParameterOption('-v', true) || $input->hasParameterOption('--verbose=1', true) || $input->hasParameterOption('--verbose', true) || $input->getParameterOption('--verbose', false, true)) {
907917
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
918+
$shellVerbosity = 1;
908919
}
909920
}
921+
922+
if (-1 === $shellVerbosity) {
923+
$input->setInteractive(false);
924+
}
925+
926+
putenv('SHELL_VERBOSITY='.$shellVerbosity);
927+
$_ENV['SHELL_VERBOSITY'] = $shellVerbosity;
928+
$_SERVER['SHELL_VERBOSITY'] = $shellVerbosity;
910929
}
911930

912931
/**

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
3.4.0
55
-----
66

7+
* added `SHELL_VERBOSITY` env var to control verbosity
78
* added `CommandLoaderInterface`, `FactoryCommandLoader` and PSR-11
89
`ContainerCommandLoader` for commands lazy-loading
910
* added a case-insensitive command name matching fallback

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,13 @@ public function testErrorIsRethrownIfNotHandledByConsoleErrorEventWithCatchingEn
15801580
$this->assertSame($e->getMessage(), 'Class \'UnknownClass\' not found');
15811581
}
15821582
}
1583+
1584+
protected function tearDown()
1585+
{
1586+
putenv('SHELL_VERBOSITY');
1587+
unset($_ENV['SHELL_VERBOSITY']);
1588+
unset($_SERVER['SHELL_VERBOSITY']);
1589+
}
15831590
}
15841591

15851592
class CustomApplication extends Application

src/Symfony/Component/HttpKernel/DependencyInjection/LoggerPass.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\HttpKernel\DependencyInjection;
1313

1414
use Psr\Log\LoggerInterface;
15-
use Psr\Log\LogLevel;
1615
use Symfony\Component\HttpKernel\Log\Logger;
1716
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1817
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -29,17 +28,14 @@ class LoggerPass implements CompilerPassInterface
2928
*/
3029
public function process(ContainerBuilder $container)
3130
{
32-
$alias = $container->setAlias(LoggerInterface::class, 'logger');
33-
$alias->setPublic(false);
31+
$container->setAlias(LoggerInterface::class, 'logger')
32+
->setPublic(false);
3433

3534
if ($container->has('logger')) {
3635
return;
3736
}
3837

39-
$loggerDefinition = $container->register('logger', Logger::class);
40-
$loggerDefinition->setPublic(false);
41-
if ($container->getParameter('kernel.debug')) {
42-
$loggerDefinition->addArgument(LogLevel::DEBUG);
43-
}
38+
$container->register('logger', Logger::class)
39+
->setPublic(false);
4440
}
4541
}

src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ public function configure(Event $event = null)
131131
}
132132
$this->exceptionHandler = null;
133133
}
134+
if ($this->scream && $event instanceof KernelEvent) {
135+
putenv('SHELL_VERBOSITY=3');
136+
$_ENV['SHELL_VERBOSITY'] = 3;
137+
$_SERVER'SHELL_VERBOSITY'] = 3;
138+
}
134139
}
135140

136141
public static function getSubscribedEvents()

src/Symfony/Component/HttpKernel/Log/Logger.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,21 @@ class Logger extends AbstractLogger
3737
private $formatter;
3838
private $handle;
3939

40-
public function __construct($minLevel = LogLevel::WARNING, $output = 'php://stderr', callable $formatter = null)
40+
public function __construct($minLevel = null, $output = 'php://stderr', callable $formatter = null)
4141
{
42+
if (!$minLevel) {
43+
$minLevel = LogLevel::WARNING;
44+
45+
if (isset($_SERVER['SHELL_VERBOSITY'])) {
46+
switch ($_SERVER['SHELL_VERBOSITY']) {
47+
case '-1': $minLevel = LogLevel::ERROR; break;
48+
case '1': $minLevel = LogLevel::NOTICE; break;
49+
case '2': $minLevel = LogLevel::INFO; break;
50+
case '3': $minLevel = LogLevel::DEBUG; break;
51+
}
52+
}
53+
}
54+
4255
if (!isset(self::$levels[$minLevel])) {
4356
throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $minLevel));
4457
}

0 commit comments

Comments
 (0)
0