modify
This commit is contained in:
+119
-115
@@ -17,134 +17,138 @@ use Snowflake\Snowflake;
|
|||||||
abstract class AbstractConsole extends Component
|
abstract class AbstractConsole extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Command[]
|
* @var Command[]
|
||||||
*/
|
*/
|
||||||
public array $commands = [];
|
public array $commands = [];
|
||||||
|
|
||||||
/** @var Input $parameters */
|
/** @var Input $parameters */
|
||||||
private Input $parameters;
|
private Input $parameters;
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private array $_config;
|
private array $_config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* AbstractConsole constructor.
|
* AbstractConsole constructor.
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(array $config = [])
|
public function __construct(array $config = [])
|
||||||
{
|
{
|
||||||
$this->_config = $config;
|
$this->_config = $config;
|
||||||
$this->signCommand(Snowflake::createObject(DefaultCommand::class));
|
$this->signCommand(Snowflake::createObject(DefaultCommand::class));
|
||||||
|
|
||||||
parent::__construct($config);
|
parent::__construct($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Input $input
|
* @param Input $input
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setParameters(Input $input): static
|
public function setParameters(Input $input): static
|
||||||
{
|
{
|
||||||
$this->parameters = $input;
|
$this->parameters = $input;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Command $command
|
* @param Command $command
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function execCommand(Command $command): mixed
|
public function execCommand(Command $command): mixed
|
||||||
{
|
{
|
||||||
fire(Event::SERVER_BEFORE_START);
|
fire(Event::BEFORE_COMMAND_EXECUTE);
|
||||||
|
|
||||||
return $command->onHandler($this->parameters);
|
$data = $command->onHandler($this->parameters);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
fire(Event::AFTER_COMMAND_EXECUTE, [$data]);
|
||||||
* @return Command|null
|
|
||||||
*/
|
|
||||||
public function search(): ?Command
|
|
||||||
{
|
|
||||||
$name = $this->parameters->getCommandName();
|
|
||||||
$this->parameters->set('commandList', $this->getCommandList());
|
|
||||||
|
|
||||||
$help = 'system:help';
|
return $data;
|
||||||
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
|
* @return Command|null
|
||||||
*
|
*/
|
||||||
* 注册命令
|
public function search(): ?Command
|
||||||
*/
|
{
|
||||||
public function signCommand(Command $abstractConsole)
|
$name = $this->parameters->getCommandName();
|
||||||
{
|
$this->parameters->set('commandList', $this->getCommandList());
|
||||||
$this->commands[] = $abstractConsole;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
$help = 'system:help';
|
||||||
* @param $kernel
|
foreach ($this->commands as $command) {
|
||||||
* @throws Exception
|
if ($command->command == $help) {
|
||||||
*/
|
$help = $command;
|
||||||
public function batch($kernel)
|
}
|
||||||
{
|
if ($command->command != $name) {
|
||||||
if (is_object($kernel)) {
|
continue;
|
||||||
if (!property_exists($kernel, 'commands')) {
|
}
|
||||||
return;
|
return $command;
|
||||||
}
|
}
|
||||||
$kernel = $kernel->commands;
|
if (is_object($help)) {
|
||||||
}
|
return $help;
|
||||||
if (!is_array($kernel)) {
|
}
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
foreach ($kernel as $command) {
|
|
||||||
$this->signCommand(Snowflake::createObject($command));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Command $abstractConsole
|
* @param Command $abstractConsole
|
||||||
* 释放一个命令
|
*
|
||||||
*/
|
* 注册命令
|
||||||
public function destroyCommand(Command $abstractConsole)
|
*/
|
||||||
{
|
public function signCommand(Command $abstractConsole)
|
||||||
foreach ($this->commands as $index => $command) {
|
{
|
||||||
if ($abstractConsole === $command) {
|
$this->commands[] = $abstractConsole;
|
||||||
unset($this->commands[$index]);
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @param $kernel
|
||||||
*/
|
* @throws Exception
|
||||||
private function getCommandList(): array
|
*/
|
||||||
{
|
public function batch($kernel)
|
||||||
$_tmp = [];
|
{
|
||||||
foreach ($this->commands as $command) {
|
if (is_object($kernel)) {
|
||||||
if ($command->command === 'system:help') {
|
if (!property_exists($kernel, 'commands')) {
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
$_tmp[$command->command] = [$command->description, $command];
|
$kernel = $kernel->commands;
|
||||||
}
|
}
|
||||||
ksort($_tmp, SORT_ASC);
|
if (!is_array($kernel)) {
|
||||||
return $_tmp;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user