modify
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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')]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
@@ -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
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+574
-593
File diff suppressed because it is too large
Load Diff
@@ -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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
|
||||
@@ -154,8 +154,9 @@ abstract class BaseApplication extends Service
|
||||
*/
|
||||
public function parseInt($config)
|
||||
{
|
||||
$_config = Snowflake::app()->getConfig();
|
||||
foreach ($config as $key => $value) {
|
||||
Config::set($key, $value);
|
||||
$_config->setData($key, $value);
|
||||
}
|
||||
if ($storage = Config::get('storage', 'storage')) {
|
||||
if (!str_contains($storage, APP_PATH)) {
|
||||
|
||||
@@ -23,119 +23,6 @@ use Snowflake\Snowflake;
|
||||
class Component extends BaseObject
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $_events = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $name [事件名称]
|
||||
* @param $callback [回调函数]
|
||||
* @param array $param [函数参数]
|
||||
*
|
||||
* {
|
||||
* 事件名, 回调, 参数
|
||||
* }
|
||||
*/
|
||||
public function on($name, $callback, $param = [])
|
||||
{
|
||||
if (isset($this->_events[$name])) {
|
||||
array_push($this->_events[$name], [$callback, $param]);
|
||||
} else {
|
||||
$this->_events[$name][] = [$callback, $param];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $callback
|
||||
* @return bool
|
||||
*/
|
||||
#[Pure] public function hasEvent($name, $callback = null): bool
|
||||
{
|
||||
if (!isset($this->_events[$name])) {
|
||||
return false;
|
||||
}
|
||||
if (!is_array($this->_events[$name])) {
|
||||
return false;
|
||||
}
|
||||
foreach ($this->_events[$name] as $event) {
|
||||
[$_callback, $param] = $event;
|
||||
if ($_callback === $callback) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $event
|
||||
* @param array $params
|
||||
* @param bool $isRemove
|
||||
* @throws Exception
|
||||
*/
|
||||
public function trigger($name, $event = null, $params = [], $isRemove = false)
|
||||
{
|
||||
$aEvents = Snowflake::app()->getEvent();
|
||||
if (isset($this->_events[$name])) {
|
||||
$events = $this->_events[$name];
|
||||
foreach ($events as $key => $_event) {
|
||||
if (!empty($event)) {
|
||||
$_event = $event;
|
||||
}
|
||||
call_user_func($_event, ...$params);
|
||||
if ($isRemove) {
|
||||
unset($this->_events[$name][$key]);
|
||||
$aEvents->of($name, $_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
$aEvents->trigger($name, $event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $handler
|
||||
* @return void
|
||||
* @throws ComponentException
|
||||
*/
|
||||
public function off($name, $handler = NULL): void
|
||||
{
|
||||
$aEvents = Snowflake::app()->getEvent();
|
||||
if (!isset($this->_events[$name])) {
|
||||
$aEvents->of($name, $handler);
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($handler)) {
|
||||
unset($this->_events[$name]);
|
||||
|
||||
$aEvents->of($name, $handler);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->_events[$name] as $key => $val) {
|
||||
if ($val[0] != $handler) {
|
||||
continue;
|
||||
}
|
||||
unset($this->_events[$name][$key]);
|
||||
|
||||
break;
|
||||
}
|
||||
$aEvents->of($name, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function offAll()
|
||||
{
|
||||
$this->_events = [];
|
||||
$aEvents = Snowflake::app()->getEvent();
|
||||
$aEvents->clean();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
|
||||
@@ -9,16 +9,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace Snowflake\Cache;
|
||||
|
||||
use Annotation\Aspect;
|
||||
use Database\InjectProperty;
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
|
||||
+66
-59
@@ -18,72 +18,79 @@ class Channel extends Component
|
||||
{
|
||||
|
||||
|
||||
private array $_channels = [];
|
||||
private array $_channels = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function push(mixed $value, string $name = ''): void
|
||||
{
|
||||
$channel = $this->channelInit($name);
|
||||
$channel->enqueue($value);
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function push(mixed $value, string $name = ''): void
|
||||
{
|
||||
unset($value);
|
||||
return;
|
||||
$channel = $this->channelInit($name);
|
||||
$channel->enqueue($value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool|SplQueue
|
||||
*/
|
||||
private function channelInit(string $name = ''): bool|SplQueue
|
||||
{
|
||||
if (!isset($this->_channels[$name]) || !($this->_channels[$name] instanceof SplQueue)) {
|
||||
$this->_channels[$name] = new SplQueue();
|
||||
}
|
||||
return $this->_channels[$name];
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool|SplQueue
|
||||
*/
|
||||
private function channelInit(string $name = ''): bool|SplQueue
|
||||
{
|
||||
if (!isset($this->_channels[$name]) || !($this->_channels[$name] instanceof SplQueue)) {
|
||||
$this->_channels[$name] = new SplQueue();
|
||||
}
|
||||
return $this->_channels[$name];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* 清空缓存
|
||||
*/
|
||||
public function cleanAll()
|
||||
{
|
||||
/** @var SplQueue $channel */
|
||||
foreach ($this->_channels as $channel) {
|
||||
while ($channel->count() > 0) {
|
||||
$channel->dequeue();
|
||||
}
|
||||
}
|
||||
$this->_channels = [];
|
||||
}
|
||||
/**
|
||||
*
|
||||
* 清空缓存
|
||||
*/
|
||||
public function cleanAll()
|
||||
{
|
||||
/** @var SplQueue $channel */
|
||||
foreach ($this->_channels as $channel) {
|
||||
if (!($channel instanceof SplQueue)) {
|
||||
continue;
|
||||
}
|
||||
while ($channel->count() > 0) {
|
||||
$channel->dequeue();
|
||||
}
|
||||
}
|
||||
$this->_channels = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $timeout
|
||||
* @param Closure $closure
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function pop(string $name, Closure $closure, int|float $timeout = null): mixed
|
||||
{
|
||||
if (($channel = $this->channelInit($name)) == false) {
|
||||
return $this->addError('Channel is full.');
|
||||
}
|
||||
if (!$channel->isEmpty()) {
|
||||
return $channel->pop();
|
||||
}
|
||||
if ($timeout !== null) {
|
||||
$data = $channel->dequeue();
|
||||
}
|
||||
if (empty($data)) {
|
||||
$data = call_user_func($closure);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
/**
|
||||
* @param $timeout
|
||||
* @param Closure $closure
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function pop(string $name, Closure $closure, int|float $timeout = null): mixed
|
||||
{
|
||||
return call_user_func($closure);
|
||||
|
||||
if (($channel = $this->channelInit($name)) == false) {
|
||||
return $this->addError('Channel is full.');
|
||||
}
|
||||
if (!$channel->isEmpty()) {
|
||||
return $channel->shift();
|
||||
}
|
||||
if ($timeout !== null) {
|
||||
$data = $channel->dequeue();
|
||||
}
|
||||
if (empty($data)) {
|
||||
$data = call_user_func($closure);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ class Logger extends Component
|
||||
|
||||
private array $logs = [];
|
||||
|
||||
public int $worker_id;
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
|
||||
@@ -25,8 +25,6 @@ class LoggerAspect implements IAspect
|
||||
*/
|
||||
#[Pure] public function __construct(public array $handler)
|
||||
{
|
||||
$this->className = get_class($this->handler[0]);
|
||||
$this->methodName = $this->handler[1];
|
||||
}
|
||||
|
||||
|
||||
@@ -47,8 +45,11 @@ class LoggerAspect implements IAspect
|
||||
|
||||
private function print_runtime($startTime)
|
||||
{
|
||||
$className = get_class($this->handler[0]);
|
||||
$methodName = $this->handler[1];
|
||||
|
||||
$runTime = round(microtime(true) - $startTime, 6);
|
||||
echo sprintf('run %s::%s use time %6f', $this->className, $this->methodName, $runTime);
|
||||
echo sprintf('run %s::%s use time %6f', $className, $methodName, $runTime);
|
||||
echo PHP_EOL;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,19 +3,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace Snowflake\Jwt;
|
||||
|
||||
use Annotation\Aspect;
|
||||
use Database\InjectProperty;
|
||||
use Exception;
|
||||
use HttpServer\Http\HttpHeaders;
|
||||
use ReflectionException;
|
||||
use Snowflake\Cache\Redis;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Core\Str;
|
||||
use Snowflake\Exception\AuthException;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
|
||||
|
||||
@@ -23,10 +23,6 @@ class Connection extends Pool
|
||||
|
||||
public int $timeout = 1900;
|
||||
|
||||
/** @var PDO[] */
|
||||
protected array $connections = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $timeout
|
||||
*/
|
||||
|
||||
@@ -20,7 +20,7 @@ use Swoole\Coroutine\System;
|
||||
abstract class Process extends \Swoole\Process implements SProcess
|
||||
{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Process constructor.
|
||||
* @param $application
|
||||
|
||||
@@ -17,7 +17,7 @@ class Validator extends BaseValidator
|
||||
{
|
||||
|
||||
/** @var BaseValidator[] */
|
||||
private array $validators = [];
|
||||
private ?array $validators = [];
|
||||
|
||||
/** @var ?Validator */
|
||||
private static ?Validator $instance = null;
|
||||
@@ -197,6 +197,7 @@ class Validator extends BaseValidator
|
||||
}
|
||||
break;
|
||||
}
|
||||
unset($this->validators);
|
||||
$this->validators = [];
|
||||
return !isset($isTrue);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user