Skip to content

Commit b178954

Browse files
committed
add new features:
- allow define alias by aliases() - support run command by coroutine, base on swoole.
1 parent bab294c commit b178954

File tree

7 files changed

+134
-12
lines changed

7 files changed

+134
-12
lines changed

examples/Commands/CorCommand.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018-01-26
6+
* Time: 17:47
7+
*/
8+
9+
namespace Inhere\Console\Examples\Commands;
10+
11+
use Inhere\Console\Command;
12+
use Inhere\Console\IO\Input;
13+
use Inhere\Console\IO\Output;
14+
use Inhere\Console\Utils\Helper;
15+
16+
/**
17+
* Class CorCommand
18+
* @package Inhere\Console\Examples\Commands
19+
*/
20+
class CorCommand extends Command
21+
{
22+
protected static $name = 'cor';
23+
protected static $description = 'a coroutine test command';
24+
protected static $coroutine = true;
25+
26+
/**
27+
* @return array
28+
*/
29+
public static function aliases(): array
30+
{
31+
return ['coro'];
32+
}
33+
34+
/**
35+
* do execute
36+
* @param Input $input
37+
* @param Output $output
38+
*/
39+
protected function execute($input, $output)
40+
{
41+
$output->dump(Helper::isSupportCoroutine(), Helper::inCoroutine());
42+
}
43+
}

examples/Controllers/HomeController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Inhere\Console\Components\Style\LiteStyle;
1212
use Inhere\Console\Utils\Helper;
1313
use Inhere\Console\Utils\Interact;
14-
use Inhere\Console\Utils\ProcessUtil;
1514
use Inhere\Console\Utils\ProgressBar;
1615
use Inhere\Console\Utils\Show;
1716

examples/commands.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Inhere\Console\BuiltIn\PharController;
1111
use Inhere\Console\BuiltIn\SelfUpdateCommand;
12+
use Inhere\Console\Examples\Commands\CorCommand;
1213
use Inhere\Console\Examples\Commands\DemoCommand;
1314
use Inhere\Console\Examples\Commands\TestCommand;
1415
use Inhere\Console\Examples\Controllers\HomeController;
@@ -31,7 +32,7 @@
3132
'aliases' => ['selfUpdate']
3233
]);
3334

34-
// $app->controller(PharController::class);
35+
$app->command(CorCommand::class);
3536

3637
$app->controller('home', HomeController::class, [
3738
'aliases' => ['h']

src/Application.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public function controller(string $name, string $class = null, $option = null)
5858
return $this;
5959
}
6060

61+
// allow define aliases in Command class by Controller::aliases()
62+
if ($aliases = $class::aliases()) {
63+
$option['aliases'] = isset($option['aliases']) ? array_merge($option['aliases'], $aliases) : $aliases;
64+
}
65+
6166
$this->controllers[$name] = $class;
6267

6368
if (!$option) {
@@ -134,14 +139,18 @@ public function command(string $name, $handler = null, $option = null)
134139
if (!$handler::isEnabled()) {
135140
return $this;
136141
}
142+
143+
// allow define aliases in Command class by Command::aliases()
144+
if ($aliases = $handler::aliases()) {
145+
$option['aliases'] = isset($option['aliases']) ? array_merge($option['aliases'], $aliases) : $aliases;
146+
}
137147
} elseif (!\is_object($handler) || !method_exists($handler, '__invoke')) {
138148
throw new \InvalidArgumentException(sprintf(
139149
'The console command handler must is an subclass of %s OR a Closure OR a object have method __invoke()',
140150
Command::class
141151
));
142152
}
143153

144-
145154
// is an class name string
146155
$this->commands[$name] = $handler;
147156

src/Base/AbstractCommand.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Inhere\Console\Traits\UserInteractAwareTrait;
1717
use Inhere\Console\Utils\Annotation;
1818
use Inhere\Console\Utils\FormatUtil;
19+
use Inhere\Console\Utils\Helper;
20+
use Swoole\Coroutine;
1921

2022
/**
2123
* Class AbstractCommand
@@ -38,6 +40,11 @@ abstract class AbstractCommand implements BaseCommandInterface
3840
*/
3941
protected static $description = '';
4042

43+
/**
44+
* @var bool Whether enable coroutine. It is require swoole extension.
45+
*/
46+
protected static $coroutine = false;
47+
4148
/**
4249
* Allow display message tags in the command annotation
4350
* @var array
@@ -72,6 +79,16 @@ public static function isEnabled(): bool
7279
return true;
7380
}
7481

82+
/**
83+
* Setting current command/group name aliases
84+
* @return string[]
85+
*/
86+
public static function aliases(): array
87+
{
88+
// return ['alias1', 'alias2'];
89+
return [];
90+
}
91+
7592
/**
7693
* Command constructor.
7794
* @param Input $input
@@ -150,7 +167,6 @@ public function run(string $command = ''): int
150167

151168
if ($this->input->sameOpt(['h', 'help'])) {
152169
$this->showHelp();
153-
154170
return 0;
155171
}
156172

@@ -159,12 +175,29 @@ public function run(string $command = ''): int
159175
return -1;
160176
}
161177

162-
if (true !== $this->beforeExecute()) {
178+
// return False to deny go on
179+
if (false === $this->beforeExecute()) {
163180
return -1;
164181
}
165182

166-
$status = (int)$this->execute($this->input, $this->output);
167-
$this->afterExecute();
183+
$ok = false;
184+
$status = 0;
185+
186+
// if enable coroutine
187+
if (self::isCoroutine() && Helper::isSupportCoroutine()) {
188+
$ok = Coroutine::create(function () {
189+
$status = (int)$this->execute($this->input, $this->output);
190+
191+
$this->afterExecute();
192+
$this->getApp()->stop($status);
193+
});
194+
}
195+
196+
// when not enable coroutine OR coroutine create fail.
197+
if (!$ok){
198+
$status = (int)$this->execute($this->input, $this->output);
199+
$this->afterExecute();
200+
}
168201

169202
return $status;
170203
}
@@ -501,6 +534,22 @@ public static function setDescription(string $description)
501534
static::$description = $description;
502535
}
503536

537+
/**
538+
* @return bool
539+
*/
540+
public static function isCoroutine(): bool
541+
{
542+
return self::$coroutine;
543+
}
544+
545+
/**
546+
* @param bool $coroutine
547+
*/
548+
public static function setCoroutine($coroutine)
549+
{
550+
self::$coroutine = (bool)$coroutine;
551+
}
552+
504553
/**
505554
* @return array
506555
*/

src/Controller.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
abstract class Controller extends AbstractCommand implements ControllerInterface
2424
{
2525
/** @var array */
26-
private static $aliases;
26+
private static $commandAliases;
2727

2828
/** @var string */
2929
private $action;
@@ -291,15 +291,15 @@ protected function getRealCommandName(string $name)
291291
*/
292292
public static function getCommandAliases(string $name = null): array
293293
{
294-
if (null === self::$aliases) {
295-
self::$aliases = static::commandAliases();
294+
if (null === self::$commandAliases) {
295+
self::$commandAliases = static::commandAliases();
296296
}
297297

298298
if ($name) {
299-
return self::$aliases ? array_keys(self::$aliases, $name, true) : [];
299+
return self::$commandAliases ? array_keys(self::$commandAliases, $name, true) : [];
300300
}
301301

302-
return self::$aliases;
302+
return self::$commandAliases;
303303
}
304304

305305
/**

src/Utils/Helper.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Inhere\Console\Utils;
1212

1313
use Inhere\Console\Traits\RuntimeProfileTrait;
14+
use Swoole\Coroutine;
1415

1516
/**
1617
* Class Helper
@@ -67,6 +68,26 @@ public static function isRoot(): bool
6768
return getmyuid() === 0;
6869
}
6970

71+
/**
72+
* @return bool
73+
*/
74+
public static function isSupportCoroutine(): bool
75+
{
76+
return class_exists(Coroutine::class, false);
77+
}
78+
79+
/**
80+
* @return bool
81+
*/
82+
public static function inCoroutine(): bool
83+
{
84+
if (self::isSupportCoroutine()) {
85+
return Coroutine::getuid() > 0;
86+
}
87+
88+
return false;
89+
}
90+
7091
/**
7192
* @return bool
7293
*/

0 commit comments

Comments
 (0)