diff --git a/Console/AbstractConsole.php b/Console/AbstractConsole.php index d7f4fe47..b0a33e0c 100644 --- a/Console/AbstractConsole.php +++ b/Console/AbstractConsole.php @@ -5,6 +5,7 @@ namespace Console; use Exception; +use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Input; use Snowflake\Snowflake; use Swoole\Coroutine\Channel; @@ -13,13 +14,15 @@ use Swoole\Coroutine\Channel; * Class AbstractConsole * @package Console */ -abstract class AbstractConsole +abstract class AbstractConsole extends Component { /** * @var Command[] */ - public $commands = []; + public $commands = [ + DefaultCommand::class + ]; /** @var Input $parameters */ private $parameters; @@ -36,23 +39,17 @@ abstract class AbstractConsole { $this->_config = $config; $this->signCommand(Snowflake::createObject(DefaultCommand::class)); + + parent::__construct($config); } /** - * @return array - */ - public function getConfig() - { - return $this->_config; - } - - /** + * @param Input $input * @return $this - * @throws Exception */ - public function setParameters() + public function setParameters(Input $input) { - $this->parameters = new Input($_SERVER['argv']); + $this->parameters = $input; return $this; } diff --git a/Console/Console.php b/Console/Console.php index d5b08f7a..3e5e76ee 100644 --- a/Console/Console.php +++ b/Console/Console.php @@ -3,6 +3,8 @@ namespace Console; +use Snowflake\Snowflake; + /** * Class Console * @package Console @@ -10,4 +12,17 @@ namespace Console; class Console extends AbstractConsole { + + /** + * @param $class + * @throws + */ + public function register($class) + { + if (is_string($class) || is_callable($class, true)) { + $class = Snowflake::createObject($class); + } + $this->signCommand($class); + } + } diff --git a/Console/ConsoleProviders.php b/Console/ConsoleProviders.php index 8ff89802..9bf851fb 100644 --- a/Console/ConsoleProviders.php +++ b/Console/ConsoleProviders.php @@ -20,7 +20,7 @@ class ConsoleProviders extends Providers */ public function onImport(Application $application) { - $application->set('console', ['class' => \Console\Application::class]); + $application->set('console', ['class' => Console::class]); } diff --git a/Console/Kernel.php b/Console/Kernel.php deleted file mode 100644 index 0db09134..00000000 --- a/Console/Kernel.php +++ /dev/null @@ -1,12 +0,0 @@ -start(); break; default: - $this->error('only start|stop|reload|restart'); + $this->error('I don\'t know what I want to do.'); } } diff --git a/HttpServer/ServerProviders.php b/HttpServer/ServerProviders.php index 9781d094..04f1aedd 100644 --- a/HttpServer/ServerProviders.php +++ b/HttpServer/ServerProviders.php @@ -4,6 +4,7 @@ namespace HttpServer; +use Console\Console; use Exception; use HttpServer\Server; use Snowflake\Abstracts\Component; @@ -28,8 +29,8 @@ class ServerProviders extends Providers { $application->set('server', ['class' => Server::class]); -// /** @var \Console\Application $console */ -// $console = $application->get('console'); -// $console->register(Command::class); + /** @var Console $console */ + $console = $application->get('console'); + $console->register(Command::class); } } diff --git a/System/Application.php b/System/Application.php index 51f9eae2..f14ec483 100644 --- a/System/Application.php +++ b/System/Application.php @@ -9,7 +9,9 @@ namespace Snowflake; +use Console\Console; use Console\ConsoleProviders; +use Console\Kernel; use Database\DatabasesProviders; use Exception; use HttpServer\Server; @@ -19,6 +21,8 @@ use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Input; use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\ComponentException; +use Swoole\Runtime; +use Swoole\Timer; /** * Class Init @@ -69,9 +73,9 @@ class Application extends BaseApplication * @param string $command * @throws ComponentException */ - public function command(string $command) + public function register(string $command) { - /** @var \Console\Application $abstracts */ + /** @var Console $abstracts */ $abstracts = $this->get('console'); $abstracts->register($command); } @@ -79,26 +83,26 @@ class Application extends BaseApplication /** * @param $argv + * @return bool|string * @throws */ public function start(Input $argv) { $this->set('input', $argv); - $manager = Snowflake::app()->server; - $manager->setDaemon($argv->get('daemon', 0)); - switch ($argv->get('action')) { - case 'stop': - $manager->shutdown(); - break; - case 'restart': - $manager->shutdown(); - $manager->start(); - break; - case 'start': - $manager->start(); - break; - default: - $this->error('I don\'t know what I want to do.'); + try { + $manager = Snowflake::app()->get('console'); + $manager->setParameters(); + $class = $manager->search(); + $params = response()->send($manager->execCommand($class)); + } catch (\Exception $exception) { + $params = response()->send(implode("\n", [ + 'Msg: ' . $exception->getMessage(), + 'Line: ' . $exception->getLine(), + 'File: ' . $exception->getFile() + ])); + } finally { + Timer::clearAll(); + return $params; } }