Skip to content

Commit 82ba9a3

Browse files
committed
update: exception display, add required arg|opt get method...
1 parent 8ba7684 commit 82ba9a3

File tree

5 files changed

+80
-15
lines changed

5 files changed

+80
-15
lines changed

src/AbstractApp.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ abstract class AbstractApp
3838
*/
3939
protected $config = [
4040
'env' => 'pdt', // dev test pdt
41-
'debug' => false,
41+
'debug' => true,
4242
'name' => 'My Console',
4343
'version' => '0.5.1',
4444
'publishAt' => '2017.03.24',
@@ -118,7 +118,7 @@ public function run($exit = true)
118118
// do run ...
119119
try {
120120
$returnCode = $this->dispatch($command);
121-
} catch (\Exception $e) {
121+
} catch (\Throwable $e) {
122122
self::fire(self::ON_RUN_ERROR, [$e, $this]);
123123
$returnCode = $e->getCode() === 0 ? __LINE__ : $e->getCode();
124124
$this->dispatchExHandler($e);
@@ -243,7 +243,11 @@ public function command(string $name, $handler = null)
243243
public function commands(array $commands)
244244
{
245245
foreach ($commands as $name => $handler) {
246-
$this->command($name, $handler);
246+
if (is_int($name)) {
247+
$this->command($handler);
248+
} else {
249+
$this->command($name, $handler);
250+
}
247251
}
248252
}
249253

@@ -253,20 +257,30 @@ public function commands(array $commands)
253257

254258
/**
255259
* 运行异常处理
256-
* @param \Exception $e
260+
* @param \Exception|\Throwable $e
257261
* @throws \Exception
258262
*/
259-
protected function dispatchExHandler(\Exception $e)
263+
protected function dispatchExHandler($e)
260264
{
261265
// $this->logger->ex($e);
262266

263267
// open debug, throw exception
264268
if ($this->isDebug()) {
265-
throw $e;
269+
$message = sprintf(
270+
"<bold>Exception(%d)</bold>: <red>%s</red>\nCalled At %s, Line: <cyan>%d</cyan>\nCatch the exception by: %s\nCode Trace:\n%s\n",
271+
$e->getCode(),
272+
$e->getMessage(),
273+
$e->getFile(),
274+
$e->getLine(),
275+
get_class($e),
276+
$e->getTraceAsString()
277+
);
278+
279+
$this->output->write($message, false);
280+
} else {
281+
// simple output
282+
$this->output->error('An error occurred! MESSAGE: ' . $e->getMessage());
266283
}
267-
268-
// no output
269-
$this->output->error('An error occurred! MESSAGE: ' . $e->getMessage());
270284
}
271285

272286
/**

src/App.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@
1414
*/
1515
class App extends AbstractApp
1616
{
17+
/**
18+
* @var string
19+
*/
20+
private $delimiter = '/';
21+
1722
/**
1823
* addCommand
19-
* @param string $name
20-
* @param mixed $controller
24+
* @param string $name
25+
* @param mixed $handler
26+
* @return $this
2127
*/
2228
public function addCommand(string $name, $handler = null)
2329
{
@@ -26,8 +32,9 @@ public function addCommand(string $name, $handler = null)
2632

2733
/**
2834
* addGroup
29-
* @param string $name
35+
* @param string $name
3036
* @param string|null $controller
37+
* @return static
3138
*/
3239
public function addGroup(string $name, string $controller = null)
3340
{
@@ -43,7 +50,7 @@ public function addGroup(string $name, string $controller = null)
4350
*/
4451
protected function dispatch($name)
4552
{
46-
$sep = '/';
53+
$sep = $this->delimiter ?: '/';
4754

4855
//// is a command name
4956

src/io/Input.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ public function get($name, $default = null)
174174
return $this->args[$name] ?? $default;
175175
}
176176

177+
/**
178+
* get a required argument
179+
* @param int|string $name
180+
* @return mixed
181+
*/
182+
public function getRequiredArg($name)
183+
{
184+
if (isset($this->args[$name])) {
185+
return $this->args[$name];
186+
}
187+
188+
throw new \InvalidArgumentException("The argument '{$name}' is required");
189+
}
190+
177191
/**
178192
* get first argument
179193
* @param string $default
@@ -238,6 +252,20 @@ public function getOption(string $name, $default = null)
238252
return $this->getOpt($name, $default);
239253
}
240254

255+
/**
256+
* get a required argument
257+
* @param int|string $name
258+
* @return mixed
259+
*/
260+
public function getRequiredOpt($name)
261+
{
262+
if (null === ($val = $this->getOpt($name))) {
263+
throw new \InvalidArgumentException("The option '{$name}' is required");
264+
}
265+
266+
return $val;
267+
}
268+
241269
/**
242270
* get (long/short)opt value(bool)
243271
* eg: -h --help

src/io/Output.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ public function stderr($text = '', $nl = true)
7373
return $this;
7474
}
7575

76+
/**
77+
* @param array ...$args
78+
*/
79+
public function dumpVars(...$args)
80+
{
81+
echo Helper::dumpVars(...$args);
82+
}
83+
84+
/**
85+
* @param array ...$args
86+
*/
87+
public function printVars(...$args)
88+
{
89+
echo Helper::printVars(...$args);
90+
}
91+
7692
/////////////////////////////////////////////////////////////////
7793
/// Getter/Setter
7894
/////////////////////////////////////////////////////////////////

src/utils/Helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public static function wrapText($text, $indent = 0, $width = 0)
411411
* @param array ...$args
412412
* @return string
413413
*/
414-
public static function dumpVar(...$args)
414+
public static function dumpVars(...$args)
415415
{
416416
ob_start();
417417
var_dump(...$args);
@@ -425,7 +425,7 @@ public static function dumpVar(...$args)
425425
* @param array ...$args
426426
* @return string
427427
*/
428-
public static function printR(...$args)
428+
public static function printVars(...$args)
429429
{
430430
ob_start();
431431

0 commit comments

Comments
 (0)