介紹
所有command類都實現(xiàn)了CommandInterface接口
interface CommandInterface
{
public function commandName():string;
public function exec(array $args):?string ;
public function help(array $args):?string ;
}
分析
CommandRunner類里面當輸入不存在的command時,會默認為help
function run(array $args):?string
{
···
if(!CommandContainer::getInstance()->get($command)){
$command = 'help';
}
···
}
help類
class Help implements CommandInterface
{
public function commandName(): string
{
// TODO: Implement commandName() method.
return 'help';
}
public function exec(array $args): ?string
{
// TODO: Implement exec() method.
// 如果沒有任何command,則直接調(diào)用本類的help方法
if (!isset($args[0])) {
return $this->help($args);
} else {
$actionName = $args[0];
array_shift($args);
// 獲取相應命令的對象
$call = CommandContainer::getInstance()->get($actionName);
// 是否實現(xiàn)了CommandInterface接口
if ($call instanceof CommandInterface) {
// 執(zhí)行相應command類的help方法
return $call->help($args);
} else {
return "no help message for command {$actionName} was found";
}
}
}
public function help(array $args): ?string
{
// TODO: Implement help() method.
// 獲取所有command
$allCommand = implode(PHP_EOL, CommandContainer::getInstance()->getCommandList());
// 展示es log
$logo = Utility::easySwooleLog();
return $logo.<<<HELP
Welcome To EASYSWOOLE Command Console!
Usage: php easyswoole [command] [arg]
Get help : php easyswoole help [command]
Current Register Command:
{$allCommand}
HELP;
}
}