e
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Console;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Swoole\Coroutine\Channel;
|
||||
|
||||
/**
|
||||
* Class AbstractConsole
|
||||
* @package BeReborn\Console
|
||||
*/
|
||||
abstract class AbstractConsole
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Command[]
|
||||
*/
|
||||
public $commands = [];
|
||||
|
||||
/** @var Dtl $parameters */
|
||||
private $parameters;
|
||||
|
||||
/** @var array */
|
||||
private $_config;
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* AbstractConsole constructor.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->_config = $config;
|
||||
$this->signCommand(\BeReborn::createObject(DefaultCommand::class));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getConfig()
|
||||
{
|
||||
return $this->_config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setParameters()
|
||||
{
|
||||
$this->parameters = new Dtl($_SERVER['argv']);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Command $command
|
||||
* @return mixed
|
||||
*/
|
||||
public function execCommand(Command $command)
|
||||
{
|
||||
return $command->handler($this->parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Command|null
|
||||
*/
|
||||
public function search()
|
||||
{
|
||||
$name = $this->parameters->getCommandName();
|
||||
$this->parameters->set('commandList', $this->getCommandList());
|
||||
foreach ($this->commands as $command) {
|
||||
if ($command->command != $name) {
|
||||
continue;
|
||||
}
|
||||
return $command;
|
||||
}
|
||||
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(\BeReborn::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()
|
||||
{
|
||||
$_tmp = [];
|
||||
foreach ($this->commands as $command) {
|
||||
$_tmp[$command->command] = [$command->description, $command];
|
||||
}
|
||||
ksort($_tmp, SORT_ASC);
|
||||
return $_tmp;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/10/7 0007
|
||||
* Time: 2:16
|
||||
*/
|
||||
|
||||
namespace BeReborn\Console;
|
||||
|
||||
|
||||
use BeReborn\Base\BaseApplication;
|
||||
use kafka\Protocol\SyncGroup;
|
||||
use Swoole\Coroutine\Channel;
|
||||
use Swoole\Runtime;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
* Class Application
|
||||
* @package BeReborn\Console
|
||||
*/
|
||||
class Application extends BaseApplication
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $id = 'uniqueId';
|
||||
|
||||
private $console;
|
||||
|
||||
/** @var Channel */
|
||||
private $channel;
|
||||
|
||||
/**
|
||||
* Application constructor.
|
||||
* @param array $config
|
||||
* @throws
|
||||
*/
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
parent::__construct($config);
|
||||
|
||||
$this->channel = new Channel(1);
|
||||
$this->console = new Console($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @throws
|
||||
*/
|
||||
public function register($class)
|
||||
{
|
||||
if (is_string($class) || is_callable($class, true)) {
|
||||
$class = \BeReborn::createObject($class);
|
||||
}
|
||||
$this->console->signCommand($class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param null $kernel
|
||||
* @return string|void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function run($kernel = null)
|
||||
{
|
||||
setCommand(true);
|
||||
try {
|
||||
$params = '';
|
||||
$kernel = \BeReborn::make($kernel, Kernel::class);
|
||||
|
||||
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
|
||||
|
||||
$this->console->setParameters();
|
||||
$this->console->batch($kernel);
|
||||
$class = $this->console->search();
|
||||
$params = response()->send($this->console->execCommand($class));
|
||||
} catch (\Exception $exception) {
|
||||
$params = response()->send(implode("\n", [
|
||||
'Msg: ' . $exception->getMessage(),
|
||||
'Line: ' . $exception->getLine(),
|
||||
'File: ' . $exception->getFile()
|
||||
]));
|
||||
} finally {
|
||||
Timer::clearAll();
|
||||
return $params;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Console;
|
||||
|
||||
use Snowflake\Abstracts\BaseObject;
|
||||
|
||||
/**
|
||||
* Class Command
|
||||
* @package BeReborn\Console
|
||||
*/
|
||||
abstract class Command extends BaseObject implements CommandInterface
|
||||
{
|
||||
|
||||
public $command = '';
|
||||
public $description = '';
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* 返回执行的命令名称
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->command;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*
|
||||
* 返回命令描述
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Console;
|
||||
|
||||
/**
|
||||
* Interface CommandInterface
|
||||
* @package BeReborn\Console
|
||||
*/
|
||||
interface CommandInterface
|
||||
{
|
||||
|
||||
public function handler(Dtl $dtl);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace BeReborn\Console;
|
||||
|
||||
/**
|
||||
* Class Console
|
||||
* @package BeReborn\Console
|
||||
*/
|
||||
class Console extends AbstractConsole
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace BeReborn\Console;
|
||||
|
||||
/**
|
||||
* Class DefaultCommand
|
||||
* @package BeReborn\Console
|
||||
*/
|
||||
class DefaultCommand extends Command
|
||||
{
|
||||
public $command = 'list';
|
||||
|
||||
public $description = 'help';
|
||||
|
||||
public function handler(Dtl $dtl)
|
||||
{
|
||||
$param = $dtl->get('commandList');
|
||||
|
||||
$last = '';
|
||||
$lists = [str_pad('Commands', 24, ' ', STR_PAD_RIGHT) . '注释'];
|
||||
foreach ($param as $key => $val) {
|
||||
$split = explode(':', $key);
|
||||
if (empty($last) && isset($split[0])) {
|
||||
$lists[] = str_pad("\033[32;40;1;1m" . $split[0] . " \033[0m", 40, ' ', STR_PAD_RIGHT);
|
||||
} else if (isset($split[0]) && $last != $split[0]) {
|
||||
$lists[] = str_pad("\033[32;40;1;1m" . $split[0] . " \033[0m", 40, ' ', STR_PAD_RIGHT);
|
||||
}
|
||||
|
||||
$last = $split[0] ?? '';
|
||||
|
||||
list($method, $ts) = $val;
|
||||
$lists[] = str_pad("\033[32;40;1;1m " . $key . " \033[0m", 40, ' ', STR_PAD_RIGHT) . $method;
|
||||
}
|
||||
return implode(PHP_EOL, $lists);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Snowflake\Console;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
use HttpServer\Http\HttpParams;
|
||||
use HttpServer\Http\Request;
|
||||
|
||||
/**
|
||||
* Class Dtl
|
||||
* @package BeReborn\Console
|
||||
*/
|
||||
class Dtl
|
||||
{
|
||||
|
||||
private $parameters;
|
||||
|
||||
private $command = '';
|
||||
|
||||
/**
|
||||
* Dtl constructor.
|
||||
* @param $parameters
|
||||
* @throws
|
||||
*/
|
||||
public function __construct($parameters)
|
||||
{
|
||||
$this->parameters = $this->resolve($parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCommandName()
|
||||
{
|
||||
return $this->command;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $parameters
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function resolve($parameters)
|
||||
{
|
||||
$arrays = [];
|
||||
$parameters = array_slice($parameters, 1);
|
||||
|
||||
$this->command = array_shift($parameters);
|
||||
foreach ($parameters as $parameter) {
|
||||
$explode = explode('=', $parameter);
|
||||
if (count($explode) < 2) {
|
||||
continue;
|
||||
}
|
||||
$arrays[array_shift($explode)] = current($explode);
|
||||
}
|
||||
|
||||
$request = new Request();
|
||||
$request->fd = 0;
|
||||
$request->params = new HttpParams([], $arrays, []);
|
||||
$request->headers = new HttpParams([]);
|
||||
|
||||
Context::setRequest($request);
|
||||
return $arrays;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param null $default
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
return $this->parameters[$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return $this
|
||||
*/
|
||||
public function set($key, $value)
|
||||
{
|
||||
$this->parameters[$key] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
* @return $this
|
||||
*/
|
||||
public function setBatch(array $array)
|
||||
{
|
||||
if (empty($array)) {
|
||||
return $this;
|
||||
}
|
||||
foreach ($array as $key => $value) {
|
||||
$this->parameters[$key] = $value;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* 获取
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
* 清理
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
return $this->parameters = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|string
|
||||
*/
|
||||
public function toJson()
|
||||
{
|
||||
return json_encode($this->parameters, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace BeReborn\Console;
|
||||
|
||||
|
||||
interface ICommand
|
||||
{
|
||||
|
||||
public function handler();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace BeReborn\Console;
|
||||
|
||||
|
||||
class Kernel
|
||||
{
|
||||
public $commands = [
|
||||
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user