This commit is contained in:
2020-12-17 14:09:14 +08:00
parent 672a719dbd
commit 36c1d0502a
151 changed files with 1937 additions and 2848 deletions
-257
View File
@@ -1,257 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route\Annotation;
use HttpServer\IInterface\After;
use HttpServer\IInterface\Interceptor;
use HttpServer\IInterface\Limits;
use HttpServer\Route\Node;
use ReflectionClass;
use ReflectionException;
use Snowflake\Annotation\Annotation;
use Snowflake\Snowflake;
/**
* Class Annotation
*/
class Http extends Annotation
{
const HTTP_EVENT = 'http:event:';
const CLOSE = 'Close';
/**
* @var string
* @Interceptor(LoginInterceptor)
*/
private string $Interceptor = 'required|not empty';
/**
* @var string
*/
private string $Limits = 'required|not empty';
private string $Method = 'post';
private string $Middleware = '';
private string $After = '';
protected array $_annotations = [];
/**
* @param Node $node
* @param ReflectionClass $reflect
* @param $method
* @param $annotations
* @throws ReflectionException
*/
public function read(Node $node, ReflectionClass $reflect, $method, $annotations)
{
$method = $reflect->getMethod($method);
$_annotations = $this->getDocCommentAnnotation($annotations, $method->getDocComment());
foreach ($_annotations as $keyName => $annotation) {
if (!in_array($keyName, $annotations)) {
continue;
}
$this->bind($keyName, $node, $annotation);
}
}
/**
* @param $keyName
* @param $node
* @param $annotation
*/
private function bind($keyName, $node, $annotation)
{
switch ($keyName) {
case 'Method':
$this->bindMethod($node, $annotation);
break;
case'Interceptor':
$this->bindInterceptors($node, $annotation);
break;
case 'Middleware':
$this->bindMiddleware($node, $annotation);
break;
case 'Limits':
$this->bindLimits($node, $annotation);
break;
case 'After':
$this->bindAfter($node, $annotation);
break;
}
}
/**
* @param $node
* @param $annotation
*/
private function bindMethod($node, $annotation)
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
if (in_array('any', $explode)) {
$explode = ['*'];
}
$node->method = $explode;
}
/**
* @param Node $node
* @param $annotation
* @throws
*/
private function bindMiddleware(Node $node, $annotation)
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\Middleware\\' . $middleware;
}
if (!class_exists($middleware)) {
continue;
}
$node->addMiddleware($middleware);
}
}
/**
* @param Node $node
* @param $annotation
* @throws
*/
private function bindInterceptors(Node $node, $annotation)
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\Interceptor\\' . $middleware;
}
if (!class_exists($middleware)) {
continue;
}
$middleware = Snowflake::createObject($middleware);
if (!($middleware instanceof Interceptor)) {
continue;
}
$node->addInterceptor([$middleware, 'Interceptor']);
}
}
/**
* @param Node $node
* @param $annotation
* @throws
*/
private function bindAfter(Node $node, $annotation)
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\After\\' . $middleware;
}
if (!class_exists($middleware)) {
continue;
}
$middleware = Snowflake::createObject($middleware);
if (!($middleware instanceof After)) {
continue;
}
$node->addAfter([$middleware, 'onHandler']);
}
}
/**
* @param Node $node
* @param $annotation
* @throws
*/
private function bindLimits(Node $node, $annotation)
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\Limits\\' . $middleware;
}
if (!class_exists($middleware)) {
continue;
}
$middleware = Snowflake::createObject($middleware);
if (!($middleware instanceof Limits)) {
continue;
}
$node->addLimits([$middleware, 'next']);
}
}
/**
* @param $controller
* @param $methodName
* @param $events
* @return array|void
* @throws
*/
public function createHandler($controller, $methodName, $events)
{
return Snowflake::createObject($events[2]);
}
/**
* @param $events
* @return bool
*/
public function isLegitimate($events)
{
return isset($events[2]) && !empty($events[2]);
}
/**
* @param $name
* @param $comment
* @return false|string
*/
public function getName($name, $comment = [])
{
$prefix = self::HTTP_EVENT . ltrim($name, ':');
if (isset($comment[2]) && !empty($comment[2])) {
return $prefix . ':' . $comment[2];
}
return $prefix;
}
}
-62
View File
@@ -1,62 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route\Annotation;
use Snowflake\Annotation\Annotation;
/**
* Class Tcp
* @package HttpServer\Route\Annotation
*/
class Tcp extends Annotation
{
const CONNECT = 'Connect';
const PACKET = 'Packet';
const RECEIVE = 'Receive';
const CLOSE = 'Close';
private string $Message = 'required|not empty';
/**
* @param $controller
* @param $methodName
* @param $events
* @return array
*/
public function createHandler($controller, $methodName, $events)
{
return [$controller, $methodName];
}
/**
* @param $events
* @return bool|void
*/
public function isLegitimate($events)
{
return true;
}
/**
* @param $events
* @param $comment
* @return false|string
*/
public function getName($events, $comment = [])
{
$prefix = 'TCP:ANNOTATION:' . ltrim($events, ':');
if (isset($comment[2]) && !empty($comment[2])) {
return $prefix . ':' . $comment[2];
}
return $prefix;
}
}
-65
View File
@@ -1,65 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route\Annotation;
use Snowflake\Annotation\Annotation;
/**
* Class Websocket
* @package Snowflake\Annotation
*/
class Websocket extends Annotation
{
const MESSAGE = 'Message';
const HANDSHAKE = 'Handshake';
const CLOSE = 'Close';
private string $Message = 'required|not empty';
private string $Handshake;
private string $Close;
/**
* @param $controller
* @param $methodName
* @param $events
* @return array
*/
public function createHandler($controller, $methodName, $events)
{
return [$controller, $methodName];
}
/**
* @param $events
* @return bool|void
*/
public function isLegitimate($events)
{
return true;
}
/**
* @param $events
* @param $comment
* @return false|string
*/
public function getName($events, $comment = [])
{
$prefix = 'WEBSOCKET:ANNOTATION:' . ltrim($events, ':');
if (isset($comment[2]) && !empty($comment[2])) {
return $prefix . ':' . $comment[2];
}
return $prefix;
}
}
+2 -2
View File
@@ -12,7 +12,7 @@ namespace HttpServer\Route;
class Any
{
private $nodes = [];
private array $nodes = [];
/**
* Any constructor.
@@ -29,7 +29,7 @@ class Any
* @param $arguments
* @return $this
*/
public function __call($name, $arguments)
public function __call($name, $arguments): static
{
foreach ($this->nodes as $node) {
$node->{$name}(...$arguments);
+7 -7
View File
@@ -32,7 +32,7 @@ class Filter extends Application
* @return BodyFilter|bool
* @throws Exception
*/
public function setBody(array $value)
public function setBody(array $value): bool|BodyFilter
{
if (empty($value)) {
return true;
@@ -52,7 +52,7 @@ class Filter extends Application
* @return HeaderFilter|bool
* @throws Exception
*/
public function setHeader(array $value)
public function setHeader(array $value): HeaderFilter|bool
{
if (empty($value)) {
return true;
@@ -72,7 +72,7 @@ class Filter extends Application
* @return QueryFilter|bool
* @throws Exception
*/
public function setQuery(array $value)
public function setQuery(array $value): QueryFilter|bool
{
if (empty($value)) {
return true;
@@ -90,7 +90,7 @@ class Filter extends Application
/**
* @throws Exception
*/
public function handler()
public function handler(): bool
{
if (($error = $this->filters()) !== true) {
throw new FilterException($error);
@@ -104,7 +104,7 @@ class Filter extends Application
/**
* @return bool
*/
private function filters()
private function filters(): bool
{
if (empty($this->_filters)) {
return true;
@@ -118,9 +118,9 @@ class Filter extends Application
}
/**
* @return bool|mixed
* @return mixed
*/
private function grant()
private function grant(): mixed
{
if (!is_callable($this->grant, true)) {
return true;
+1 -1
View File
@@ -18,7 +18,7 @@ class BodyFilter extends Filter
* @return bool
* @throws Exception
*/
public function check()
public function check(): bool
{
return $this->validator();
}
+1 -1
View File
@@ -27,7 +27,7 @@ abstract class Filter extends Application
* @return bool
* @throws Exception
*/
protected function validator()
protected function validator(): bool
{
$validator = Validator::getInstance();
$validator->setParams($this->params);
+9 -1
View File
@@ -5,12 +5,20 @@ declare(strict_types=1);
namespace HttpServer\Route\Filter;
use JetBrains\PhpStorm\Pure;
use Throwable;
class FilterException extends \Exception
{
public function __construct($message = "", $code = 0, Throwable $previous = null)
/**
* FilterException constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
#[Pure] public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
+1 -1
View File
@@ -18,7 +18,7 @@ class HeaderFilter extends Filter
* @return bool
* @throws Exception
*/
public function check()
public function check(): bool
{
return $this->validator();
}
+1 -1
View File
@@ -18,7 +18,7 @@ class QueryFilter extends Filter
* @return bool
* @throws Exception
*/
public function check()
public function check(): bool
{
return $this->validator();
}
+4 -2
View File
@@ -7,6 +7,7 @@ namespace HttpServer\Route;
use Exception;
use HttpServer\Application;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
/**
@@ -43,9 +44,10 @@ class Handler extends Application
/**
* @param $route
* @param $handler
* @return Handler
* @return Handler|Node|null
* @throws ConfigException
*/
public function handler($route, $handler)
public function handler($route, $handler): Handler|Node|null
{
return $this->router->addRoute($route, $handler, 'receive');
}
-1
View File
@@ -15,7 +15,6 @@ use Annotation\Route\Middleware as RMiddleware;
use Exception;
use Annotation\Route\Limits;
use HttpServer\Route\Dispatch\Dispatch;
use Snowflake\Core\JSON;
use Snowflake\Snowflake;
/**
+26 -34
View File
@@ -5,18 +5,14 @@ namespace HttpServer\Route;
use Closure;
use Exception;
use HttpServer\Exception\ExitException;
use HttpServer\Http\Context;
use HttpServer\Http\Request;
use HttpServer\IInterface\RouterInterface;
use HttpServer\Application;
use HttpServer\Route\Annotation\Http;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Config;
use Snowflake\Core\JSON;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
use Swoole\Coroutine;
defined('ROUTER_TREE') or define('ROUTER_TREE', 1);
defined('ROUTER_HASH') or define('ROUTER_HASH', 2);
@@ -42,8 +38,6 @@ class Router extends Application implements RouterInterface
public bool $useTree = false;
private bool $reading = false;
/**
* @param Closure $middleware
@@ -115,7 +109,7 @@ class Router extends Application implements RouterInterface
* @param $path
* @return string
*/
private function resolve($path)
#[Pure] private function resolve($path): string
{
$paths = array_column($this->groupTacks, 'prefix');
if (empty($paths)) {
@@ -182,10 +176,10 @@ class Router extends Application implements RouterInterface
/**
* @param $route
* @param $handler
* @return mixed
* @return Node|null
* @throws ConfigException
*/
public function socket($route, $handler): mixed
public function socket($route, $handler): ?Node
{
return $this->addRoute($route, $handler, 'socket');
}
@@ -205,10 +199,10 @@ class Router extends Application implements RouterInterface
/**
* @param $route
* @param $handler
* @return mixed|Node|null
* @throws
* @return Node|null
* @throws ConfigException
*/
public function get($route, $handler)
public function get($route, $handler): ?Node
{
return $this->addRoute($route, $handler, 'get');
}
@@ -219,7 +213,7 @@ class Router extends Application implements RouterInterface
* @return mixed|Node|null
* @throws
*/
public function options($route, $handler)
public function options($route, $handler): ?Node
{
return $this->addRoute($route, $handler, 'options');
}
@@ -240,8 +234,9 @@ class Router extends Application implements RouterInterface
* @param $route
* @param $handler
* @return Any
* @throws ConfigException
*/
public function any($route, $handler)
public function any($route, $handler): Any
{
$nodes = [];
foreach (['get', 'post', 'options', 'put', 'delete'] as $method) {
@@ -253,10 +248,10 @@ class Router extends Application implements RouterInterface
/**
* @param $route
* @param $handler
* @return mixed|Node|null
* @throws
* @return Node|null
* @throws ConfigException
*/
public function delete($route, $handler)
public function delete($route, $handler): ?Node
{
return $this->addRoute($route, $handler, 'delete');
}
@@ -264,10 +259,10 @@ class Router extends Application implements RouterInterface
/**
* @param $route
* @param $handler
* @return mixed|Node|null
* @throws
* @return Node|null
* @throws ConfigException
*/
public function put($route, $handler)
public function put($route, $handler): ?Node
{
return $this->addRoute($route, $handler, 'put');
}
@@ -380,7 +375,7 @@ class Router extends Application implements RouterInterface
* @return array
* '*'
*/
public function split($path)
public function split($path): array
{
$prefix = $this->addPrefix();
$path = ltrim($path, '/');
@@ -403,7 +398,7 @@ class Router extends Application implements RouterInterface
/**
* @return array
*/
public function each()
public function each(): array
{
$paths = [];
foreach ($this->nodes as $node) {
@@ -427,7 +422,7 @@ class Router extends Application implements RouterInterface
* @param array $returns
* @return array
*/
private function readByArray($array, $returns = [])
private function readByArray($array, $returns = []): array
{
foreach ($array as $value) {
if (empty($value)) {
@@ -450,7 +445,7 @@ class Router extends Application implements RouterInterface
* @param string $paths
* @return array
*/
private function readByChild($child, $paths = '')
private function readByChild($child, $paths = ''): array
{
$newPath = [];
/** @var Node $item */
@@ -490,11 +485,10 @@ class Router extends Application implements RouterInterface
/**
* @param $exception
* @return false|int|mixed|string
* @return mixed
* @throws ComponentException
* @throws Exception
*/
private function exception($exception)
private function exception($exception): mixed
{
return Snowflake::app()->getLogger()->exception($exception);
}
@@ -502,10 +496,10 @@ class Router extends Application implements RouterInterface
/**
* @param Request $request
* @return Node|false|int|mixed|string|null
* @return Node|null 树干搜索
* 树干搜索
*/
private function find_path(Request $request)
private function find_path(Request $request): ?Node
{
$method = $request->getMethod();
if (!isset($this->nodes[$method])) {
@@ -545,7 +539,7 @@ class Router extends Application implements RouterInterface
* @param $request
* @return Node|null
*/
private function search_options($request)
private function search_options($request): ?Node
{
$method = $request->getMethod();
if (!isset($this->nodes[$method])) {
@@ -563,7 +557,7 @@ class Router extends Application implements RouterInterface
* @return Node|null
* 树杈搜索
*/
private function Branch_search($request)
private function Branch_search($request): ?Node
{
$node = $this->tree_search($request->getExplode(), $request->getMethod());
if ($node instanceof Node) {
@@ -596,7 +590,6 @@ class Router extends Application implements RouterInterface
private function loadRouteDir($path)
{
$files = glob($path . '/*');
$this->reading = true;
for ($i = 0; $i < count($files); $i++) {
if (is_dir($files[$i])) {
$this->loadRouteDir($files[$i]);
@@ -604,7 +597,6 @@ class Router extends Application implements RouterInterface
$this->loadRouterFile($files[$i]);
}
}
$this->reading = false;
}