2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2020-09-03 00:15:57 +08:00
|
|
|
namespace Console;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2020-09-08 10:55:06 +08:00
|
|
|
use Snowflake\Abstracts\Component;
|
2020-09-07 18:11:36 +08:00
|
|
|
use Snowflake\Abstracts\Input;
|
2021-04-09 02:12:54 +08:00
|
|
|
use Snowflake\Event;
|
2020-08-31 12:38:32 +08:00
|
|
|
use Snowflake\Snowflake;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class AbstractConsole
|
2020-09-03 00:15:57 +08:00
|
|
|
* @package Console
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2020-09-08 10:55:06 +08:00
|
|
|
abstract class AbstractConsole extends Component
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
|
2021-04-09 02:15:45 +08:00
|
|
|
/**
|
|
|
|
|
* @var Command[]
|
|
|
|
|
*/
|
|
|
|
|
public array $commands = [];
|
|
|
|
|
|
|
|
|
|
/** @var Input $parameters */
|
|
|
|
|
private Input $parameters;
|
|
|
|
|
|
|
|
|
|
/** @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));
|
|
|
|
|
|
|
|
|
|
parent::__construct($config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Input $input
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function setParameters(Input $input): static
|
|
|
|
|
{
|
|
|
|
|
$this->parameters = $input;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 11:02:11 +08:00
|
|
|
/**
|
|
|
|
|
* @param Command $command
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-04-09 02:15:45 +08:00
|
|
|
public function execCommand(Command $command): mixed
|
|
|
|
|
{
|
|
|
|
|
fire(Event::BEFORE_COMMAND_EXECUTE);
|
|
|
|
|
|
|
|
|
|
$data = $command->onHandler($this->parameters);
|
|
|
|
|
|
|
|
|
|
fire(Event::AFTER_COMMAND_EXECUTE, [$data]);
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Command|null
|
|
|
|
|
*/
|
|
|
|
|
public function search(): ?Command
|
|
|
|
|
{
|
|
|
|
|
$name = $this->parameters->getCommandName();
|
|
|
|
|
$this->parameters->set('commandList', $this->getCommandList());
|
|
|
|
|
|
|
|
|
|
$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 signCommand(Command $abstractConsole)
|
|
|
|
|
{
|
|
|
|
|
$this->commands[] = $abstractConsole;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|