This commit is contained in:
2020-09-08 10:55:06 +08:00
parent a78d5c14fe
commit 37d29d5b86
7 changed files with 52 additions and 47 deletions
+10 -13
View File
@@ -5,6 +5,7 @@ namespace Console;
use Exception; use Exception;
use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Input; use Snowflake\Abstracts\Input;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine\Channel; use Swoole\Coroutine\Channel;
@@ -13,13 +14,15 @@ use Swoole\Coroutine\Channel;
* Class AbstractConsole * Class AbstractConsole
* @package Console * @package Console
*/ */
abstract class AbstractConsole abstract class AbstractConsole extends Component
{ {
/** /**
* @var Command[] * @var Command[]
*/ */
public $commands = []; public $commands = [
DefaultCommand::class
];
/** @var Input $parameters */ /** @var Input $parameters */
private $parameters; private $parameters;
@@ -36,23 +39,17 @@ abstract class AbstractConsole
{ {
$this->_config = $config; $this->_config = $config;
$this->signCommand(Snowflake::createObject(DefaultCommand::class)); $this->signCommand(Snowflake::createObject(DefaultCommand::class));
parent::__construct($config);
} }
/** /**
* @return array * @param Input $input
*/
public function getConfig()
{
return $this->_config;
}
/**
* @return $this * @return $this
* @throws Exception
*/ */
public function setParameters() public function setParameters(Input $input)
{ {
$this->parameters = new Input($_SERVER['argv']); $this->parameters = $input;
return $this; return $this;
} }
+15
View File
@@ -3,6 +3,8 @@
namespace Console; namespace Console;
use Snowflake\Snowflake;
/** /**
* Class Console * Class Console
* @package Console * @package Console
@@ -10,4 +12,17 @@ namespace Console;
class Console extends AbstractConsole 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);
}
} }
+1 -1
View File
@@ -20,7 +20,7 @@ class ConsoleProviders extends Providers
*/ */
public function onImport(Application $application) public function onImport(Application $application)
{ {
$application->set('console', ['class' => \Console\Application::class]); $application->set('console', ['class' => Console::class]);
} }
-12
View File
@@ -1,12 +0,0 @@
<?php
namespace Console;
class Kernel
{
public $commands = [
];
}
+1 -1
View File
@@ -44,7 +44,7 @@ class Command extends \Console\Command
$manager->start(); $manager->start();
break; break;
default: default:
$this->error('only start|stop|reload|restart'); $this->error('I don\'t know what I want to do.');
} }
} }
+4 -3
View File
@@ -4,6 +4,7 @@
namespace HttpServer; namespace HttpServer;
use Console\Console;
use Exception; use Exception;
use HttpServer\Server; use HttpServer\Server;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
@@ -28,8 +29,8 @@ class ServerProviders extends Providers
{ {
$application->set('server', ['class' => Server::class]); $application->set('server', ['class' => Server::class]);
// /** @var \Console\Application $console */ /** @var Console $console */
// $console = $application->get('console'); $console = $application->get('console');
// $console->register(Command::class); $console->register(Command::class);
} }
} }
+21 -17
View File
@@ -9,7 +9,9 @@
namespace Snowflake; namespace Snowflake;
use Console\Console;
use Console\ConsoleProviders; use Console\ConsoleProviders;
use Console\Kernel;
use Database\DatabasesProviders; use Database\DatabasesProviders;
use Exception; use Exception;
use HttpServer\Server; use HttpServer\Server;
@@ -19,6 +21,8 @@ use Snowflake\Abstracts\Config;
use Snowflake\Abstracts\Input; use Snowflake\Abstracts\Input;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Swoole\Runtime;
use Swoole\Timer;
/** /**
* Class Init * Class Init
@@ -69,9 +73,9 @@ class Application extends BaseApplication
* @param string $command * @param string $command
* @throws ComponentException * @throws ComponentException
*/ */
public function command(string $command) public function register(string $command)
{ {
/** @var \Console\Application $abstracts */ /** @var Console $abstracts */
$abstracts = $this->get('console'); $abstracts = $this->get('console');
$abstracts->register($command); $abstracts->register($command);
} }
@@ -79,26 +83,26 @@ class Application extends BaseApplication
/** /**
* @param $argv * @param $argv
* @return bool|string
* @throws * @throws
*/ */
public function start(Input $argv) public function start(Input $argv)
{ {
$this->set('input', $argv); $this->set('input', $argv);
$manager = Snowflake::app()->server; try {
$manager->setDaemon($argv->get('daemon', 0)); $manager = Snowflake::app()->get('console');
switch ($argv->get('action')) { $manager->setParameters();
case 'stop': $class = $manager->search();
$manager->shutdown(); $params = response()->send($manager->execCommand($class));
break; } catch (\Exception $exception) {
case 'restart': $params = response()->send(implode("\n", [
$manager->shutdown(); 'Msg: ' . $exception->getMessage(),
$manager->start(); 'Line: ' . $exception->getLine(),
break; 'File: ' . $exception->getFile()
case 'start': ]));
$manager->start(); } finally {
break; Timer::clearAll();
default: return $params;
$this->error('I don\'t know what I want to do.');
} }
} }