This commit is contained in:
2020-09-03 00:15:57 +08:00
parent 3886a01549
commit 7428b69c36
19 changed files with 132 additions and 80 deletions
-13
View File
@@ -25,7 +25,6 @@ use Snowflake\Exception\InitException;
use Snowflake\Jwt\Jwt;
use Snowflake\Pool\Connection;
use Snowflake\Pool\RedisClient;
use Snowflake\Processes;
use Snowflake\Snowflake;
use Snowflake\Event;
use Snowflake\Pool\Pool as SPool;
@@ -38,7 +37,6 @@ use Database\DatabasesProviders;
* @property Annotation $annotation
* @property Event $event
* @property Router $router
* @property Processes $processes
* @property SPool $pool
* @property Server $server
* @property DatabasesProviders $db
@@ -239,16 +237,6 @@ abstract class BaseApplication extends Service
}
/**
* @return Processes
* @throws ComponentException
*/
public function getProcesses()
{
return $this->get('processes');
}
/**
* @return Annotation
* @throws ComponentException
@@ -346,7 +334,6 @@ abstract class BaseApplication extends Service
'error' => ['class' => ErrorHandler::class],
'event' => ['class' => Event::class],
'annotation' => ['class' => Annotation::class],
'processes' => ['class' => Processes::class],
'connections' => ['class' => Connection::class],
'redis_connections' => ['class' => RedisClient::class],
'pool' => ['class' => SPool::class],
+14
View File
@@ -0,0 +1,14 @@
<?php
namespace Snowflake\Abstracts;
use Snowflake\Application;
interface Provider
{
public function onImport(Application $application);
}
+1 -1
View File
@@ -10,7 +10,7 @@ use Snowflake\Application;
* Class Providers
* @package Snowflake\Abstracts
*/
abstract class Providers extends Component
abstract class Providers extends Component implements Provider
{
+6 -3
View File
@@ -9,8 +9,10 @@
namespace Snowflake;
use Console\ConsoleProviders;
use Database\DatabasesProviders;
use Exception;
use HttpServer\Server;
use HttpServer\ServerProviders;
use Snowflake\Abstracts\BaseApplication;
use Snowflake\Exception\NotFindClassException;
@@ -36,6 +38,7 @@ class Application extends BaseApplication
*/
public function init()
{
$this->import(ConsoleProviders::class);
$this->import(DatabasesProviders::class);
$this->import(ServerProviders::class);
}
@@ -64,9 +67,9 @@ class Application extends BaseApplication
*/
public function start()
{
$process = Snowflake::get()->processes;
$process->initCore();
$process->start();
/** @var Server $manager */
$manager = Snowflake::get()->get('server');
$manager->start();
}
-140
View File
@@ -1,140 +0,0 @@
<?php
namespace Snowflake\Console;
use Exception;
use Snowflake\Snowflake;
use Swoole\Coroutine\Channel;
/**
* Class AbstractConsole
* @package Snowflake\Console
*/
abstract class AbstractConsole
{
/**
* @var Command[]
*/
public $commands = [];
/** @var Dtl $parameters */
private $parameters;
/** @var array */
private $_config;
/**
* @param array $config
* AbstractConsole constructor.
* @throws Exception
*/
public function __construct(array $config = [])
{
$this->_config = $config;
$this->signCommand(Snowflake::createObject(DefaultCommand::class));
}
/**
* @return array
*/
public function getConfig()
{
return $this->_config;
}
/**
* @return $this
*/
public function setParameters()
{
$this->parameters = new Dtl($_SERVER['argv']);
return $this;
}
/**
* @param Command $command
* @return mixed
*/
public function execCommand(Command $command)
{
return $command->handler($this->parameters);
}
/**
* @return Command|null
*/
public function search()
{
$name = $this->parameters->getCommandName();
$this->parameters->set('commandList', $this->getCommandList());
foreach ($this->commands as $command) {
if ($command->command != $name) {
continue;
}
return $command;
}
return null;
}
/**
* @param Command $abstractConsole
*
* 注册命令
*/
public function signCommand(Command $abstractConsole)
{
$this->commands[] = $abstractConsole;
}
/**
* @param $kernel
* @throws Exception
*/
public function batch($kernel)
{
if (is_object($kernel)) {
if (!property_exists($kernel, 'commands')) {
return;
}
$kernel = $kernel->commands;
}
if (!is_array($kernel)) {
return;
}
foreach ($kernel as $command) {
$this->signCommand(Snowflake::createObject($command));
}
}
/**
* @param Command $abstractConsole
* 释放一个命令
*/
public function destroyCommand(Command $abstractConsole)
{
foreach ($this->commands as $index => $command) {
if ($abstractConsole === $command) {
unset($this->commands[$index]);
break;
}
}
}
/**
* @return array
*/
private function getCommandList()
{
$_tmp = [];
foreach ($this->commands as $command) {
$_tmp[$command->command] = [$command->description, $command];
}
ksort($_tmp, SORT_ASC);
return $_tmp;
}
}
-89
View File
@@ -1,89 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/10/7 0007
* Time: 2:16
*/
namespace Snowflake\Console;
use Snowflake\Abstracts\BaseApplication;
use Swoole\Coroutine\Channel;
use Swoole\Runtime;
use Swoole\Timer;
use Snowflake\Snowflake;
/**
* Class Application
* @package Snowflake\Console
*/
class Application extends BaseApplication
{
/**
* @var string
*/
public $id = 'uniqueId';
private $console;
/** @var Channel */
private $channel;
/**
* Application constructor.
* @param array $config
* @throws
*/
public function __construct(array $config = [])
{
parent::__construct($config);
$this->channel = new Channel(1);
$this->console = new Console($config);
}
/**
* @param $class
* @throws
*/
public function register($class)
{
if (is_string($class) || is_callable($class, true)) {
$class = Snowflake::createObject($class);
}
$this->console->signCommand($class);
}
/**
* @param null $kernel
* @return string|void
* @throws \Exception
*/
public function run($kernel = null)
{
try {
$kernel = make($kernel, Kernel::class);
Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
$this->console->setParameters();
$this->console->batch($kernel);
$class = $this->console->search();
$params = response()->send($this->console->execCommand($class));
} catch (\Exception $exception) {
$params = response()->send(implode("\n", [
'Msg: ' . $exception->getMessage(),
'Line: ' . $exception->getLine(),
'File: ' . $exception->getFile()
]));
} finally {
Timer::clearAll();
return $params;
}
}
}
-38
View File
@@ -1,38 +0,0 @@
<?php
namespace Snowflake\Console;
use Snowflake\Abstracts\BaseObject;
/**
* Class Command
* @package Snowflake\Console
*/
abstract class Command extends BaseObject implements CommandInterface
{
public $command = '';
public $description = '';
/**
* @return string
* 返回执行的命令名称
*/
public function getName()
{
return $this->command;
}
/**
* @return string
*
* 返回命令描述
*/
public function getDescription()
{
return $this->description;
}
}
-15
View File
@@ -1,15 +0,0 @@
<?php
namespace Snowflake\Console;
/**
* Interface CommandInterface
* @package Snowflake\Console
*/
interface CommandInterface
{
public function handler(Dtl $dtl);
}
-13
View File
@@ -1,13 +0,0 @@
<?php
namespace Snowflake\Console;
/**
* Class Console
* @package Snowflake\Console
*/
class Console extends AbstractConsole
{
}
-38
View File
@@ -1,38 +0,0 @@
<?php
namespace Snowflake\Console;
/**
* Class DefaultCommand
* @package Snowflake\Console
*/
class DefaultCommand extends Command
{
public $command = 'list';
public $description = 'help';
public function handler(Dtl $dtl)
{
$param = $dtl->get('commandList');
$last = '';
$lists = [str_pad('Commands', 24, ' ', STR_PAD_RIGHT) . '注释'];
foreach ($param as $key => $val) {
$split = explode(':', $key);
if (empty($last) && isset($split[0])) {
$lists[] = str_pad("\033[32;40;1;1m" . $split[0] . " \033[0m", 40, ' ', STR_PAD_RIGHT);
} else if (isset($split[0]) && $last != $split[0]) {
$lists[] = str_pad("\033[32;40;1;1m" . $split[0] . " \033[0m", 40, ' ', STR_PAD_RIGHT);
}
$last = $split[0] ?? '';
list($method, $ts) = $val;
$lists[] = str_pad("\033[32;40;1;1m " . $key . " \033[0m", 40, ' ', STR_PAD_RIGHT) . $method;
}
return implode(PHP_EOL, $lists);
}
}
-131
View File
@@ -1,131 +0,0 @@
<?php
namespace Snowflake\Console;
use HttpServer\Http\Context;
use HttpServer\Http\HttpHeaders;
use HttpServer\Http\HttpParams;
use HttpServer\Http\Request;
/**
* Class Dtl
* @package Snowflake\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);
}
}
-12
View File
@@ -1,12 +0,0 @@
<?php
namespace Snowflake\Console;
interface ICommand
{
public function handler();
}
-12
View File
@@ -1,12 +0,0 @@
<?php
namespace Snowflake\Console;
class Kernel
{
public $commands = [
];
}
-47
View File
@@ -1,47 +0,0 @@
<?php
namespace Snowflake;
use Exception;
use HttpServer\Server;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use Snowflake\Abstracts\Component;
use Swoole\Process\Pool;
/**
* Class Processes
* @package Snowflake
*/
class Processes extends Component
{
public $processes = [];
/**
* 构建服务
* @throws Exception
*/
public function initCore()
{
/** @var Server $manager */
$manager = Snowflake::get()->get('server');
$serverConfig = Config::get('servers', true);
return $manager->initCore($serverConfig);
}
/**
* @throws Exception
*/
public function start()
{
$server = $this->initCore();
$server->start();
}
}