From 09c82b810e895aff3b1cafdb88f7470692fcd97f Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Fri, 9 Apr 2021 02:15:45 +0800 Subject: [PATCH] modify --- Console/AbstractConsole.php | 234 ++++++++++++++++++------------------ 1 file changed, 119 insertions(+), 115 deletions(-) diff --git a/Console/AbstractConsole.php b/Console/AbstractConsole.php index 1b340e18..027351fc 100644 --- a/Console/AbstractConsole.php +++ b/Console/AbstractConsole.php @@ -17,134 +17,138 @@ use Snowflake\Snowflake; abstract class AbstractConsole extends Component { - /** - * @var Command[] - */ - public array $commands = []; + /** + * @var Command[] + */ + public array $commands = []; - /** @var Input $parameters */ - private Input $parameters; + /** @var Input $parameters */ + private Input $parameters; - /** @var array */ - private array $_config; + /** @var array */ + private array $_config; - /** - * @param array $config - * AbstractConsole constructor. - * @throws Exception - */ - public function __construct(array $config = []) - { - $this->_config = $config; - $this->signCommand(Snowflake::createObject(DefaultCommand::class)); + /** + * @param array $config + * AbstractConsole constructor. + * @throws Exception + */ + public function __construct(array $config = []) + { + $this->_config = $config; + $this->signCommand(Snowflake::createObject(DefaultCommand::class)); - parent::__construct($config); - } + parent::__construct($config); + } - /** - * @param Input $input - * @return $this - */ - public function setParameters(Input $input): static - { - $this->parameters = $input; - return $this; - } + /** + * @param Input $input + * @return $this + */ + public function setParameters(Input $input): static + { + $this->parameters = $input; + return $this; + } - /** - * @param Command $command - * @return mixed - */ - public function execCommand(Command $command): mixed - { - fire(Event::SERVER_BEFORE_START); + /** + * @param Command $command + * @return mixed + */ + public function execCommand(Command $command): mixed + { + fire(Event::BEFORE_COMMAND_EXECUTE); - return $command->onHandler($this->parameters); - } + $data = $command->onHandler($this->parameters); - /** - * @return Command|null - */ - public function search(): ?Command - { - $name = $this->parameters->getCommandName(); - $this->parameters->set('commandList', $this->getCommandList()); + fire(Event::AFTER_COMMAND_EXECUTE, [$data]); - $help = 'system:help'; - foreach ($this->commands as $command) { - if ($command->command == $help) { - $help = $command; - } - if ($command->command != $name) { - continue; - } - return $command; - } - if (is_object($help)) { - return $help; - } - return null; - } + return $data; + } - /** - * @param Command $abstractConsole - * - * 注册命令 - */ - public function signCommand(Command $abstractConsole) - { - $this->commands[] = $abstractConsole; - } + /** + * @return Command|null + */ + public function search(): ?Command + { + $name = $this->parameters->getCommandName(); + $this->parameters->set('commandList', $this->getCommandList()); - /** - * @param $kernel - * @throws Exception - */ - public function batch($kernel) - { - if (is_object($kernel)) { - if (!property_exists($kernel, 'commands')) { - return; - } - $kernel = $kernel->commands; - } - if (!is_array($kernel)) { - return; - } - foreach ($kernel as $command) { - $this->signCommand(Snowflake::createObject($command)); - } - } + $help = 'system:help'; + foreach ($this->commands as $command) { + if ($command->command == $help) { + $help = $command; + } + if ($command->command != $name) { + continue; + } + return $command; + } + if (is_object($help)) { + return $help; + } + return null; + } - /** - * @param Command $abstractConsole - * 释放一个命令 - */ - public function destroyCommand(Command $abstractConsole) - { - foreach ($this->commands as $index => $command) { - if ($abstractConsole === $command) { - unset($this->commands[$index]); - break; - } - } - } + /** + * @param Command $abstractConsole + * + * 注册命令 + */ + public function signCommand(Command $abstractConsole) + { + $this->commands[] = $abstractConsole; + } - /** - * @return array - */ - private function getCommandList(): array - { - $_tmp = []; - foreach ($this->commands as $command) { - if ($command->command === 'system:help') { - continue; - } - $_tmp[$command->command] = [$command->description, $command]; - } - ksort($_tmp, SORT_ASC); - return $_tmp; - } + /** + * @param $kernel + * @throws Exception + */ + public function batch($kernel) + { + if (is_object($kernel)) { + if (!property_exists($kernel, 'commands')) { + return; + } + $kernel = $kernel->commands; + } + if (!is_array($kernel)) { + return; + } + foreach ($kernel as $command) { + $this->signCommand(Snowflake::createObject($command)); + } + } + + /** + * @param Command $abstractConsole + * 释放一个命令 + */ + public function destroyCommand(Command $abstractConsole) + { + foreach ($this->commands as $index => $command) { + if ($abstractConsole === $command) { + unset($this->commands[$index]); + break; + } + } + } + + /** + * @return array + */ + private function getCommandList(): array + { + $_tmp = []; + foreach ($this->commands as $command) { + if ($command->command === 'system:help') { + continue; + } + $_tmp[$command->command] = [$command->description, $command]; + } + ksort($_tmp, SORT_ASC); + return $_tmp; + } }