This commit is contained in:
2021-09-18 17:23:30 +08:00
parent a6b95adc9b
commit 8b943076a1
7 changed files with 0 additions and 377 deletions
-156
View File
@@ -1,156 +0,0 @@
<?php
declare(strict_types=1);
namespace Console;
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Input;
use Kiri\Kiri;
use Server\Events\OnAfterCommandExecute;
use Server\Events\OnBeforeCommandExecute;
/**
* Class AbstractConsole
* @package Console
*/
abstract class AbstractConsole extends Component
{
/**
* @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(Kiri::createObject(DefaultCommand::class));
parent::__construct($config);
}
/**
* @param Input $input
* @return $this
*/
public function setParameters(Input $input): static
{
$this->parameters = $input;
return $this;
}
/**
* @param Command $command
* @return mixed
* @throws Exception
*/
public function exec(Command $command): mixed
{
fire(new OnBeforeCommandExecute());
$data = $command->onHandler($this->parameters);
fire(new OnAfterCommandExecute($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(Kiri::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;
}
}
-87
View File
@@ -1,87 +0,0 @@
<?php
declare(strict_types=1);
namespace Console;
use Exception;
use ReflectionException;
use Kiri\Abstracts\BaseObject;
use Kiri\Abstracts\TraitApplication;
use Kiri\Exception\ComponentException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
/**
* Class Command
* @package Console
*/
abstract class Command extends BaseObject implements CommandInterface
{
use TraitApplication;
public string $command = '';
public string $description = '';
/**
* @return string
* 返回执行的命令名称
*/
public function getName(): string
{
return $this->command;
}
/**
* @return string
*
* 返回命令描述
*/
public function getDescription(): string
{
return $this->description;
}
/**
* @param $name
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public function __get($name): mixed
{
if ($this->has($name)) {
return $this->get($name);
}
return parent::__get($name); // TODO: Change the autogenerated stub
}
/**
* @param $name
* @return bool
* @throws
*/
private function has($name): bool
{
return Kiri::app()->has($name);
}
/**
* @param $name
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
*/
private function get($name): mixed
{
return Kiri::app()->get($name);
}
}
-18
View File
@@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
namespace Console;
use Kiri\Abstracts\Input;
/**
* Interface CommandInterface
* @package Console
*/
interface CommandInterface
{
public function onHandler(Input $dtl);
}
-29
View File
@@ -1,29 +0,0 @@
<?php
declare(strict_types=1);
namespace Console;
use Kiri\Kiri;
/**
* Class Console
* @package Console
*/
class Console extends AbstractConsole
{
/**
* @param $class
* @throws
*/
public function register($class)
{
if (is_string($class) || is_callable($class, true)) {
$class = Kiri::createObject($class);
}
$this->signCommand($class);
}
}
-28
View File
@@ -1,28 +0,0 @@
<?php
declare(strict_types=1);
namespace Console;
use Exception;
use Kiri\Abstracts\Providers;
use Kiri\Application;
/**
* Class ConsoleProviders
* @package Console
*/
class ConsoleProviders extends Providers
{
/**
* @param Application $application
* @throws Exception
*/
public function onImport(Application $application)
{
/** @var Console $console */
$application->set('console', ['class' => Console::class]);
}
}
-45
View File
@@ -1,45 +0,0 @@
<?php
declare(strict_types=1);
namespace Console;
use Kiri\Abstracts\Input;
/**
* Class DefaultCommand
* @package Console
*/
class DefaultCommand extends Command
{
public string $command = 'system:help';
public string $description = 'command lists';
/**
* @param Input $dtl
* @return string
*/
public function onHandler(Input $dtl): string
{
$param = $dtl->get('commandList');
$last = '';
$lists = ["Commands\t" . '注释'];
foreach ($param as $key => $val) {
$split = explode(':', $key);
if (empty($last) && isset($split[0])) {
$lists[] = "\033[32;40;1;1m" . $split[0] . " \033[0m\t";
} else if (isset($split[0]) && $last != $split[0]) {
$lists[] = "\033[32;40;1;1m" . $split[0] . " \033[0m\t";
}
$last = $split[0] ?? '';
list($method, $ts) = $val;
$lists[] = "\033[32;40;1;1m " . $key . " \033[0m\t\t" . $method."\t";
}
return implode(PHP_EOL, $lists);
}
}
-14
View File
@@ -1,14 +0,0 @@
<?php
declare(strict_types=1);
namespace Console;
use Kiri\Abstracts\Input;
interface ICommand
{
public function onHandler(Input $dtl);
}