This commit is contained in:
as2252258@163.com
2021-04-23 03:12:00 +08:00
parent 8b57f44f7e
commit 325ab04f20
26 changed files with 775 additions and 1493 deletions
-3
View File
@@ -56,9 +56,6 @@ class Client extends ClientAbstracts
if (in_array($client->getStatusCode(), [200, 201])) {
return $this->structure($body, $data, $client->getHeaders());
}
// if ($client->getStatusCode() == 302) {
// return $this->get($client->getHeaders()['location']);
// }
if (is_string($body)) {
$message = 'Request error code ' . $client->getStatusCode();
} else {
+64 -69
View File
@@ -20,88 +20,83 @@ class OnWorkerStart extends Callback
{
/**
* @param Server $server
* @param int $worker_id
*
* @return mixed
* @throws Exception
*/
public function onHandler(Server $server, int $worker_id): void
{
putenv('state=start');
putenv('worker=' . $worker_id);
/**
* @param Server $server
* @param int $worker_id
*
* @return mixed
* @throws Exception
*/
public function onHandler(Server $server, int $worker_id): void
{
putenv('state=start');
putenv('worker=' . $worker_id);
$content = System::readFile(storage('runtime.php'));
$content = System::readFile(storage('runtime.php'));
$annotation = Snowflake::app()->getAnnotation();
$annotation->setLoader(unserialize($content));
if ($worker_id < $server->setting['worker_num']) {
$this->onWorker($server, $annotation);
} else {
$this->onTask($server, $annotation);
}
}
$annotation = Snowflake::app()->getAnnotation();
$annotation->setLoader(unserialize($content));
if ($worker_id < $server->setting['worker_num']) {
$this->onWorker($server, $annotation);
} else {
$this->onTask($server, $annotation);
}
}
/**
* @param Server $server
* @param int $worker_id
* @return bool
*/
private function isWorker(Server $server, int $worker_id): bool
{
return $worker_id < $server->setting['worker_num'];
}
/**
* @param Server $server
* @param int $worker_id
* @return bool
*/
private function isWorker(Server $server, int $worker_id): bool
{
return $worker_id < $server->setting['worker_num'];
}
/**
* @param Server $server
* @param Annotation $annotation
* @throws ConfigException
* @throws Exception
*/
public function onTask(Server $server, Annotation $annotation)
{
putenv('environmental=' . Snowflake::TASK);
/**
* @param Server $server
* @param Annotation $annotation
* @throws ConfigException
* @throws Exception
*/
public function onTask(Server $server, Annotation $annotation)
{
putenv('environmental=' . Snowflake::TASK);
$annotation->runtime(directory('app'), [
CONTROLLER_PATH,
LISTENER_PATH,
SOCKET_PATH,
TASK_PATH,
]);
name($server->worker_pid, 'Task#' . $server->worker_id);
$annotation->runtime(directory('app'), [
CONTROLLER_PATH,
LISTENER_PATH,
SOCKET_PATH,
TASK_PATH,
]);
name($server->worker_pid, 'Task#' . $server->worker_id);
Snowflake::setTaskId($server->worker_pid);
Snowflake::setTaskId($server->worker_pid);
fire(Event::SERVER_TASK_START);
}
fire(Event::SERVER_TASK_START);
}
/**
* @param Server $server
* @param Annotation $annotation
* @throws Exception
*/
public function onWorker(Server $server, Annotation $annotation)
{
try {
name($server->worker_pid, 'Worker#' . $server->worker_id);
/**
* @param Server $server
* @param Annotation $annotation
* @throws Exception
*/
public function onWorker(Server $server, Annotation $annotation)
{
name($server->worker_pid, 'Worker#' . $server->worker_id);
$time = microtime(true);
$annotation->runtime(CONTROLLER_PATH);
$this->debug('use time.' . (microtime(true) - $time));
$annotation->runtime(directory('app'), CONTROLLER_PATH);
$time = microtime(true);
$annotation->runtime(CONTROLLER_PATH);
$this->debug('use time.' . (microtime(true) - $time));
$annotation->runtime(directory('app'), CONTROLLER_PATH);
Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER);
Snowflake::setWorkerId($server->worker_pid);
putenv('environmental=' . Snowflake::WORKER);
fire(Event::SERVER_WORKER_START, [getenv('worker')]);
} catch (\Throwable $exception) {
$this->addError($exception, 'throwable');
write($exception->getMessage(), 'worker');
}
}
fire(Event::SERVER_WORKER_START, [getenv('worker')]);
}
}
-100
View File
@@ -1,100 +0,0 @@
<?php
namespace HttpServer\Events;
class Pipeline
{
private ?\Closure $_if = null;
private ?\Closure $_else = null;
private ?\Closure $_catch = null;
private ?\Closure $_after = null;
private ?\Closure $_before = null;
private bool $condition;
/**
* @param bool $condition
* @param \Closure $handler
* @return $this
*/
public function if(bool $condition, \Closure $handler): static
{
$this->condition = $condition;
$this->_if = $handler;
return $this;
}
/**
* @param \Closure $handler
* @return $this
*/
public function else(\Closure $handler): static
{
$this->_else = $handler;
return $this;
}
/**
* @param \Closure $handler
* @return $this
*/
public function catch(\Closure $handler): static
{
$this->_catch = $handler;
return $this;
}
/**
* @param \Closure $handler
* @return $this
*/
public function after(\Closure $handler): static
{
$this->_after = $handler;
return $this;
}
/**
* @param \Closure $handler
* @return $this
*/
public function before(\Closure $handler): static
{
$this->_before = $handler;
return $this;
}
/**
* @param $argv
* @return mixed
*/
public function exec(...$argv)
{
try {
if ($this->_before instanceof \Closure) {
call_user_func($this->_before, ...$argv);
}
if ($this->condition !== true) {
call_user_func($this->_else, ...$argv);
} else {
call_user_func($this->_if, ...$argv);
}
return $argv;
} catch (\Throwable $exception) {
call_user_func($this->_catch, $exception);
return $argv;
} finally {
if ($this->_after instanceof \Closure) {
call_user_func($this->_after, ...$argv);
}
}
}
}
-92
View File
@@ -1,92 +0,0 @@
<?php
namespace HttpServer\Events\Utility;
use Closure;
use Exception;
use ReflectionException;
use Snowflake\Core\Json;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
class DataResolve
{
/**
* @param $unpack
* @param $data
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public static function pack($unpack, $data)
{
if (empty($unpack)) {
$params = Json::encode($data);
} else {
$params = self::callbackResolve($unpack, null, null, $data);
}
if ($params === null) {
return 'Format error.';
}
return $params;
}
/**
* @param $unpack
* @param $address
* @param $port
* @param $data
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
public static function unpack($unpack, $address, $port, $data)
{
if (empty($unpack)) {
$params = Json::decode($data);
} else {
$params = self::callbackResolve($unpack, $address, $port, $data);
}
if ($params === null) {
return 'Format error.';
}
return $params;
}
/**
* @param $callback
* @param $address
* @param $port
* @param $data
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
private static function callbackResolve($callback, $address, $port, $data): mixed
{
if ($callback instanceof Closure) {
if (empty($address) && empty($port)) {
return $callback($data);
}
return $callback($address, $port, $data);
}
if (is_string($callback)) {
$callback = [$callback, 'onHandler'];
}
if (isset($callback[0]) && is_string($callback[0])) {
$callback[0] = Snowflake::getDi()->get($callback[0]);
}
if (!empty($address) && !empty($port)) {
return call_user_func($callback, $address, $port, $data);
}
return call_user_func($callback, $data);
}
}
-131
View File
@@ -1,131 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route;
use HttpServer\Abstracts\HttpService;
use HttpServer\Exception\AuthException;
use HttpServer\Route\Filter\BodyFilter;
use HttpServer\Route\Filter\FilterException;
use HttpServer\Route\Filter\HeaderFilter;
use HttpServer\Route\Filter\QueryFilter;
use Exception;
use Snowflake\Snowflake;
/**
* Class Filter
* @package Snowflake\Snowflake\Route
*/
class Filter extends HttpService
{
/** @var Filter\Filter[] */
private array $_filters = [];
/** @var array */
public array $grant = [];
/**
* @param array $value
* @return BodyFilter|bool
* @throws Exception
*/
public function setBody(array $value): bool|BodyFilter
{
if (empty($value)) {
return true;
}
/** @var BodyFilter $class */
$class = Snowflake::createObject(BodyFilter::class);
$class->rules = [];
$class->params = Input()->params();
return $this->_filters[] = $class;
}
/**
* @param array $value
* @return HeaderFilter|bool
* @throws Exception
*/
public function setHeader(array $value): HeaderFilter|bool
{
if (empty($value)) {
return true;
}
/** @var HeaderFilter $class */
$class = Snowflake::createObject(HeaderFilter::class);
$class->rules = [];
$class->params = request()->headers->getHeaders();
return $this->_filters[] = $class;
}
/**
* @param array $value
* @return QueryFilter|bool
* @throws Exception
*/
public function setQuery(array $value): QueryFilter|bool
{
if (empty($value)) {
return true;
}
/** @var QueryFilter $class */
$class = Snowflake::createObject(QueryFilter::class);
$class->rules = [];
$class->params = request()->headers->getHeaders();
return $this->_filters[] = $class;
}
/**
* @throws Exception
*/
public function handler(): bool
{
if (($error = $this->filters()) !== true) {
throw new FilterException($error);
}
if (!$this->grant()) {
throw new AuthException('Authentication error.');
}
return true;
}
/**
* @return bool
*/
private function filters(): bool
{
if (empty($this->_filters)) {
return true;
}
foreach ($this->_filters as $filter) {
if (!$filter->check()) {
return false;
}
}
return true;
}
/**
* @return mixed
*/
private function grant(): mixed
{
if (!is_callable($this->grant, true)) {
return true;
}
return call_user_func($this->grant);
}
}
-26
View File
@@ -1,26 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route\Filter;
use Exception;
/**
* Class BodyFilter
* @package Snowflake\Snowflake\Route\Filter
*/
class BodyFilter extends Filter
{
/**
* @return bool
* @throws Exception
*/
public function check(): bool
{
return $this->validator();
}
}
-47
View File
@@ -1,47 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route\Filter;
use Exception;
use HttpServer\Abstracts\HttpService;
use validator\Validator;
/**
* Class Filter
* @package Snowflake\Snowflake\Route\Filter
*/
abstract class Filter extends HttpService
{
public array $rules = [];
public array $params = [];
abstract public function check();
/**
* @return bool
* @throws Exception
*/
protected function validator(): bool
{
$validator = Validator::getInstance();
$validator->setParams($this->params);
foreach ($this->rules as $val) {
$field = array_shift($val);
if (empty($val)) {
continue;
}
$validator->make($field, $val);
}
if (!$validator->validation()) {
return $this->addError($validator->getError());
}
return true;
}
}
@@ -1,26 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route\Filter;
use Throwable;
class FilterException extends \Exception
{
/**
* FilterException constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
-26
View File
@@ -1,26 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route\Filter;
use Exception;
/**
* Class HeaderFilter
* @package Snowflake\Snowflake\Route\Filter
*/
class HeaderFilter extends Filter
{
/**
* @return bool
* @throws Exception
*/
public function check(): bool
{
return $this->validator();
}
}
-26
View File
@@ -1,26 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route\Filter;
use Exception;
/**
* Class QueryFilter
* @package Snowflake\Snowflake\Route\Filter
*/
class QueryFilter extends Filter
{
/**
* @return bool
* @throws Exception
*/
public function check(): bool
{
return $this->validator();
}
}
-54
View File
@@ -1,54 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route;
use Exception;
use HttpServer\Abstracts\HttpService;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
/**
* Class TcpListen
* @package Snowflake\Snowflake\Route
*/
class Handler extends HttpService
{
/** @var Router */
protected Router $router;
/**
* Listen constructor.
* @throws Exception
*/
public function __construct()
{
$this->router = Snowflake::app()->router;
parent::__construct([]);
}
/**
* @param $config
* @param $handler
*/
public function group($config, $handler)
{
$this->router->group($config, $handler, $this);
}
/**
* @param $route
* @param $handler
* @return Handler|Node|null
*/
public function handler($route, $handler): Handler|Node|null
{
return $this->router->addRoute($route, $handler, 'receive');
}
}
+15 -44
View File
@@ -126,11 +126,11 @@ class Node extends HttpService
public function createDispatch(): Closure
{
return function () {
$dispatchParam = Context::getContext('dispatch-param');
if (empty($dispatchParam)) {
$dispatchParam = [\request()];
}
return \aop($this->handler, $dispatchParam);
$dispatchParam = Context::getContext('dispatch-param');
if (empty($dispatchParam)) {
$dispatchParam = [\request()];
}
return \aop($this->handler, $dispatchParam);
};
}
@@ -276,31 +276,10 @@ class Node extends HttpService
return false;
}
}
return $this->checkRule();
}
/**
* @return bool
* @throws Exception
*/
private function checkRule(): bool
{
if (empty($this->rules)) {
return true;
}
foreach ($this->rules as $rule) {
if (!isset($rule['class'])) {
$rule['class'] = Filter::class;
}
/** @var Filter $object */
$object = Snowflake::createObject($rule);
if (!$object->handler()) {
return false;
}
}
return true;
}
/**
* @param string $controller
* @param string $action
@@ -518,19 +497,11 @@ class Node extends HttpService
*/
public function dispatch(): mixed
{
try {
Context::setContext('dispatch-param', func_get_args());
if (empty($this->callback)) {
return Json::to(404, $this->errorMsg());
}
return $this->httpFilter();
} catch (Throwable $throwable) {
$this->addError($throwable, 'throwable');
$code = $throwable->getCode() == 0 ? 500 : $throwable->getCode();
return Json::to($code, $throwable->getMessage());
Context::setContext('dispatch-param', func_get_args());
if (empty($this->callback)) {
return Json::to(404, $this->errorMsg());
}
return $this->httpFilter();
}
@@ -548,11 +519,11 @@ class Node extends HttpService
}
/**
* @param $dispatchParams
* @return mixed
* @throws Exception
*/
/**
* @param $dispatchParams
* @return mixed
* @throws Exception
*/
private function runValidator($dispatchParams): mixed
{
/** @var HttpFilter $filter */
+47 -52
View File
@@ -13,63 +13,58 @@ class Reduce
{
/**
* @param $last
* @param $middleWares
* @return mixed
*/
public static function reduce($last, $middleWares): mixed
{
return array_reduce(array_reverse($middleWares), static::core(), $last);
}
/**
* @param $last
* @param $middleWares
* @return mixed
*/
public static function reduce($last, $middleWares): mixed
{
return array_reduce(array_reverse($middleWares), static::core(), $last);
}
/**
* @param $middleWares
* @return mixed
*/
public static function after($middleWares): mixed
{
return array_reduce(array_reverse($middleWares), function ($stack, $pipe) {
return function ($request, $passable) use ($stack, $pipe) {
try {
if (!($pipe instanceof After)) {
return call_user_func($pipe, $request, $passable, $stack);
}
return $pipe->onHandler($request, $passable);
} catch (\Throwable $throwable) {
logger()->addError($throwable, 'throwable');
return Json::to(0, $throwable);
}
};
});
}
/**
* @param $middleWares
* @return mixed
*/
public static function after($middleWares): mixed
{
return array_reduce(array_reverse($middleWares), function ($stack, $pipe) {
return function ($request, $passable) use ($stack, $pipe) {
if (!($pipe instanceof After)) {
return call_user_func($pipe, $request, $passable, $stack);
}
return $pipe->onHandler($request, $passable);
};
});
}
/**
* @return Closure
*/
private static function core(): Closure
{
return function ($stack, $pipe) {
return static::passable($stack, $pipe);
};
}
/**
* @return Closure
*/
private static function core(): Closure
{
return function ($stack, $pipe) {
return static::passable($stack, $pipe);
};
}
/**
* @param $stack
* @param $pipe
* @return Closure
*/
public static function passable($stack, $pipe): Closure
{
return function ($passable) use ($stack, $pipe) {
if ($pipe instanceof Middleware) {
return $pipe->onHandler($passable, $stack);
}
return call_user_func($pipe, $passable, $stack);
};
}
/**
* @param $stack
* @param $pipe
* @return Closure
*/
public static function passable($stack, $pipe): Closure
{
return function ($passable) use ($stack, $pipe) {
if ($pipe instanceof Middleware) {
return $pipe->onHandler($passable, $stack);
}
return call_user_func($pipe, $passable, $stack);
};
}
}
File diff suppressed because it is too large Load Diff
-8
View File
@@ -13,12 +13,4 @@ abstract class Tcp extends Server implements Service
use \HttpServer\Service\Abstracts\Server;
/** @var Closure|array */
public array|Closure $unpack;
/** @var Closure|array */
public array|Closure $pack;
}
-3
View File
@@ -4,11 +4,8 @@
namespace HttpServer;
use Annotation\Aspect;
use Database\InjectProperty;
use Exception;
use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Config;
use Snowflake\Exception\ConfigException;