Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
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
Allow using OutputInterface::VERBOSITY_* constants in PHP config
  • Loading branch information
HeahDude committed May 21, 2022
commit 8fcacbca6504aeff941871672a389eb355fcbabe
6 changes: 5 additions & 1 deletion 6 DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Console\Output\OutputInterface;

/**
* This class contains the configuration information for the bundle
Expand Down Expand Up @@ -1015,10 +1016,13 @@ private function addVerbosityLevelSection(ArrayNodeDefinition $handerNode)
->then(function ($v) {
$map = [];
$verbosities = ['VERBOSITY_QUIET', 'VERBOSITY_NORMAL', 'VERBOSITY_VERBOSE', 'VERBOSITY_VERY_VERBOSE', 'VERBOSITY_DEBUG'];
// allow numeric indexed array with ascendning verbosity and lowercase names of the constants
$verbosityConstants = [OutputInterface::VERBOSITY_QUIET, OutputInterface::VERBOSITY_NORMAL, OutputInterface::VERBOSITY_VERBOSE, OutputInterface::VERBOSITY_VERY_VERBOSE, OutputInterface::VERBOSITY_DEBUG];
Copy link
Member

Choose a reason for hiding this comment

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

The array should look like [OutputInterface::VERBOSITY_QUIET => 'VERBOSITY_QUIET', ...] to remove the array_search and make the code more readable.

Copy link
Member

Choose a reason for hiding this comment

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

I think we can make the code a little clearer with this changes: HeahDude#1

// allow numeric indexed array with ascending verbosity and lowercase names of the constants
foreach ($v as $verbosity => $level) {
if (is_int($verbosity) && isset($verbosities[$verbosity])) {
$map[$verbosities[$verbosity]] = strtoupper($level);
} elseif (is_int($verbosity) && false !== $i = \array_search($verbosity, $verbosityConstants, true)) {
$map[$verbosities[$i]] = strtoupper($level);
} else {
$map[strtoupper($verbosity)] = strtoupper($level);
}
Expand Down
51 changes: 38 additions & 13 deletions 51 Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,17 @@ public function testWithTelegramBotHandler()
$this->assertEquals('-100', $config['handlers']['telegram']['channel']);
}

public function testWithConsoleHandler()
/**
* @dataProvider provideConsoleHandlerCases
*/
public function testWithConsoleHandler(array $verbosityLevels, array $expectedVerbosityMap)
{
$configs = [
[
'handlers' => [
'console' => [
'type' => 'console',
'verbosity_levels' => [
'VERBOSITY_NORMAL' => 'NOTICE',
'verbosity_verbose' => 'info',
'VERBOSITY_very_VERBOSE' => '200'
]
'verbosity_levels' => $verbosityLevels
]
]
]
Expand All @@ -287,13 +286,39 @@ public function testWithConsoleHandler()
$config = $this->process($configs);

$this->assertSame('console', $config['handlers']['console']['type']);
$this->assertSame([
OutputInterface::VERBOSITY_NORMAL => Logger::NOTICE,
OutputInterface::VERBOSITY_VERBOSE => Logger::INFO,
OutputInterface::VERBOSITY_VERY_VERBOSE => 200,
OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG
], $config['handlers']['console']['verbosity_levels']);
$this->assertSame($expectedVerbosityMap, $config['handlers']['console']['verbosity_levels']);
}

public function provideConsoleHandlerCases(): iterable
{
yield 'with strings only' => [
[
'VERBOSITY_NORMAL' => 'NOTICE',
'verbosity_verbose' => 'info',
'VERBOSITY_very_VERBOSE' => '200'
],
[
OutputInterface::VERBOSITY_NORMAL => Logger::NOTICE,
OutputInterface::VERBOSITY_VERBOSE => Logger::INFO,
OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO,
OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG
]
];
yield 'with constants' => [
[
OutputInterface::VERBOSITY_NORMAL => Logger::NOTICE,
'verbosity_verbose' => 'info',
OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO
],
[
OutputInterface::VERBOSITY_NORMAL => Logger::NOTICE,
OutputInterface::VERBOSITY_VERBOSE => Logger::INFO,
OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO,
OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG
]
];
}

public function testWithType()
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.