This commit is contained in:
2020-09-07 18:11:36 +08:00
parent c5c716f6e5
commit 03ab3bb92f
10 changed files with 68 additions and 171 deletions
+5 -3
View File
@@ -5,6 +5,7 @@ namespace Console;
use Exception;
use Snowflake\Abstracts\Input;
use Snowflake\Snowflake;
use Swoole\Coroutine\Channel;
@@ -20,7 +21,7 @@ abstract class AbstractConsole
*/
public $commands = [];
/** @var Dtl $parameters */
/** @var Input $parameters */
private $parameters;
/** @var array */
@@ -47,10 +48,11 @@ abstract class AbstractConsole
/**
* @return $this
* @throws Exception
*/
public function setParameters()
{
$this->parameters = new Dtl($_SERVER['argv']);
$this->parameters = new Input($_SERVER['argv']);
return $this;
}
@@ -60,7 +62,7 @@ abstract class AbstractConsole
*/
public function execCommand(Command $command)
{
return $command->handler($this->parameters);
return $command->onHandler($this->parameters);
}
/**
+5 -11
View File
@@ -27,24 +27,18 @@ class Application extends Component
*/
public $id = 'uniqueId';
/** @var Console */
private $console;
/** @var Channel */
private $channel;
/**
* Application constructor.
* @param array $config
* @throws
*
*/
public function __construct(array $config = [])
public function init()
{
parent::__construct($config);
$this->channel = new Channel(1);
$this->console = new Console($config);
$this->console = new Console();
}
/**
* @param $class
* @throws
+3 -1
View File
@@ -3,6 +3,8 @@
namespace Console;
use Snowflake\Abstracts\Input;
/**
* Interface CommandInterface
* @package Console
@@ -10,6 +12,6 @@ namespace Console;
interface CommandInterface
{
public function handler(Dtl $dtl);
public function onHandler(Input $dtl);
}
+3 -1
View File
@@ -3,6 +3,8 @@
namespace Console;
use Snowflake\Abstracts\Input;
/**
* Class DefaultCommand
* @package Console
@@ -13,7 +15,7 @@ class DefaultCommand extends Command
public $description = 'help';
public function handler(Dtl $dtl)
public function onHandler(Input $dtl)
{
$param = $dtl->get('commandList');
-131
View File
@@ -1,131 +0,0 @@
<?php
namespace Console;
use HttpServer\Http\Context;
use HttpServer\Http\HttpHeaders;
use HttpServer\Http\HttpParams;
use HttpServer\Http\Request;
/**
* Class Dtl
* @package 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 HttpHeaders([]);
Context::setContext('request', $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);
}
}
+3 -1
View File
@@ -4,9 +4,11 @@
namespace Console;
use Snowflake\Abstracts\Input;
interface ICommand
{
public function handler();
public function onHandler(Input $dtl);
}
+17 -21
View File
@@ -4,7 +4,8 @@
namespace HttpServer;
use Console\Dtl;
use Exception;
use Snowflake\Abstracts\Input;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
@@ -22,33 +23,28 @@ class Command extends \Console\Command
public $description = 'server start|stop|reload|restart';
private $actions = [
'start', 'stop', 'reload', 'restart'
];
/**
* @param Dtl $dtl
* @throws ComponentException|ConfigException
* @throws \Exception
* @param Input $dtl
* @throws ConfigException
* @throws Exception
*/
public function handler(Dtl $dtl)
public function onHandler(Input $dtl)
{
$action = $dtl->get('action', 3);
/** @var Server $server */
$server = Snowflake::app()->get('server');
switch ($action) {
case 'restart':
$server->shutdown();
$server->start();
break;
$manager = Snowflake::app()->server;
$manager->setDaemon($dtl->get('daemon', 0));
switch ($dtl->get('action')) {
case 'stop':
$server->shutdown();
$manager->shutdown();
break;
case 'restart':
$manager->shutdown();
$manager->start();
break;
case 'start':
$manager->start();
break;
default:
$server->start();
$this->error('only start|stop|reload|restart');
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace Snowflake\Abstracts;
abstract class Command extends Component
{
}
+9 -2
View File
@@ -26,6 +26,14 @@ class Input
}
/**
* @return string
*/
public function getCommandName()
{
return '';
}
/**
* @param $key
@@ -49,7 +57,6 @@ class Input
}
/**
* @return false|string
*/
@@ -68,7 +75,7 @@ class Input
{
$arrays = [];
$parameters = array_slice($parameters, 1);
// $this->_command = array_shift($parameters);
$this->_command = array_shift($parameters);
foreach ($parameters as $parameter) {
$explode = explode('=', $parameter);
if (count($explode) < 2) {
+13
View File
@@ -18,6 +18,7 @@ use Snowflake\Abstracts\BaseApplication;
use Snowflake\Abstracts\Config;
use Snowflake\Abstracts\Input;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Exception\ComponentException;
/**
* Class Init
@@ -64,6 +65,18 @@ class Application extends BaseApplication
}
/**
* @param string $command
* @throws ComponentException
*/
public function command(string $command)
{
/** @var \Console\Application $abstracts */
$abstracts = $this->get('console');
$abstracts->register($command);
}
/**
* @param $argv
* @throws