diff --git a/Console/AbstractConsole.php b/Console/AbstractConsole.php index 60d16c1b..d7f4fe47 100644 --- a/Console/AbstractConsole.php +++ b/Console/AbstractConsole.php @@ -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); } /** diff --git a/Console/Application.php b/Console/Application.php index 9ae008d4..6274e78b 100644 --- a/Console/Application.php +++ b/Console/Application.php @@ -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 diff --git a/Console/CommandInterface.php b/Console/CommandInterface.php index 4ae5341a..8f80073d 100644 --- a/Console/CommandInterface.php +++ b/Console/CommandInterface.php @@ -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); } diff --git a/Console/DefaultCommand.php b/Console/DefaultCommand.php index f45325e7..37382805 100644 --- a/Console/DefaultCommand.php +++ b/Console/DefaultCommand.php @@ -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'); diff --git a/Console/Dtl.php b/Console/Dtl.php deleted file mode 100644 index 715451c2..00000000 --- a/Console/Dtl.php +++ /dev/null @@ -1,131 +0,0 @@ -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); - } -} diff --git a/Console/ICommand.php b/Console/ICommand.php index b3775652..8f789b1c 100644 --- a/Console/ICommand.php +++ b/Console/ICommand.php @@ -4,9 +4,11 @@ namespace Console; +use Snowflake\Abstracts\Input; + interface ICommand { - public function handler(); + public function onHandler(Input $dtl); } diff --git a/HttpServer/Command.php b/HttpServer/Command.php index fc8e9d9b..38b34057 100644 --- a/HttpServer/Command.php +++ b/HttpServer/Command.php @@ -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'); } } diff --git a/System/Abstracts/Command.php b/System/Abstracts/Command.php new file mode 100644 index 00000000..7674c157 --- /dev/null +++ b/System/Abstracts/Command.php @@ -0,0 +1,10 @@ +_command = array_shift($parameters); + $this->_command = array_shift($parameters); foreach ($parameters as $parameter) { $explode = explode('=', $parameter); if (count($explode) < 2) { diff --git a/System/Application.php b/System/Application.php index c71c2f50..265eec01 100644 --- a/System/Application.php +++ b/System/Application.php @@ -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