Skip to content

Commit b923458

Browse files
committed
some logic updates ...
1 parent 63aba9c commit b923458

File tree

8 files changed

+186
-138
lines changed

8 files changed

+186
-138
lines changed

src/Application.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
class Application extends AbstractApplication
1818
{
19-
2019
/**********************************************************
2120
* register console controller/command
2221
**********************************************************/
@@ -214,6 +213,7 @@ public function runCommand($name, $believable = false)
214213
}
215214

216215
$object::setName($name);
216+
$object->setApp($this);
217217
$status = $object->run();
218218
}
219219

@@ -250,6 +250,7 @@ public function runAction($name, $action, $believable = false, $standAlone = fal
250250

251251
$object::setName($name);
252252
$object->delimiter = $this->delimiter;
253+
$object->setApp($this);
253254
$object->setStandAlone($standAlone);
254255

255256
return $object->setAction($action)->run();

src/Base/AbstractApplication.php

Lines changed: 87 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Inhere\Console\IO\Output;
1313
use Inhere\Console\Traits\InputOutputTrait;
1414
use Inhere\Console\Traits\SimpleEventTrait;
15+
use Inhere\Console\Utils\Helper;
16+
use Inhere\Library\Helpers\Php;
1517

1618
/**
1719
* Class AbstractApplication
@@ -24,28 +26,6 @@ abstract class AbstractApplication implements ApplicationInterface
2426
/** @var bool render no color */
2527
private static $noColor = false;
2628

27-
/**
28-
* @var string
29-
*/
30-
public $delimiter = ':'; // '/' ':'
31-
32-
/**
33-
* app meta config
34-
* @var array
35-
*/
36-
private $meta = [
37-
'name' => 'My Console',
38-
'version' => '0.5.1',
39-
'publishAt' => '2017.03.24',
40-
'updateAt' => '2017.03.24',
41-
'rootPath' => '',
42-
'hideRootPath' => true,
43-
// 'env' => 'pdt', // dev test pdt
44-
// 'debug' => false,
45-
// 'charset' => 'UTF-8',
46-
// 'timeZone' => 'Asia/Shanghai',
47-
];
48-
4929
/**
5030
* @var array
5131
*/
@@ -65,30 +45,40 @@ abstract class AbstractApplication implements ApplicationInterface
6545
];
6646

6747
/**
48+
* app meta config
6849
* @var array
6950
*/
51+
private $meta = [
52+
'name' => 'My Console',
53+
'debug' => false,
54+
'version' => '0.5.1',
55+
'publishAt' => '2017.03.24',
56+
'updateAt' => '2017.03.24',
57+
'rootPath' => '',
58+
'hideRootPath' => true,
59+
// 'timeZone' => 'Asia/Shanghai',
60+
// 'env' => 'pdt', // dev test pdt
61+
// 'charset' => 'UTF-8',
62+
63+
// runtime stats
64+
'_stats' => [],
65+
];
66+
67+
/** @var string Command delimiter. e.g dev:serve */
68+
public $delimiter = ':'; // '/' ':'
69+
70+
/** @var array The group commands */
7071
protected $controllers = [];
7172

72-
/**
73-
* @var array
74-
*/
73+
/** @var array The independent commands */
7574
protected $commands = [];
7675

77-
/**
78-
* @var array
79-
*/
76+
/** @var array */
8077
private $commandMessages = [];
8178

82-
/**
83-
* @var string
84-
*/
79+
/** @var string */
8580
private $commandName;
8681

87-
/**
88-
* @var bool
89-
*/
90-
//private $hideRootPath = true;
91-
9282
/**
9383
* App constructor.
9484
* @param array $meta
@@ -106,19 +96,13 @@ public function __construct(array $meta = [], Input $input = null, Output $outpu
10696
$this->init();
10797
}
10898

109-
protected function runtimeCheck()
110-
{
111-
// check env
112-
if (!in_array(PHP_SAPI, ['cli', 'cli-server'], true)) {
113-
header('HTTP/1.1 403 Forbidden');
114-
exit(" 403 Forbidden \n\n"
115-
. " current environment is CLI. \n"
116-
. " :( Sorry! Run this script is only allowed in the terminal environment!\n,You are not allowed to access this file.\n");
117-
}
118-
}
119-
12099
protected function init()
121100
{
101+
$this->meta['_stats'] = [
102+
'startTime' => microtime(1),
103+
'startMemory' => memory_get_usage(true),
104+
];
105+
122106
$this->commandName = $this->input->getCommand();
123107
set_exception_handler([$this, 'handleException']);
124108
}
@@ -133,6 +117,9 @@ protected function prepareRun()
133117
//new AutoCompletion(array_merge($this->getCommandNames(), $this->getControllerNames()));
134118
}
135119

120+
protected function beforeRun()
121+
{}
122+
136123
/**
137124
* run app
138125
* @param bool $exit
@@ -146,6 +133,7 @@ public function run($exit = true)
146133

147134
// call 'onBeforeRun' service, if it is registered.
148135
self::fire(self::ON_BEFORE_RUN, [$this]);
136+
$this->beforeRun();
149137

150138
// do run ...
151139
try {
@@ -156,8 +144,11 @@ public function run($exit = true)
156144
$this->handleException($e);
157145
}
158146

147+
$this->meta['_stats']['endTime'] = microtime(1);
148+
159149
// call 'onAfterRun' service, if it is registered.
160150
self::fire(self::ON_AFTER_RUN, [$this]);
151+
$this->afterRun();
161152

162153
if ($exit) {
163154
$this->stop((int)$returnCode);
@@ -170,6 +161,18 @@ public function run($exit = true)
170161
*/
171162
abstract protected function dispatch($command);
172163

164+
protected function afterRun()
165+
{
166+
// display runtime info
167+
if ($this->isDebug()) {
168+
$title = '------------ Runtime Stats ------------';
169+
$stats = $this->meta['_stats'];
170+
$this->meta['_stats'] = Helper::runtime($stats['startTime'], $stats['startMemory'], $stats);
171+
$this->output->write('');
172+
$this->output->aList($this->meta['_stats'], $title);
173+
}
174+
}
175+
173176
/**
174177
* @param int $code
175178
*/
@@ -181,32 +184,55 @@ public function stop($code = 0)
181184
exit((int)$code);
182185
}
183186

184-
185187
/**********************************************************
186188
* helper method for the application
187189
**********************************************************/
188190

191+
/**
192+
* runtime env check
193+
*/
194+
protected function runtimeCheck()
195+
{
196+
// check env
197+
if (!in_array(PHP_SAPI, ['cli', 'cli-server'], true)) {
198+
header('HTTP/1.1 403 Forbidden');
199+
exit(" 403 Forbidden \n\n"
200+
. " current environment is CLI. \n"
201+
. " :( Sorry! Run this script is only allowed in the terminal environment!\n,You are not allowed to access this file.\n");
202+
}
203+
}
204+
189205
/**
190206
* 运行异常处理
191207
* @param \Exception|\Throwable $e
192208
*/
193209
public function handleException($e)
194210
{
195-
// $this->logger->ex($e);
211+
$type = $e instanceof \Error ? 'Error' : 'Exception';
212+
$title = ":( OO ... An $type Occurred!";
213+
$this->logError($e);
196214

197215
// open debug, throw exception
198216
if ($this->isDebug()) {
217+
$tpl = <<<ERR
218+
<danger>$title</danger>
219+
220+
Message <magenta>%s</magenta>
221+
File <cyan>%s</cyan> line <cyan>%d</cyan>
222+
Catch by %s()\n
223+
Code Trace:\n%s\n
224+
ERR;
199225
$message = sprintf(
200-
"<red>Exception</red>: %s\nCalled At %s, Line: <cyan>%d</cyan>\nCatch the exception by: %s\nCode Trace:\n%s\n",
226+
$tpl,
201227
// $e->getCode(),
202228
$e->getMessage(),
203229
$e->getFile(),
204230
$e->getLine(),
205-
get_class($e),
231+
__METHOD__,
206232
$e->getTraceAsString()
207233
);
208234

209-
if ($this->meta['hideRootPath'] && $rootPath = $this->meta['rootPath']) {
235+
if ($this->meta['hideRootPath'] && ($rootPath = $this->meta['rootPath'])) {
210236
$message = str_replace($rootPath, '{ROOT}', $message);
211237
}
212238

@@ -217,6 +243,14 @@ public function handleException($e)
217243
}
218244
}
219245

246+
/**
247+
* @param \Throwable $e
248+
*/
249+
protected function logError($e)
250+
{
251+
// you can log error ...
252+
}
253+
220254
/**
221255
* @param $command
222256
*/
@@ -539,7 +573,7 @@ public function getMeta($name = null, $default = null)
539573
*/
540574
public function isDebug()
541575
{
542-
return $this->input->getOpt('debug');
576+
return $this->input->getOpt('debug', $this->meta['debug']);
543577
}
544578

545579
/**

0 commit comments

Comments
 (0)