Skip to content

Commit 5ef207b

Browse files
committed
add router class for collect register command/group
1 parent c617af4 commit 5ef207b

File tree

8 files changed

+529
-119
lines changed

8 files changed

+529
-119
lines changed

src/AbstractApplication.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ abstract class AbstractApplication implements ApplicationInterface
7777
*/
7878
public $delimiter = ':'; // '/' ':'
7979

80+
/**
81+
* @var Router
82+
*/
83+
private $router;
84+
8085
/**
8186
* @var ErrorHandlerInterface Can custom error handler
8287
*/
@@ -111,6 +116,7 @@ public function __construct(array $config = [], Input $input = null, Output $out
111116

112117
$this->input = $input ?: new Input();
113118
$this->output = $output ?: new Output();
119+
$this->router = new Router();
114120

115121
$this->init();
116122
}
@@ -416,6 +422,20 @@ public function findCommand(string $name)
416422
return $this->commands[$name] ?? $this->controllers[$name] ?? null;
417423
}
418424

425+
/**
426+
* @param int $level
427+
* @param string $format
428+
* @param mixed ...$args
429+
*/
430+
public function logf(int $level, string $format, ...$args): void
431+
{
432+
if ($this->getVerbLevel() < $level) {
433+
return;
434+
}
435+
436+
Console::logf($level, $format, ...$args);
437+
}
438+
419439
/**********************************************************
420440
* getter/setter methods
421441
**********************************************************/
@@ -578,6 +598,14 @@ public function getVersion(): string
578598
return $this->config['version'];
579599
}
580600

601+
/**
602+
* @return Router
603+
*/
604+
public function getRouter(): Router
605+
{
606+
return $this->router;
607+
}
608+
581609
/**
582610
* @param array $config
583611
*/

src/Application.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function command(string $name, $handler = null, $option = null)
164164
*/
165165
public function commands(array $commands): void
166166
{
167-
$this->setCommands($commands);
167+
$this->getRouter()->addCommands($commands);
168168
}
169169

170170
/**
@@ -266,6 +266,7 @@ protected function getFileFilter(): callable
266266
*/
267267
protected function dispatch(string $name)
268268
{
269+
$this->logf(Console::VERB_DEBUG, 'begin dispatch command - %s', $name);
269270
$sep = $this->delimiter ?: ':';
270271

271272
// maybe is a command name

src/Console.php

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Inhere\Console;
44

55
use Inhere\Console\Component\Style\Style;
6+
use Inhere\Console\IO\Input;
7+
use Inhere\Console\IO\Output;
68
use Toolkit\Cli\ColorTag;
79

810
/**
@@ -20,7 +22,7 @@ class Console
2022
public const VERB_CRAZY = 5;
2123

2224
// level => name
23-
public const VERB_NAMES = [
25+
public const LEVEL_NAMES = [
2426
self::VERB_QUIET => 'QUIET',
2527
self::VERB_ERROR => 'ERROR',
2628
self::VERB_WARN => 'WARN',
@@ -29,13 +31,13 @@ class Console
2931
self::VERB_CRAZY => 'CRAZY',
3032
];
3133

32-
public const LOG_LEVEL2TAG = [
33-
'info' => 'info',
34-
'warn' => 'warning',
35-
'warning' => 'warning',
36-
'debug' => 'cyan',
37-
'notice' => 'notice',
38-
'error' => 'error',
34+
public const LEVEL2TAG = [
35+
self::VERB_QUIET => 'normal',
36+
self::VERB_ERROR => 'error',
37+
self::VERB_WARN => 'warning',
38+
self::VERB_INFO => 'info',
39+
self::VERB_DEBUG => 'cyan',
40+
self::VERB_CRAZY => 'magenta',
3941
];
4042

4143
/**
@@ -65,6 +67,20 @@ public static function setApp(Application $app): void
6567
self::$app = $app;
6668
}
6769

70+
/**
71+
* @param array $config
72+
* @param Input|null $input
73+
* @param Output|null $output
74+
* @return Application
75+
*/
76+
public static function newApp(
77+
array $config = [],
78+
Input $input = null,
79+
Output $output = null
80+
): Application {
81+
return new Application($config, $input, $output);
82+
}
83+
6884
/**
6985
* @return Style
7086
*/
@@ -150,8 +166,8 @@ public static function write($messages, $nl = true, $quit = false, array $opts =
150166
}
151167

152168
$messages = self::$buffer;
153-
154-
self::clearBuffer();
169+
// clear buffer
170+
self::$buffer = '';
155171
} else {
156172
$messages .= $nl ? \PHP_EOL : '';
157173
}
@@ -196,10 +212,26 @@ public static function stderr($text, $nl = true, $quit = -200): void
196212
}
197213

198214
/**
199-
* print log to console
215+
* @param int $level
216+
* @param string $format
217+
* @param mixed ...$args
218+
*/
219+
public static function logf(int $level, string $format, ...$args): void
220+
{
221+
$levelName = self::LEVEL_NAMES[$level] ?? 'INFO';
222+
$colorName = self::LEVEL2TAG[$level] ?? 'info';
223+
$taggedName = ColorTag::add($levelName, $colorName);
224+
225+
$message = \strpos($format, '%') > 0 ? \sprintf($format, ...$args) : $format;
226+
227+
self::writef('[%s] %s', $taggedName, $message);
228+
}
229+
230+
/**
231+
* Print log message to console
200232
* @param string $msg
201233
* @param array $data
202-
* @param string $level
234+
* @param int $level
203235
* @param array $opts
204236
* [
205237
* '_category' => 'application',
@@ -208,11 +240,11 @@ public static function stderr($text, $nl = true, $quit = -200): void
208240
* 'coId' => 12,
209241
* ]
210242
*/
211-
public static function log(string $msg, array $data = [], string $level = 'info', array $opts = []): void
243+
public static function log(string $msg, array $data = [], int $level = self::VERB_DEBUG, array $opts = []): void
212244
{
213-
if (isset(self::LOG_LEVEL2TAG[$level])) {
214-
$level = ColorTag::add(\strtoupper($level), self::LOG_LEVEL2TAG[$level]);
215-
}
245+
$levelName = self::LEVEL_NAMES[$level] ?? 'INFO';
246+
$colorName = self::LEVEL2TAG[$level] ?? 'info';
247+
$taggedName = ColorTag::add($levelName, $colorName);
216248

217249
$userOpts = [];
218250
foreach ($opts as $n => $v) {
@@ -228,7 +260,7 @@ public static function log(string $msg, array $data = [], string $level = 'info'
228260
self::write(\sprintf(
229261
'%s [%s]%s %s %s',
230262
\date('Y/m/d H:i:s'),
231-
$level,
263+
$taggedName,
232264
$optString,
233265
\trim($msg),
234266
$data ? \PHP_EOL . \json_encode($data, \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT) : ''

src/Contract/RouterInterface.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,45 @@ interface RouterInterface
1818
public const NOT_FOUND = 2;
1919

2020
public const TYPE_GROUP = 1;
21-
public const TYPE_SINGLE = 1;
21+
public const TYPE_SINGLE = 2;
2222

2323
/**
2424
* Register a app group command(by controller)
2525
* @param string $name The controller name
2626
* @param string|ControllerInterface $class The controller class
27-
* @param null|array|string $option
28-
* string: define the description message.
27+
* @param array $options
2928
* array:
3029
* - aliases The command aliases
3130
* - description The description message
3231
* @return static
3332
* @throws \InvalidArgumentException
3433
*/
35-
public function controller(string $name, $class = null, $option = null);
34+
public function addGroup(string $name, $class = null, array $options = []): self;
3635

3736
/**
3837
* Register a app independent console command
3938
* @param string|CommandInterface $name
4039
* @param string|\Closure|CommandInterface $handler
41-
* @param null|array|string $option
42-
* string: define the description message.
40+
* @param array $options
4341
* array:
4442
* - aliases The command aliases
4543
* - description The description message
46-
* @return mixed
44+
* @return static
4745
* @throws \InvalidArgumentException
4846
*/
49-
public function command(string $name, $handler = null, $option = null);
47+
public function addCommand(string $name, $handler = null, array $options = []): self;
5048

5149
/**
52-
* @param string $command
53-
* @return array
50+
* @param string $name The input command name
51+
* @return array return route info array. If not found, will return empty array.
5452
* [
55-
* status,
56-
* type,
57-
* route info(array)
53+
* type => 1, // 1 group 2 command
54+
* handler => handler class/object/func ...
55+
* options => [
56+
* aliases => [],
57+
* description => '',
58+
* ],
5859
* ]
5960
*/
60-
public function match(string $command): array;
61+
public function match(string $name): array;
6162
}

src/Dispatcher.php

Lines changed: 0 additions & 86 deletions
This file was deleted.

0 commit comments

Comments
 (0)