Skip to content

Commit dd651ef

Browse files
committed
update some logic for app. add more test for run command
1 parent 190cf57 commit dd651ef

File tree

8 files changed

+140
-43
lines changed

8 files changed

+140
-43
lines changed

src/AbstractApplication.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
/**
4141
* Class AbstractApplication
42+
*
4243
* @package Inhere\Console
4344
*/
4445
abstract class AbstractApplication implements ApplicationInterface
@@ -107,9 +108,11 @@ abstract class AbstractApplication implements ApplicationInterface
107108

108109
/**
109110
* Class constructor.
111+
*
110112
* @param array $config
111113
* @param Input $input
112114
* @param Output $output
115+
*
113116
* @throws InvalidArgumentException
114117
*/
115118
public function __construct(array $config = [], Input $input = null, Output $output = null)
@@ -181,7 +184,9 @@ protected function beforeRun(): void
181184

182185
/**
183186
* run application
187+
*
184188
* @param bool $exit
189+
*
185190
* @return int|mixed
186191
* @throws InvalidArgumentException
187192
*/
@@ -251,6 +256,7 @@ public function stop(int $code = 0)
251256
* @param string $command
252257
* @param InputInterface $input
253258
* @param OutputInterface $output
259+
*
254260
* @return int|mixed
255261
*/
256262
public function subRun(string $command, InputInterface $input, OutputInterface $output)
@@ -274,14 +280,13 @@ protected function runtimeCheck(): void
274280
// check env
275281
if (!in_array(PHP_SAPI, ['cli', 'phpdbg', 'cli-server'], true)) {
276282
header('HTTP/1.1 403 Forbidden');
277-
exit(" 403 Forbidden \n\n"
278-
. " current environment is CLI. \n"
279-
. " :( Sorry! Run this script is only allowed in the terminal environment!\n,You are not allowed to access this file.\n");
283+
exit(" 403 Forbidden \n\n" . " current environment is CLI. \n" . " :( Sorry! Run this script is only allowed in the terminal environment!\n,You are not allowed to access this file.\n");
280284
}
281285
}
282286

283287
/**
284288
* register error handle
289+
*
285290
* @throws InvalidArgumentException
286291
*/
287292
protected function registerErrorHandle(): void
@@ -296,11 +301,13 @@ protected function registerErrorHandle(): void
296301
}
297302

298303
/**
299-
* 运行异常处理
304+
* Running error handling
305+
*
300306
* @param int $num
301307
* @param string $str
302308
* @param string $file
303309
* @param int $line
310+
*
304311
* @throws InvalidArgumentException
305312
*/
306313
public function handleError(int $num, string $str, string $file, int $line): void
@@ -311,18 +318,20 @@ public function handleError(int $num, string $str, string $file, int $line): voi
311318

312319
/**
313320
* Running exception handling
321+
*
314322
* @param Throwable $e
323+
*
315324
* @throws InvalidArgumentException
316325
*/
317326
public function handleException($e): void
318327
{
319328
// you can log error on sub class ...
320-
321329
$this->errorHandler->handle($e, $this);
322330
}
323331

324332
/**
325333
* @param string $command
334+
*
326335
* @return bool True will stop run, False will goon run give command.
327336
*/
328337
protected function filterSpecialCommand(string $command): bool
@@ -365,6 +374,7 @@ protected function filterSpecialCommand(string $command): bool
365374
/**
366375
* @param string $name
367376
* @param string|array $aliases
377+
*
368378
* @return $this
369379
*/
370380
public function addAliases(string $name, $aliases): self
@@ -449,6 +459,7 @@ public function getInternalCommands(): array
449459

450460
/**
451461
* @param $name
462+
*
452463
* @return bool
453464
*/
454465
public function isInternalCommand(string $name): bool
@@ -500,8 +511,10 @@ public function getConfig(): array
500511

501512
/**
502513
* Get config param value
514+
*
503515
* @param null|string $name
504516
* @param null|string $default
517+
*
505518
* @return array|string
506519
*/
507520
public function getParam(string $name, $default = null)
@@ -519,6 +532,7 @@ public function isStrictMode(): bool
519532

520533
/**
521534
* get current debug level value
535+
*
522536
* @return int
523537
*/
524538
public function getVerbLevel(): int
@@ -528,6 +542,7 @@ public function getVerbLevel(): int
528542

529543
/**
530544
* is profile
545+
*
531546
* @return boolean
532547
*/
533548
public function isProfile(): bool

src/Application.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
/**
2727
* Class App
28+
*
2829
* @package Inhere\Console
2930
*/
3031
class Application extends AbstractApplication
@@ -50,7 +51,8 @@ public function controller(string $name, $class = null, $option = null)
5051
}
5152

5253
/**
53-
* add group/controller
54+
* Add group/controller
55+
*
5456
* @inheritdoc
5557
* @see controller()
5658
*/
@@ -61,8 +63,8 @@ public function addGroup(string $name, $controller = null, $option = null)
6163

6264
/**
6365
* {@inheritdoc}
64-
* @see controller()
6566
* @throws InvalidArgumentException
67+
* @see controller()
6668
*/
6769
public function addController(string $name, $class = null, $option = null)
6870
{
@@ -71,6 +73,7 @@ public function addController(string $name, $class = null, $option = null)
7173

7274
/**
7375
* @param array $controllers
76+
*
7477
* @throws InvalidArgumentException
7578
*/
7679
public function controllers(array $controllers): void
@@ -80,6 +83,7 @@ public function controllers(array $controllers): void
8083

8184
/**
8285
* @param array $controllers
86+
*
8387
* @throws InvalidArgumentException
8488
* @deprecated please use addControllers() instead it.
8589
*/
@@ -90,6 +94,7 @@ public function setControllers(array $controllers): void
9094

9195
/**
9296
* @param array $controllers
97+
*
9398
* @throws InvalidArgumentException
9499
*/
95100
public function addControllers(array $controllers): void
@@ -115,6 +120,7 @@ public function command(string $name, $handler = null, $option = null)
115120

116121
/**
117122
* add command
123+
*
118124
* @inheritdoc
119125
* @see command()
120126
*/
@@ -125,6 +131,7 @@ public function addCommand(string $name, $handler = null, $option = null): self
125131

126132
/**
127133
* @param array $commands
134+
*
128135
* @throws InvalidArgumentException
129136
*/
130137
public function addCommands(array $commands): void
@@ -134,6 +141,7 @@ public function addCommands(array $commands): void
134141

135142
/**
136143
* @param array $commands
144+
*
137145
* @throws InvalidArgumentException
138146
*/
139147
public function commands(array $commands): void
@@ -150,6 +158,7 @@ public function commands(array $commands): void
150158
*
151159
* @param string $namespace
152160
* @param string $basePath
161+
*
153162
* @return $this
154163
* @throws InvalidArgumentException
155164
*/
@@ -168,8 +177,10 @@ public function registerCommands(string $namespace, string $basePath): self
168177

169178
/**
170179
* auto register controllers from a dir.
180+
*
171181
* @param string $namespace
172182
* @param string $basePath
183+
*
173184
* @return $this
174185
* @throws InvalidArgumentException
175186
*/
@@ -241,6 +252,9 @@ public function dispatch(string $name, bool $standAlone = false)
241252
return 2;
242253
}
243254

255+
// save command ID
256+
$this->input->setCommandId($info['cmdId']);
257+
244258
// is command
245259
if ($info['type'] === Router::TYPE_SINGLE) {
246260
return $this->runCommand($info['name'], $info['handler'], $info['options']);
@@ -252,9 +266,11 @@ public function dispatch(string $name, bool $standAlone = false)
252266

253267
/**
254268
* run a independent command
255-
* @param string $name Command name
269+
*
270+
* @param string $name Command name
256271
* @param Closure|string $handler Command class
257-
* @param array $options
272+
* @param array $options
273+
*
258274
* @return mixed
259275
* @throws InvalidArgumentException
260276
*/
@@ -290,11 +306,13 @@ protected function runCommand(string $name, $handler, array $options)
290306

291307
/**
292308
* Execute an action in a group command(controller)
293-
* @param string $group The group name
294-
* @param string $action Command method, no suffix
309+
*
310+
* @param string $group The group name
311+
* @param string $action Command method, no suffix
295312
* @param mixed $handler The controller class or object
296313
* @param array $options
297314
* @param bool $standAlone
315+
*
298316
* @return mixed
299317
* @throws ReflectionException
300318
*/
@@ -312,11 +330,8 @@ protected function runAction(string $group, string $action, $handler, array $opt
312330
}
313331

314332
if (!($handler instanceof Controller)) {
315-
Helper::throwInvalidArgument(
316-
'The console controller class [%s] must instanceof the %s',
317-
$handler,
318-
Controller::class
319-
);
333+
Helper::throwInvalidArgument('The console controller class [%s] must instanceof the %s', $handler,
334+
Controller::class);
320335
}
321336

322337
$handler::setName($group);

src/Contract/RouterInterface.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ interface RouterInterface
2626

2727
/**
2828
* Register a app group command(by controller)
29-
* @param string $name The controller name
30-
* @param string|ControllerInterface $class The controller class
31-
* @param array $options
32-
* array:
33-
* - aliases The command aliases
34-
* - description The description message
29+
*
30+
* @param string $name The controller name
31+
* @param string|ControllerInterface $class The controller class
32+
* @param array $options array:
33+
* - aliases The command aliases
34+
* - description The description message
35+
*
3536
* @return static
3637
* @throws InvalidArgumentException
3738
*/
@@ -43,7 +44,7 @@ public function addGroup(string $name, $class = null, array $options = []): self
4344
* @param string|CommandInterface $name
4445
* @param string|Closure|CommandInterface $handler
4546
* @param array $options
46-
* array:
47+
* array:
4748
* - aliases The command aliases
4849
* - description The description message
4950
*
@@ -54,6 +55,7 @@ public function addCommand(string $name, $handler = null, array $options = []):
5455

5556
/**
5657
* @param string $name The input command name
58+
*
5759
* @return array return route info array. If not found, will return empty array.
5860
* [
5961
* type => 1, // 1 group 2 command

0 commit comments

Comments
 (0)