forked from inhere/php-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.php
More file actions
277 lines (228 loc) · 7.7 KB
/
Copy pathApp.php
File metadata and controls
277 lines (228 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017-02-27
* Time: 17:56
*/
namespace inhere\console;
/**
* Class App
* @package inhere\console
*/
class App extends AbstractApp
{
/**********************************************************
* app run
**********************************************************/
/**
* @inheritdoc
*/
public function doRun()
{
try {
$status = $this->dispatch();
} catch (\Exception $e) {
$status = -$e->getCode();
$this->dispatchExHandler($e);
}
return $status;
}
/**********************************************************
* dispatch and run console controller/command
**********************************************************/
/**
* @return int|mixed
*/
public function dispatch()
{
$sep = '/';
$command = $name = trim($this->input->getCommand(), $sep);
$this->filterSpecialCommand($command);
//// is a command name
if (isset($this->commands[$name])) {
return $this->runCommand($name, true);
}
//// is a controller name
$action = '';
// like 'home/index'
if (strpos($name, $sep) > 0) {
$input = array_filter(explode($sep, $name));
list($name, $action) = count($input) > 2 ? array_splice($input, 2) : $input;
}
if (isset($this->controllers[$name])) {
return $this->runAction($name, $action, true);
}
if ($cb = self::$hooks[self::ON_NOT_FOUND]) {
$cb($command, $this);
} else {
// not match, output error message
$this->output->error("Console Controller or Command [$command] not exists!");
$this->showCommandList(false);
}
return 404;
}
/**
* run a command
* @param string $name Command name
* @param bool $believable The `$name` is believable
* @return mixed
*/
public function runCommand($name, $believable = false)
{
// if $believable = true, will skip check.
if (!$believable && !isset($this->commands[$name])) {
throw new \InvalidArgumentException("The console independent-command [$name] not exists!");
}
// Command class
$handler = $this->commands[$name];
if (is_object($handler) && ($handler instanceof \Closure)) {
$status = $handler($this->input, $this->output);
} else {
if (!class_exists($handler)) {
throw new \InvalidArgumentException("The console command class [$handler] not exists!");
}
/** @var Command $object */
$object = new $handler($this->input, $this->output);
if (!($object instanceof Command)) {
throw new \InvalidArgumentException("The console command class [$handler] must instanceof the " . Command::class);
}
$status = $object->run($name);
}
return $status;
}
/**
* @param string $name Controller name
* @param string $action
* @param bool $believable The `$name` is believable
* @return mixed
*/
public function runAction($name, $action, $believable = false)
{
// if $believable = true, will skip check.
if (!$believable && !isset($this->controllers[$name])) {
throw new \InvalidArgumentException("The console controller-command [$name] not exists!");
}
// Controller class
$controller = $this->controllers[$name];
if (!class_exists($controller)) {
throw new \InvalidArgumentException("The console controller class [$controller] not exists!");
}
/** @var Controller $object */
$object = new $controller($this->input, $this->output);
if (!($object instanceof Controller)) {
throw new \InvalidArgumentException("The console controller class [$object] must instanceof the " . Controller::class);
}
$object->setName($name);
return $object->run($action);
}
/**
* 运行异常处理
* @param \Exception $e
* @throws \Exception
*/
public function dispatchExHandler(\Exception $e)
{
// $this->logger->ex($e);
// open debug, throw exception
if ($this->isDebug()) {
throw $e;
}
// no output
$this->output->error('An error occurred! MESSAGE: ' . $e->getMessage());
}
/**
* @param $command
*/
protected function filterSpecialCommand($command)
{
// show help `./bin/app` OR `./bin/app help`
$showHelp = !$command || $command === 'help';
if ($showHelp) {
$this->showHelpInfo(false);
$this->showCommandList();
}
switch ($command) {
case 'list':
$this->showCommandList();
break;
case 'version':
$this->showVersionInfo();
break;
}
}
/**
* show the application help information
* @param bool $quit
*/
public function showHelpInfo($quit = true)
{
$script = $this->input->getScriptName();
$message = <<<EOF
<comment>Usage:</comment>
$script [route|command] [arg1=value1 arg2=value ...] [-v|-h ...]
<comment>Example:</comment>
$script test
$script home/index
EOF;
$this->output->write($message);
$quit && $this->stop();
}
/**
* show the application version information
* @param bool $quit
*/
public function showVersionInfo($quit = true)
{
$version = $this->config('version', 'Unknown');
$phpVersion = PHP_VERSION;
$os = PHP_OS;
$message = <<<EOF
Console App Version <comment>$version</comment>
<comment>System:</comment>
PHP <info>$phpVersion</info>, on OS <info>$os</info>
EOF;
$this->output->write($message);
$quit && $this->stop();
}
/**
* show the application command list information
* @param bool $quit
*/
public function showCommandList($quit = true)
{
$script = $this->getScriptName();
$controllerArr = $commandArr = [];
// built in commands
$internalCommands = $this->internalCommands;
ksort($internalCommands);
// all console controllers
$controllers = $this->controllers;
ksort($controllers);
foreach ($controllers as $name => $controller) {
$controllerArr[$name] = $controller::DESCRIPTION ?: 'No description';
}
// all independent commands
$commands = $this->commands;
ksort($commands);
foreach ($commands as $name => $command) {
$desc = 'No description';
if (is_subclass_of($command, Command::class)) {
$desc = $command::DESCRIPTION ?: 'No description';
} else if (is_string($command)) {
$desc = 'A handler: ' . $command;
} else if (is_object($command)) {
$desc = $command instanceof \Closure ? 'A Closure' : 'A Object';
}
$commandArr[$name] = $desc;
}
$this->output->write('There are all console controllers and independent commands.');
$this->output->multiList([
'<comment>Group Commands:</comment>(by controller)' => $controllerArr ?: '... No register any group command',
'<comment>Independent Commands:</comment>' => $commandArr ?: '... No register any independent command',
'<comment>Internal Commands:</comment>' => $internalCommands
]);
$this->output->write("more please see: <info>$script [controller|command]</info>");
$quit && $this->stop();
}
}