forked from inhere/php-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
357 lines (295 loc) · 10.4 KB
/
Copy pathApplication.php
File metadata and controls
357 lines (295 loc) · 10.4 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017-02-27
* Time: 17:56
*/
namespace Inhere\Console;
use Inhere\Console\Base\AbstractApplication;
use Inhere\Console\Utils\Helper;
/**
* Class App
* @package Inhere\Console
*/
class Application extends AbstractApplication
{
/****************************************************************************
* register console controller/command
****************************************************************************/
/**
* Register a app group command(by controller)
* @param string $name The controller name
* @param string $class The controller class
* @return static
* @throws \InvalidArgumentException
*/
public function controller(string $name, string $class = null)
{
if (!$class && class_exists($name)) {
/** @var Controller $class */
$class = $name;
$name = $class::getName();
}
if (!$name || !$class) {
throw new \InvalidArgumentException(
'Group-command "name" and "controller" not allowed to is empty! name: ' . $name . ', controller: ' . $class
);
}
$this->validateName($name, true);
if (!class_exists($class)) {
throw new \InvalidArgumentException("The console controller class [$class] not exists!");
}
if (!is_subclass_of($class, Controller::class)) {
throw new \InvalidArgumentException('The console controller class must is subclass of the: ' . Controller::class);
}
$this->controllers[$name] = $class;
return $this;
}
/**
* {@inheritdoc}
* @see Application::controller()
* @throws \InvalidArgumentException
*/
public function addController(string $name, string $class = null)
{
return $this->controller($name, $class);
}
/**
* @param array $controllers
*/
public function controllers(array $controllers)
{
$this->setControllers($controllers);
}
/**
* Register a app independent console command
* @param string|Command $name
* @param string|\Closure|Command $handler
* @param null|string $description
* @return $this
* @throws \InvalidArgumentException
*/
public function command(string $name, $handler = null, $description = null)
{
if (!$handler && class_exists($name)) {
/** @var Command $name */
$handler = $name;
$name = $name::getName();
}
if (!$name || !$handler) {
throw new \InvalidArgumentException("Command 'name' and 'handler' not allowed to is empty! name: $name");
}
$this->validateName($name);
if (isset($this->commands[$name])) {
throw new \InvalidArgumentException("Command '$name' have been registered!");
}
if (\is_string($handler)) {
if (!class_exists($handler)) {
throw new \InvalidArgumentException("The console command class [$handler] not exists!");
}
if (!is_subclass_of($handler, Command::class)) {
throw new \InvalidArgumentException('The console command class must is subclass of the: ' . Command::class);
}
} elseif (!\is_object($handler) || !method_exists($handler, '__invoke')) {
throw new \InvalidArgumentException(sprintf(
'The console command handler must is an subclass of %s OR a Closure OR a object have method __invoke()',
Command::class
));
}
// is an class name string
$this->commands[$name] = $handler;
if ($description) {
$this->addCommandMessage($name, $description);
}
return $this;
}
/**
* @param array $commands
*/
public function commands(array $commands)
{
$this->setCommands($commands);
}
/**
* addCommand
* @param string $name
* @param mixed $handler
* @return $this
* @throws \InvalidArgumentException
*/
public function addCommand(string $name, $handler = null)
{
return $this->command($name, $handler);
}
/**
* addGroup
* @param string $name
* @param string|null $controller
* @return static
* @throws \InvalidArgumentException
*/
public function addGroup(string $name, string $controller = null)
{
return $this->controller($name, $controller);
}
/**
* auto register commands from a dir.
* @param string $namespace
* @param string $basePath
* @return $this
* @throws \InvalidArgumentException
*/
public function registerCommands(string $namespace, string $basePath)
{
$length = \strlen($basePath) + 1;
$iterator = Helper::recursiveDirectoryIterator($basePath, $this->getFileFilter());
foreach ($iterator as $file) {
$class = $namespace . '\\' . \substr($file, $length, -4);
$this->addCommand($class);
}
return $this;
}
/**
* auto register controllers from a dir.
* @param string $namespace
* @param string $basePath
* @return $this
* @throws \InvalidArgumentException
*/
public function registerGroups(string $namespace, string $basePath)
{
$length = \strlen($basePath) + 1;
$iterator = Helper::recursiveDirectoryIterator($basePath, $this->getFileFilter());
foreach ($iterator as $file) {
$class = $namespace . '\\' . \substr($file, $length, -4);
$this->addController($class);
}
return $this;
}
/**
* @return \Closure
*/
protected function getFileFilter()
{
return function (\SplFileInfo $f) {
$name = $f->getFilename();
// Skip hidden files and directories.
if ($name[0] === '.') {
return false;
}
// go on read sub-dir
if ($f->isDir()) {
return true;
}
// php file
return $f->isFile() && \substr($name, -4) === '.php';
};
}
/****************************************************************************
* dispatch and run console controller/command
****************************************************************************/
/**
* @inheritdoc
* @throws \InvalidArgumentException
*/
protected function dispatch($name)
{
$sep = $this->delimiter ?: '/';
//// is a command name
if ($this->isCommand($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 ($this->isController($name)) {
return $this->runAction($name, $action, true);
}
// command not found
if (true !== self::fire(self::ON_NOT_FOUND, [$this])) {
$this->output->liteError("The console command '{$name}' not exists!");
// find similar command names by similar_text()
$similar = [];
$commands = array_merge($this->getControllerNames(), $this->getCommandNames());
foreach ($commands as $command) {
similar_text($name, $command, $percent);
if (45 <= (int)$percent) {
$similar[] = $command;
}
}
if ($similar) {
$this->write(sprintf("\nMaybe what you mean is:\n <info>%s</info>", implode(', ', $similar)));
} else {
$this->showCommandList(false);
}
}
return 404;
}
/**
* run a command
* @param string $name Command name
* @param bool $believable The `$name` is believable
* @return mixed
* @throws \InvalidArgumentException
*/
public function runCommand($name, $believable = false)
{
// if $believable = true, will skip check.
if (!$believable && $this->isCommand($name)) {
throw new \InvalidArgumentException("The console independent-command [$name] not exists!");
}
/** @var \Closure|string $handler Command class */
$handler = $this->commands[$name];
if (\is_object($handler) && method_exists($handler, '__invoke')) {
$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);
}
$object::setName($name);
$object->setApp($this);
$status = $object->run();
}
return $status;
}
/**
* @param string $name Controller name
* @param string $action Command
* @param bool $believable The `$name` is believable
* @param bool $standAlone
* @return mixed
* @throws \InvalidArgumentException
*/
public function runAction($name, $action, $believable = false, $standAlone = false)
{
// if $believable = true, will skip check.
if (!$believable && !$this->isController($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);
$object->setApp($this);
$object->setDelimiter($this->delimiter);
$object->setStandAlone($standAlone);
return $object->run($action);
}
}