Skip to content

Commit 8606c15

Browse files
Merge branch '3.4'
* 3.4: implement reset() in DumpDataCollector [Console][HttpKernel] Handle new SHELL_VERBOSITY, also configures the default logger
2 parents a3b136a + 78e5858 commit 8606c15

File tree

9 files changed

+69
-24
lines changed

9 files changed

+69
-24
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,18 +813,37 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
813813
}
814814
}
815815

816+
switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) {
817+
case -1: $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); break;
818+
case 1: $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); break;
819+
case 2: $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); break;
820+
case 3: $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); break;
821+
default: $shellVerbosity = 0; break;
822+
}
823+
816824
if (true === $input->hasParameterOption(array('--quiet', '-q'), true)) {
817825
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
818-
$input->setInteractive(false);
826+
$shellVerbosity = -1;
819827
} else {
820828
if ($input->hasParameterOption('-vvv', true) || $input->hasParameterOption('--verbose=3', true) || 3 === $input->getParameterOption('--verbose', false, true)) {
821829
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
830+
$shellVerbosity = 3;
822831
} elseif ($input->hasParameterOption('-vv', true) || $input->hasParameterOption('--verbose=2', true) || 2 === $input->getParameterOption('--verbose', false, true)) {
823832
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
833+
$shellVerbosity = 2;
824834
} elseif ($input->hasParameterOption('-v', true) || $input->hasParameterOption('--verbose=1', true) || $input->hasParameterOption('--verbose', true) || $input->getParameterOption('--verbose', false, true)) {
825835
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
836+
$shellVerbosity = 1;
826837
}
827838
}
839+
840+
if (-1 === $shellVerbosity) {
841+
$input->setInteractive(false);
842+
}
843+
844+
putenv('SHELL_VERBOSITY='.$shellVerbosity);
845+
$_ENV['SHELL_VERBOSITY'] = $shellVerbosity;
846+
$_SERVER['SHELL_VERBOSITY'] = $shellVerbosity;
828847
}
829848

830849
/**

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
3.4.0
1515
-----
1616

17+
* added `SHELL_VERBOSITY` env var to control verbosity
1718
* added `CommandLoaderInterface`, `FactoryCommandLoader` and PSR-11
1819
`ContainerCommandLoader` for commands lazy-loading
1920
* 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
@@ -1518,6 +1518,13 @@ public function testErrorIsRethrownIfNotHandledByConsoleErrorEventWithCatchingEn
15181518
$this->assertSame($e->getMessage(), 'Class \'UnknownClass\' not found');
15191519
}
15201520
}
1521+
1522+
protected function tearDown()
1523+
{
1524+
putenv('SHELL_VERBOSITY');
1525+
unset($_ENV['SHELL_VERBOSITY']);
1526+
unset($_SERVER['SHELL_VERBOSITY']);
1527+
}
15211528
}
15221529

15231530
class CustomApplication extends Application

src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ public function collect(Request $request, Response $response, \Exception $except
164164
}
165165
}
166166

167+
public function reset()
168+
{
169+
$this->stopwatch->reset();
170+
$this->data = array();
171+
$this->dataCount = 0;
172+
$this->isCollected = false;
173+
$this->clonesCount = 0;
174+
$this->clonesIndex = 0;
175+
}
176+
167177
public function serialize()
168178
{
169179
if ($this->clonesCount !== $this->clonesIndex) {

src/Symfony/Component/HttpKernel/DataCollector/TimeDataCollector.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\HttpFoundation\Response;
1616
use Symfony\Component\HttpKernel\KernelInterface;
17+
use Symfony\Component\Stopwatch\Stopwatch;
1718

1819
/**
1920
* TimeDataCollector.
@@ -25,7 +26,7 @@ class TimeDataCollector extends DataCollector implements LateDataCollectorInterf
2526
protected $kernel;
2627
protected $stopwatch;
2728

28-
public function __construct(KernelInterface $kernel = null, $stopwatch = null)
29+
public function __construct(KernelInterface $kernel = null, Stopwatch $stopwatch = null)
2930
{
3031
$this->kernel = $kernel;
3132
$this->stopwatch = $stopwatch;
@@ -55,6 +56,10 @@ public function collect(Request $request, Response $response, \Exception $except
5556
public function reset()
5657
{
5758
$this->data = array();
59+
60+
if (null !== $this->stopwatch) {
61+
$this->stopwatch->reset();
62+
}
5863
}
5964

6065
/**

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/Kernel.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ public function shutdown()
169169
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
170170
{
171171
if (false === $this->booted) {
172+
if ($this->debug && !isset($_SERVER['SHELL_VERBOSITY'])) {
173+
putenv('SHELL_VERBOSITY=3');
174+
$_ENV['SHELL_VERBOSITY'] = 3;
175+
$_SERVER['SHELL_VERBOSITY'] = 3;
176+
}
177+
172178
$this->boot();
173179
}
174180

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,28 @@ 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 ((int) $_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
}
4558

4659
$this->minLevelIndex = self::$levels[$minLevel];
4760
$this->formatter = $formatter ?: array($this, 'format');
48-
if (false === $this->handle = @fopen($output, 'a')) {
61+
if (false === $this->handle = is_resource($output) ? $output : @fopen($output, 'a')) {
4962
throw new InvalidArgumentException(sprintf('Unable to open "%s".', $output));
5063
}
5164
}

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/LoggerPassTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Log\LoggerInterface;
16-
use Psr\Log\LogLevel;
1716
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
1817
use Symfony\Component\HttpKernel\Log\Logger;
1918
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -54,15 +53,4 @@ public function testRegisterLogger()
5453
$this->assertSame(Logger::class, $definition->getClass());
5554
$this->assertFalse($definition->isPublic());
5655
}
57-
58-
public function testSetMinLevelWhenDebugging()
59-
{
60-
$container = new ContainerBuilder();
61-
$container->setParameter('kernel.debug', true);
62-
63-
(new LoggerPass())->process($container);
64-
65-
$definition = $container->getDefinition('logger');
66-
$this->assertSame(LogLevel::DEBUG, $definition->getArgument(0));
67-
}
6856
}

0 commit comments

Comments
 (0)