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 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;
}
+15
View File
@@ -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);
}
}
+1 -1
View File
@@ -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]);
}
-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();
break;
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;
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);
}
}
+21 -17
View File
@@ -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;
}
}