Skip to content

Commit 1dfde30

Browse files
committed
modify debug level logic
1 parent b86104a commit 1dfde30

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

src/AbstractApplication.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class AbstractApplication implements ApplicationInterface
3939

4040
/** @var array */
4141
protected static $internalOptions = [
42-
'--debug' => 'Setting the application runtime debug level',
42+
'--debug' => 'Setting the application runtime debug level(0 - 4)',
4343
'--profile' => 'Display timing and memory usage information',
4444
'--no-color' => 'Disable color/ANSI for message output',
4545
'-h, --help' => 'Display this help message',
@@ -52,7 +52,7 @@ abstract class AbstractApplication implements ApplicationInterface
5252
*/
5353
private $meta = [
5454
'name' => 'My Console Application',
55-
'debug' => false,
55+
'debug' => Console::VERB_ERROR,
5656
'profile' => false,
5757
'version' => '0.5.1',
5858
'publishAt' => '2017.03.24',
@@ -431,12 +431,23 @@ public function showCommandList($quit = true)
431431

432432
// all console controllers
433433
if ($controllers = $this->controllers) {
434+
$hasGroup = true;
434435
\ksort($controllers);
436+
}
437+
438+
// all independent commands, Independent, Single, Alone
439+
if ($commands = $this->commands) {
440+
$hasCommand = true;
441+
\ksort($commands);
442+
}
443+
444+
// add split title on both exists.
445+
if ($hasCommand && $hasGroup) {
446+
$commandArr[] = \PHP_EOL . '- <bold>Alone Commands</bold>';
435447
$controllerArr[] = \PHP_EOL . '- <bold>Group Commands</bold>';
436448
}
437449

438450
foreach ($controllers as $name => $controller) {
439-
$hasGroup = true;
440451
/** @var AbstractCommand $controller */
441452
$desc = $controller::getDescription() ?: $desPlaceholder;
442453
$aliases = $this->getCommandAliases($name);
@@ -448,15 +459,8 @@ public function showCommandList($quit = true)
448459
$controllerArr[] = '... Not register any group command(controller)';
449460
}
450461

451-
// all independent commands, Independent, Single, Alone
452-
if ($commands = $this->commands) {
453-
$commandArr[] = \PHP_EOL . '- <bold>Alone Commands</bold>';
454-
\ksort($commands);
455-
}
456-
457462
foreach ($commands as $name => $command) {
458463
$desc = $desPlaceholder;
459-
$hasCommand = true;
460464

461465
/** @var AbstractCommand $command */
462466
if (\is_subclass_of($command, CommandInterface::class)) {
@@ -755,12 +759,12 @@ public function getMeta(string $name = null, $default = null)
755759
}
756760

757761
/**
758-
* is Debug
759-
* @return boolean|int
762+
* get current debug level value
763+
* @return int
760764
*/
761-
public function isDebug()
765+
public function getVerbLevel(): int
762766
{
763-
return $this->input->getOpt('debug', $this->meta['debug']);
767+
return (int)$this->input->getLongOpt('debug', (int)$this->meta['debug']);
764768
}
765769

766770
/**

src/Console.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,33 @@
88

99
namespace Inhere\Console;
1010

11+
use Inhere\Console\IO\Input;
12+
use Inhere\Console\IO\Output;
13+
1114
/**
1215
* Class Console
1316
* @package Inhere\Console
1417
*/
1518
class Console
1619
{
20+
// constants for error level 0 - 4. you can setting by '--debug LEVEL'
21+
public const VERB_QUIET = 0;
22+
public const VERB_ERROR = 1; // default reporting on error
23+
public const VERB_WARN = 2;
24+
public const VERB_INFO = 3;
25+
public const VERB_DEBUG = 4;
26+
public const VERB_CRAZY = 5;
27+
28+
// level => name
29+
public const VERB_NAMES = [
30+
self::VERB_QUIET => 'QUIET',
31+
self::VERB_ERROR => 'ERROR',
32+
self::VERB_WARN => 'WARN',
33+
self::VERB_INFO => 'INFO',
34+
self::VERB_DEBUG => 'DEBUG',
35+
self::VERB_CRAZY => 'CRAZY',
36+
];
37+
1738
/**
1839
* @var Application
1940
*/
@@ -36,12 +57,14 @@ public static function setApp(Application $app)
3657
}
3758

3859
/**
39-
* @param array $config
60+
* @param array $config
61+
* @param Input|null $input
62+
* @param Output|null $output
4063
* @return Application
4164
*/
42-
public static function newApp(array $config = [])
65+
public static function newApp(array $config = [], Input $input = null, Output $output = null)
4366
{
44-
return new Application($config);
67+
return new Application($config, $input, $output);
4568
}
4669

4770
}

src/Traits/InputOutputAwareTrait.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Inhere\Console\Traits;
1010

11+
use Inhere\Console\Console;
1112
use Inhere\Console\IO\Input;
1213
use Inhere\Console\IO\InputInterface;
1314
use Inhere\Console\IO\Output;
@@ -170,10 +171,21 @@ public function setOutput(OutputInterface $output)
170171
}
171172

172173
/**
174+
* get debug level value
175+
* @return int
176+
*/
177+
public function getVerbLevel(): int
178+
{
179+
return (int)$this->input->getLongOpt('debug', Console::VERB_ERROR);
180+
}
181+
182+
/**
183+
* check is given verbose level
184+
* @param int $level
173185
* @return bool
174186
*/
175-
public function isDebug(): bool
187+
public function isDebug(int $level = Console::VERB_DEBUG): bool
176188
{
177-
return $this->input->boolOpt('debug');
189+
return $level <= $this->getVerbLevel();
178190
}
179191
}

0 commit comments

Comments
 (0)