Skip to content

Commit c1d4356

Browse files
committed
👔 refactor(router): move router class to component dir
1 parent 5b887a4 commit c1d4356

File tree

7 files changed

+43
-41
lines changed

7 files changed

+43
-41
lines changed

src/AbstractApplication.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use ErrorException;
1313
use Inhere\Console\Component\ErrorHandler;
14+
use Inhere\Console\Component\Router;
1415
use Inhere\Console\Component\Formatter\Title;
1516
use Inhere\Console\Concern\ApplicationHelpTrait;
1617
use Inhere\Console\Concern\InputOutputAwareTrait;

src/Application.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Inhere\Console;
1111

1212
use Closure;
13-
use Inhere\Console\Contract\ApplicationInterface;
13+
use Inhere\Console\Component\Router;
1414
use Inhere\Console\Contract\CommandInterface;
1515
use Inhere\Console\Contract\ControllerInterface;
1616
use Inhere\Console\IO\Input;
@@ -62,7 +62,7 @@ public function __construct(array $config = [], Input $input = null, Output $out
6262
/**
6363
* {@inheritdoc}
6464
*/
65-
public function controller(string $name, ControllerInterface|string $class = null, array $config = []): ApplicationInterface
65+
public function controller(string $name, ControllerInterface|string $class = null, array $config = []): static
6666
{
6767
$this->logf(Console::VERB_CRAZY, 'register group controller: %s', $name);
6868
$this->router->addGroup($name, $class, $config);
@@ -73,14 +73,14 @@ public function controller(string $name, ControllerInterface|string $class = nul
7373
/**
7474
* Add group/controller
7575
*
76-
* @param string $name
76+
* @param string|class-string $name
7777
* @param string|ControllerInterface|null $class The controller class
7878
* @param array $config
7979
*
80-
* @return Application|Contract\ApplicationInterface
80+
* @return static
8181
* @see controller()
8282
*/
83-
public function addGroup(string $name, ControllerInterface|string $class = null, array $config = []): ApplicationInterface
83+
public function addGroup(string $name, ControllerInterface|string $class = null, array $config = []): static
8484
{
8585
return $this->controller($name, $class, $config);
8686
}
@@ -90,10 +90,10 @@ public function addGroup(string $name, ControllerInterface|string $class = null,
9090
* @param string|ControllerInterface|null $class The controller class
9191
* @param array $config
9292
*
93-
* @return Application|Contract\ApplicationInterface
93+
* @return $this
9494
* @see controller()
9595
*/
96-
public function addController(string $name, ControllerInterface|string $class = null, array $config = []): ApplicationInterface
96+
public function addController(string $name, ControllerInterface|string $class = null, array $config = []): static
9797
{
9898
return $this->controller($name, $class, $config);
9999
}

src/Router.php renamed to src/Component/Router.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
* @license https://github.com/inhere/php-console/blob/master/LICENSE
88
*/
99

10-
namespace Inhere\Console;
10+
namespace Inhere\Console\Component;
1111

1212
use Closure;
13+
use Inhere\Console\Command;
1314
use Inhere\Console\Contract\CommandInterface;
1415
use Inhere\Console\Contract\ControllerInterface;
1516
use Inhere\Console\Contract\RouterInterface;
17+
use Inhere\Console\Controller;
1618
use Inhere\Console\Util\Helper;
1719
use InvalidArgumentException;
1820
use Toolkit\Stdlib\Obj\Traits\NameAliasTrait;
@@ -86,15 +88,15 @@ class Router implements RouterInterface
8688
*
8789
* @param string $name The controller name
8890
* @param string|ControllerInterface|null $class The controller class
89-
* @param array $options
91+
* @param array{aliases: array, desc: string} $config config for group.
9092
* array:
9193
* - aliases The command aliases
92-
* - description The description message
94+
* - desc The description message
9395
*
94-
* @return Router
96+
* @return static
9597
* @throws InvalidArgumentException
9698
*/
97-
public function addGroup(string $name, ControllerInterface|string $class = null, array $options = []): RouterInterface
99+
public function addGroup(string $name, ControllerInterface|string $class = null, array $config = []): static
98100
{
99101
/**
100102
* @var Controller $class name is an controller class
@@ -141,7 +143,7 @@ public function addGroup(string $name, ControllerInterface|string $class = null,
141143
];
142144

143145
// has alias option
144-
if (isset($options['aliases'])) {
146+
if ($options['aliases']) {
145147
$this->setAlias($name, $options['aliases'], true);
146148
}
147149

@@ -151,17 +153,13 @@ public function addGroup(string $name, ControllerInterface|string $class = null,
151153
/**
152154
* Register a app independent console command
153155
*
154-
* @param string|CommandInterface $name
156+
* @param string|class-string $name
155157
* @param string|Closure|CommandInterface|null $handler
156-
* @param array $options
157-
* array:
158-
* - aliases The command aliases
159-
* - description The description message
158+
* @param array{aliases: array, desc: string} $config
160159
*
161-
* @return Router|RouterInterface
162-
* @throws InvalidArgumentException
160+
* @return static
163161
*/
164-
public function addCommand(string $name, string|Closure|CommandInterface $handler = null, array $options = []): RouterInterface
162+
public function addCommand(string $name, string|Closure|CommandInterface $handler = null, array $config = []): static
165163
{
166164
if (!$handler && class_exists($name)) {
167165
$handler = $name;
@@ -217,7 +215,7 @@ public function addCommand(string $name, string|Closure|CommandInterface $handle
217215
];
218216

219217
// has alias option
220-
if (isset($options['aliases'])) {
218+
if ($options['aliases']) {
221219
$this->setAlias($name, $options['aliases'], true);
222220
}
223221

src/Console.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
use Inhere\Console\IO\Output;
1414
use Toolkit\Cli\Cli;
1515
use Toolkit\Cli\Color\ColorTag;
16+
use Toolkit\Stdlib\Json;
1617
use function date;
1718
use function debug_backtrace;
1819
use function implode;
1920
use function is_numeric;
20-
use function json_encode;
2121
use function sprintf;
2222
use function strpos;
2323
use function trim;
@@ -179,7 +179,7 @@ public static function log(int $level, string $msg, array $data = [], array $opt
179179
}
180180

181181
$optString = $userOpts ? ' ' . implode(' ', $userOpts) : '';
182-
$dataString = $data ? json_encode($data, JSON_UNESCAPED_SLASHES) : '';
182+
$dataString = $data ? Json::encode($data, JSON_UNESCAPED_SLASHES) : '';
183183

184184
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, self::$traceIndex + 2);
185185
$position = self::formatBacktrace($backtrace, self::$traceIndex);

src/Contract/ApplicationInterface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface ApplicationInterface
3333
/**
3434
* @param bool $exit
3535
*
36-
* @return int|mixed
36+
* @return mixed
3737
*/
3838
public function run(bool $exit = true): mixed;
3939

@@ -46,7 +46,7 @@ public function run(bool $exit = true): mixed;
4646
* - 'group action'
4747
* @param array $args
4848
*
49-
* @return int|mixed
49+
* @return mixed
5050
*/
5151
public function dispatch(string $name, array $args = []): mixed;
5252

@@ -60,21 +60,21 @@ public function stop(int $code = 0): void;
6060
*
6161
* @param string $name The controller name
6262
* @param string|ControllerInterface|null $class The controller class
63-
* @param array $config config the controller
63+
* @param array{desc: string, aliases: array} $config config the controller.
6464
* - aliases The command aliases
6565
* - desc The description message
6666
*
6767
* @return static
6868
* @throws InvalidArgumentException
6969
*/
70-
public function controller(string $name, ControllerInterface|string $class = null, array $config = []): ApplicationInterface;
70+
public function controller(string $name, ControllerInterface|string $class = null, array $config = []): static;
7171

7272
/**
7373
* Register a app independent console command
7474
*
75-
* @param string|CommandInterface $name
75+
* @param string|class-string $name
7676
* @param string|Closure|CommandInterface|null $handler
77-
* @param array $config config the command
77+
* @param array{desc: string, aliases: array} $config config the command.
7878
* - aliases The command aliases
7979
* - desc The description message
8080
*

src/Contract/RouterInterface.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,46 @@ interface RouterInterface
3030
/**
3131
* Register a app group command(by controller)
3232
*
33-
* @param string $name The controller name
33+
* @param string|class-string $name The controller name
3434
* @param string|ControllerInterface|null $class The controller class
35-
* @param array{aliases: array, desc: string} $options The options
35+
* @param array{aliases: array, desc: string} $config The options config.
3636
* - aliases The command aliases
3737
* - desc The description message
3838
*
3939
* @return static
4040
* @throws InvalidArgumentException
4141
*/
42-
public function addGroup(string $name, ControllerInterface|string $class = null, array $options = []): self;
42+
public function addGroup(string $name, ControllerInterface|string $class = null, array $config = []): static;
4343

4444
/**
4545
* Register a app independent console command
4646
*
47-
* @param string|CommandInterface $name
47+
* @param string|class-string $name
4848
* @param string|Closure|CommandInterface|null $handler
49-
* @param array{aliases: array, desc: string} $options The options
49+
* @param array{aliases: array, desc: string} $config The options config.
5050
* - aliases The command aliases
5151
* - desc The description message
5252
*
5353
* @return static
5454
* @throws InvalidArgumentException
5555
*/
56-
public function addCommand(string $name, string|Closure|CommandInterface $handler = null, array $options = []): self;
56+
public function addCommand(string $name, string|Closure|CommandInterface $handler = null, array $config = []): static;
5757

5858
/**
59-
* @param string $name The input command name
60-
*
61-
* @return array return route info array. If not found, will return empty array.
62-
* [
59+
* ```php
60+
* return [
6361
* type => 1, // 1 group 2 command
6462
* handler => handler class/object/func ...
6563
* options => [
6664
* aliases => [],
6765
* description => '',
6866
* ],
6967
* ]
68+
* ```
69+
*
70+
* @param string $name The input command name
71+
*
72+
* @return array return route info array. If not found, will return empty array.
7073
*/
7174
public function match(string $name): array;
7275
}

test/ApplicationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Inhere\Console\Console;
1414
use Inhere\Console\IO\Input;
1515
use Inhere\Console\Contract\InputInterface;
16-
use Inhere\Console\Router;
16+
use Inhere\Console\Component\Router;
1717
use InvalidArgumentException;
1818
use PHPUnit\Framework\TestCase;
1919
use Throwable;

0 commit comments

Comments
 (0)