Skip to content

Commit cf2145f

Browse files
committed
update some logic. allow register controller instance
1 parent 085780b commit cf2145f

File tree

9 files changed

+151
-95
lines changed

9 files changed

+151
-95
lines changed

composer.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
"name": "inhere/console",
33
"type": "library",
44
"description": "a php console library, provide console argument parse, console controller/command run, color style, user interactive, information show.",
5-
"keywords": ["library","console","console-color", "command", "command-line", "cli", "console-application"],
5+
"keywords": [
6+
"library",
7+
"console",
8+
"console-color",
9+
"command",
10+
"command-line",
11+
"cli",
12+
"console-application"
13+
],
614
"homepage": "https://github.com/inhere/php-console",
715
"license": "MIT",
816
"authors": [
@@ -13,18 +21,18 @@
1321
}
1422
],
1523
"require": {
16-
"php": ">=7.0.0",
24+
"php": ">7.0.0",
1725
"toolkit/cli-utils": "~1.0",
1826
"toolkit/sys-utils": "~1.0"
1927
},
2028
"autoload": {
2129
"psr-4": {
22-
"Inhere\\Console\\" : "src/"
30+
"Inhere\\Console\\": "src/"
2331
}
2432
},
2533
"autoload-dev": {
2634
"psr-4": {
27-
"Inhere\\Console\\Tests\\" : "tests/"
35+
"Inhere\\Console\\Test\\": "test/"
2836
}
2937
},
3038
"suggest": {

examples/demo/spinner.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018/9/6
6+
* Time: 下午5:15
7+
*/
8+
9+
class Spinner {
10+
/** @var int $speed ms */
11+
public $speed = 100;
12+
13+
public static function create($speed)
14+
{
15+
16+
}
17+
}
18+
19+
Swoole\Runtime::enableCoroutine();
20+
21+
function spinner()
22+
{
23+
$chars = '-\|/';
24+
$index = 0;
25+
26+
yield function () use ($chars, $index) {
27+
while (1) {
28+
printf("\x0D\x1B[2K %s handling ...", $chars[$index]);
29+
30+
if ($index+1 === mb_strlen($chars)) {
31+
$index = 0;
32+
} else {
33+
$index++;
34+
}
35+
}
36+
};
37+
}
38+
39+
$y = spinner();
40+
// $y->rewind();
41+
sleep(4);
42+
$y->send(false);

src/Application.php

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,35 @@ class Application extends AbstractApplication
2424
/**
2525
* Register a app group command(by controller)
2626
* @param string $name The controller name
27-
* @param string $class The controller class
27+
* @param string|Controller $class The controller class
2828
* @param null|array|string $option
2929
* @return static
3030
* @throws \InvalidArgumentException
3131
*/
32-
public function controller(string $name, string $class = null, $option = null)
32+
public function controller(string $name, $class = null, $option = null)
3333
{
34-
if (!$class && class_exists($name)) {
35-
/** @var Controller $class */
34+
/** @var Controller $class */
35+
if (!$class && \class_exists($name)) {
3636
$class = $name;
3737
$name = $class::getName();
3838
}
3939

4040
if (!$name || !$class) {
41-
throw new \InvalidArgumentException(
42-
'Group-command "name" and "controller" not allowed to is empty! name: ' . $name . ', controller: ' . $class
41+
Helper::throwInvalidArgument(
42+
'Group-command "name" and "controller" not allowed to is empty! name: %s, controller: %s',
43+
$name,
44+
$class
4345
);
4446
}
4547

4648
$this->validateName($name, true);
4749

48-
if (!class_exists($class)) {
49-
throw new \InvalidArgumentException("The console controller class [$class] not exists!");
50+
if (\is_string($class) && !\class_exists($class)) {
51+
Helper::throwInvalidArgument("The console controller class [$class] not exists!");
5052
}
5153

52-
if (!is_subclass_of($class, Controller::class)) {
53-
throw new \InvalidArgumentException('The console controller class must is subclass of the: ' . Controller::class);
54+
if (!\is_subclass_of($class, Controller::class)) {
55+
Helper::throwInvalidArgument('The console controller class must is subclass of the: ' . Controller::class);
5456
}
5557

5658
// not enable
@@ -60,21 +62,19 @@ public function controller(string $name, string $class = null, $option = null)
6062

6163
// allow define aliases in Command class by Controller::aliases()
6264
if ($aliases = $class::aliases()) {
63-
$option['aliases'] = isset($option['aliases']) ? array_merge($option['aliases'], $aliases) : $aliases;
65+
$option['aliases'] = isset($option['aliases']) ? \array_merge($option['aliases'], $aliases) : $aliases;
6466
}
6567

6668
$this->controllers[$name] = $class;
6769

68-
if (!$option) {
69-
return $this;
70-
}
71-
72-
// have option information
73-
if (\is_string($option)) {
74-
$this->addCommandMessage($name, $option);
75-
} elseif (\is_array($option)) {
76-
$this->addCommandAliases($name, $option['aliases'] ?? null);
77-
$this->addCommandMessage($name, $option['description'] ?? null);
70+
// has option information
71+
if ($option) {
72+
if (\is_string($option)) {
73+
$this->addCommandMessage($name, $option);
74+
} elseif (\is_array($option)) {
75+
$this->addCommandAliases($name, $option['aliases'] ?? null);
76+
$this->addCommandMessage($name, $option['description'] ?? null);
77+
}
7878
}
7979

8080
return $this;
@@ -85,7 +85,7 @@ public function controller(string $name, string $class = null, $option = null)
8585
* @see Application::controller()
8686
* @throws \InvalidArgumentException
8787
*/
88-
public function addController(string $name, string $class = null, $option = null)
88+
public function addController(string $name, $class = null, $option = null)
8989
{
9090
return $this->controller($name, $class, $option);
9191
}
@@ -109,7 +109,7 @@ public function controllers(array $controllers)
109109
*/
110110
public function command(string $name, $handler = null, $option = null)
111111
{
112-
if (!$handler && class_exists($name)) {
112+
if (!$handler && \class_exists($name)) {
113113
/** @var Command $name */
114114
$handler = $name;
115115
$name = $name::getName();
@@ -126,7 +126,7 @@ public function command(string $name, $handler = null, $option = null)
126126
}
127127

128128
if (\is_string($handler)) {
129-
if (!class_exists($handler)) {
129+
if (!\class_exists($handler)) {
130130
throw new \InvalidArgumentException("The console command class [$handler] not exists!");
131131
}
132132

@@ -144,7 +144,7 @@ public function command(string $name, $handler = null, $option = null)
144144
if ($aliases = $handler::aliases()) {
145145
$option['aliases'] = isset($option['aliases']) ? array_merge($option['aliases'], $aliases) : $aliases;
146146
}
147-
} elseif (!\is_object($handler) || !method_exists($handler, '__invoke')) {
147+
} elseif (!\is_object($handler) || !\method_exists($handler, '__invoke')) {
148148
throw new \InvalidArgumentException(sprintf(
149149
'The console command handler must is an subclass of %s OR a Closure OR a object have method __invoke()',
150150
Command::class
@@ -199,7 +199,7 @@ public function addCommand(string $name, $handler = null, $option = null): self
199199
* @return static
200200
* @throws \InvalidArgumentException
201201
*/
202-
public function addGroup(string $name, string $controller = null, $option = null)
202+
public function addGroup(string $name, $controller = null, $option = null)
203203
{
204204
return $this->controller($name, $controller, $option);
205205
}
@@ -253,7 +253,7 @@ protected function getFileFilter(): callable
253253
$name = $f->getFilename();
254254

255255
// Skip hidden files and directories.
256-
if ($name[0] === '.') {
256+
if (\strpos($name, '.') === 0) {
257257
return false;
258258
}
259259

@@ -302,7 +302,7 @@ protected function dispatch(string $name)
302302
}
303303

304304
// command not found
305-
if (true !== $this->fire(self::ON_NOT_FOUND, [$this])) {
305+
if (true !== $this->fire(self::ON_NOT_FOUND, $this)) {
306306
$this->output->liteError("The command '{$name}' is not exists in the console application!");
307307

308308
$commands = \array_merge($this->getControllerNames(), $this->getCommandNames());
@@ -325,7 +325,7 @@ protected function dispatch(string $name)
325325
* @return mixed
326326
* @throws \InvalidArgumentException
327327
*/
328-
public function runCommand($name, $believable = false)
328+
public function runCommand(string $name, bool $believable = false)
329329
{
330330
// if $believable = true, will skip check.
331331
if (!$believable && $this->isCommand($name)) {
@@ -372,25 +372,29 @@ public function runCommand($name, $believable = false)
372372
* @throws \InvalidArgumentException
373373
* @throws \ReflectionException
374374
*/
375-
public function runAction($name, $action, $believable = false, $standAlone = false)
375+
public function runAction(string $name, string $action, bool $believable = false, bool $standAlone = false)
376376
{
377377
// if $believable = true, will skip check.
378378
if (!$believable && !$this->isController($name)) {
379-
throw new \InvalidArgumentException("The console controller-command [$name] not exists!");
379+
Helper::throwInvalidArgument('The console controller-command [%s] not exists!', $name);
380380
}
381381

382382
// Controller class
383-
$controller = $this->controllers[$name];
383+
$object = $this->controllers[$name];
384384

385-
if (!class_exists($controller)) {
386-
throw new \InvalidArgumentException("The console controller class [$controller] not exists!");
387-
}
385+
if (\is_string($object)) {
386+
$class = $object;
388387

389-
/** @var Controller $object */
390-
$object = new $controller($this->input, $this->output);
388+
if (!\class_exists($class)) {
389+
Helper::throwInvalidArgument('The console controller class [%s] not exists!', $class);
390+
}
391+
392+
/** @var Controller $object */
393+
$object = new $class($this->input, $this->output);
394+
}
391395

392396
if (!($object instanceof Controller)) {
393-
throw new \InvalidArgumentException("The console controller class [$object] must instanceof the " . Controller::class);
397+
Helper::throwInvalidArgument('The console controller class [%s] must instanceof the %s', $object, Controller::class);
394398
}
395399

396400
$object::setName($name);

src/Base/AbstractApplication.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,30 @@ protected function beforeRun()
151151
* @param bool $exit
152152
* @throws \InvalidArgumentException
153153
*/
154-
public function run($exit = true)
154+
public function run(bool $exit = true)
155155
{
156156
$command = trim($this->input->getCommand(), $this->delimiter);
157157

158158
$this->prepareRun();
159159
$this->filterSpecialCommand($command);
160160

161161
// call 'onBeforeRun' service, if it is registered.
162-
$this->fire(self::ON_BEFORE_RUN, [$this]);
162+
$this->fire(self::ON_BEFORE_RUN, $this);
163163
$this->beforeRun();
164164

165165
// do run ...
166166
try {
167167
$returnCode = $this->dispatch($command);
168168
} catch (\Throwable $e) {
169-
$this->fire(self::ON_RUN_ERROR, [$e, $this]);
169+
$this->fire(self::ON_RUN_ERROR, $e, $this);
170170
$returnCode = $e->getCode() === 0 ? $e->getLine() : $e->getCode();
171171
$this->handleException($e);
172172
}
173173

174174
$this->meta['_stats']['endTime'] = microtime(1);
175175

176176
// call 'onAfterRun' service, if it is registered.
177-
$this->fire(self::ON_AFTER_RUN, [$this]);
177+
$this->fire(self::ON_AFTER_RUN, $this);
178178
$this->afterRun();
179179

180180
if ($exit) {
@@ -189,29 +189,17 @@ public function run($exit = true)
189189
*/
190190
abstract protected function dispatch(string $command);
191191

192-
/**
193-
* run a independent command
194-
* {@inheritdoc}
195-
*/
196-
abstract public function runCommand($name, $believable = false);
197-
198-
/**
199-
* run a controller's action
200-
* {@inheritdoc}
201-
*/
202-
abstract public function runAction($name, $action, $believable = false, $standAlone = false);
203-
204192
protected function afterRun()
205193
{
206194
}
207195

208196
/**
209197
* @param int $code
210198
*/
211-
public function stop($code = 0)
199+
public function stop(int $code = 0)
212200
{
213201
// call 'onAppStop' service, if it is registered.
214-
$this->fire(self::ON_STOP_RUN, [$this]);
202+
$this->fire(self::ON_STOP_RUN, $this);
215203

216204
// display runtime info
217205
if ($this->isProfile()) {
@@ -251,7 +239,7 @@ protected function runtimeCheck()
251239
{
252240
// check env
253241
if (!\in_array(PHP_SAPI, ['cli', 'cli-server'], true)) {
254-
header('HTTP/1.1 403 Forbidden');
242+
\header('HTTP/1.1 403 Forbidden');
255243
exit(" 403 Forbidden \n\n"
256244
. " current environment is CLI. \n"
257245
. " :( Sorry! Run this script is only allowed in the terminal environment!\n,You are not allowed to access this file.\n");

src/Base/ApplicationInterface.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ interface ApplicationInterface
2424
/**
2525
* @param bool $exit
2626
*/
27-
public function run($exit = true);
27+
public function run(bool $exit = true);
2828

29-
public function stop($code = 0);
29+
/**
30+
* @param int $code
31+
* @return mixed
32+
*/
33+
public function stop(int $code = 0);
3034

3135
/**
3236
* run a independent command
3337
* @param string $name
3438
* @param bool $believable
3539
* @return mixed
3640
*/
37-
public function runCommand($name, $believable = false);
41+
public function runCommand(string $name, bool $believable = false);
3842

3943
/**
4044
* run a controller's action
@@ -44,12 +48,12 @@ public function runCommand($name, $believable = false);
4448
* @param bool $standAlone
4549
* @return mixed
4650
*/
47-
public function runAction($name, $action, $believable = false, $standAlone = false);
51+
public function runAction(string $name, string $action, bool $believable = false, bool $standAlone = false);
4852

4953
/**
5054
* Register a app group command(by controller)
5155
* @param string $name The controller name
52-
* @param string $class The controller class
56+
* @param string|ControllerInterface $class The controller class
5357
* @param null|array|string $option
5458
* string: define the description message.
5559
* array:
@@ -58,7 +62,7 @@ public function runAction($name, $action, $believable = false, $standAlone = fal
5862
* @return static
5963
* @throws \InvalidArgumentException
6064
*/
61-
public function controller(string $name, string $class = null, $option = null);
65+
public function controller(string $name, $class = null, $option = null);
6266

6367
/**
6468
* Register a app independent console command

0 commit comments

Comments
 (0)