改名
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace HttpServer\Route;
|
||||||
|
|
||||||
|
use Snowflake\Abstracts\BaseObject;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class HandlerProviders extends BaseObject
|
||||||
|
{
|
||||||
|
|
||||||
|
private array $handlers = [];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $path
|
||||||
|
* @param $method
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function get($path, $method): mixed
|
||||||
|
{
|
||||||
|
return $this->handlers[$method][$path] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $method
|
||||||
|
* @param $path
|
||||||
|
* @param $handler
|
||||||
|
*/
|
||||||
|
public function add($method, $path, $handler)
|
||||||
|
{
|
||||||
|
$this->handlers[$method][$path] = $handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+62
-45
@@ -10,10 +10,10 @@ use Annotation\Route\RpcProducer;
|
|||||||
use Closure;
|
use Closure;
|
||||||
use Exception;
|
use Exception;
|
||||||
use HttpServer\Abstracts\HttpService;
|
use HttpServer\Abstracts\HttpService;
|
||||||
|
use HttpServer\Exception\RequestException;
|
||||||
use HttpServer\Http\Request;
|
use HttpServer\Http\Request;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Snowflake\Core\Json;
|
|
||||||
use Snowflake\Exception\NotFindClassException;
|
use Snowflake\Exception\NotFindClassException;
|
||||||
use Snowflake\IAspect;
|
use Snowflake\IAspect;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
@@ -38,13 +38,13 @@ class Node extends HttpService
|
|||||||
|
|
||||||
private string $_dataType = '';
|
private string $_dataType = '';
|
||||||
|
|
||||||
/** @var ?Closure|?array */
|
|
||||||
public Closure|array|null $handler;
|
|
||||||
public string $htmlSuffix = '.html';
|
public string $htmlSuffix = '.html';
|
||||||
public bool $enableHtmlSuffix = false;
|
public bool $enableHtmlSuffix = false;
|
||||||
public array $namespace = [];
|
public array $namespace = [];
|
||||||
public array $middleware = [];
|
public array $middleware = [];
|
||||||
|
|
||||||
|
public string $sourcePath = '';
|
||||||
|
|
||||||
/** @var array|Closure */
|
/** @var array|Closure */
|
||||||
public Closure|array $callback = [];
|
public Closure|array $callback = [];
|
||||||
|
|
||||||
@@ -78,20 +78,20 @@ class Node extends HttpService
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $handler
|
* @param $handler
|
||||||
|
* @param $path
|
||||||
* @return Node
|
* @return Node
|
||||||
* @throws
|
* @throws NotFindClassException
|
||||||
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function bindHandler($handler): static
|
public function setHandler($handler, $path): static
|
||||||
{
|
{
|
||||||
|
$this->sourcePath = $path;
|
||||||
if (is_string($handler) && str_contains($handler, '@')) {
|
if (is_string($handler) && str_contains($handler, '@')) {
|
||||||
$this->handler = $this->splitHandler($handler);
|
$handler = $this->splitHandler($handler);
|
||||||
} else if ($handler != null && !is_callable($handler, true)) {
|
} else if ($handler != null && !is_callable($handler, true)) {
|
||||||
$this->_error = 'Controller is con\'t exec.';
|
$this->_error = 'Controller is con\'t exec.';
|
||||||
} else {
|
|
||||||
$this->handler = $handler;
|
|
||||||
}
|
}
|
||||||
$this->setParameters($this->handler);
|
return $this->setParameters($handler);
|
||||||
return $this->injectMiddleware();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -115,22 +115,47 @@ class Node extends HttpService
|
|||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
private function injectMiddleware(): static
|
private function injectMiddleware($handler): static
|
||||||
{
|
{
|
||||||
$manager = di(MiddlewareManager::class);
|
$manager = di(MiddlewareManager::class);
|
||||||
if ($this->handler instanceof Closure) {
|
if (!($handler instanceof Closure)) {
|
||||||
if (!empty($this->middleware)) {
|
$callback = $this->injectControllerMiddleware($manager, $handler);
|
||||||
$this->callback = $manager->closureMiddlewares($this->middleware, $this->normalHandler($this->handler));
|
|
||||||
} else {
|
|
||||||
$this->callback = $this->normalHandler($this->handler);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$manager->addMiddlewares($this->handler[0], $this->handler[1], $this->middleware);
|
$callback = $this->injectClosureMiddleware($manager, $handler);
|
||||||
$this->callback = $manager->callerMiddlewares(
|
}
|
||||||
$this->handler[0], $this->handler[1], $this->aopHandler($this->getAop())
|
$handlerProviders = di(HandlerProviders::class);
|
||||||
);
|
return $handlerProviders->add($this->sourcePath, $this->method, $callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $manager
|
||||||
|
* @param $handler
|
||||||
|
* @return mixed
|
||||||
|
* @throws NotFindClassException
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
private function injectControllerMiddleware($manager, $handler): mixed
|
||||||
|
{
|
||||||
|
$manager->addMiddlewares($handler[0], $handler[1], $this->middleware);
|
||||||
|
return $manager->callerMiddlewares(
|
||||||
|
$handler[0], $handler[1], $this->aopHandler($this->getAop($handler), $handler)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $manager
|
||||||
|
* @param $handler
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function injectClosureMiddleware($manager, $handler): mixed
|
||||||
|
{
|
||||||
|
if (!empty($this->middleware)) {
|
||||||
|
return $manager->closureMiddlewares($this->middleware, $this->normalHandler($handler));
|
||||||
|
} else {
|
||||||
|
return $this->normalHandler($handler);
|
||||||
}
|
}
|
||||||
return $this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -141,33 +166,34 @@ class Node extends HttpService
|
|||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
*/
|
*/
|
||||||
public function setParameters($handler)
|
public function setParameters($handler): static
|
||||||
{
|
{
|
||||||
$container = Snowflake::getDi();
|
$container = Snowflake::getDi();
|
||||||
if ($handler instanceof Closure) {
|
if ($handler instanceof Closure) {
|
||||||
$this->_injectParameters = $container->resolveFunctionParameters($handler);
|
$this->_injectParameters = $container->resolveFunctionParameters($handler);
|
||||||
} else {
|
} else {
|
||||||
[$controller, $action] = $this->handler;
|
[$controller, $action] = $handler;
|
||||||
if (is_object($controller)) {
|
if (is_object($controller)) {
|
||||||
$controller = get_class($controller);
|
$controller = get_class($controller);
|
||||||
}
|
}
|
||||||
$this->_injectParameters = $container->getMethodParameters($controller, $action);
|
$this->_injectParameters = $container->getMethodParameters($controller, $action);
|
||||||
}
|
}
|
||||||
|
return $this->injectMiddleware($handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param IAspect|null $reflect
|
* @param IAspect|null $reflect
|
||||||
|
* @param $handler
|
||||||
* @return Closure
|
* @return Closure
|
||||||
*/
|
*/
|
||||||
#[Pure] private function aopHandler(?IAspect $reflect): Closure
|
#[Pure] private function aopHandler(?IAspect $reflect, $handler): Closure
|
||||||
{
|
{
|
||||||
if (is_null($reflect)) {
|
if (is_null($reflect)) {
|
||||||
return $this->normalHandler($this->handler);
|
return $this->normalHandler($handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
$params = $this->_injectParameters;
|
$params = $this->_injectParameters;
|
||||||
$handler = $this->handler;
|
|
||||||
return static function () use ($reflect, $handler, $params) {
|
return static function () use ($reflect, $handler, $params) {
|
||||||
return $reflect->invoke($handler, $params);
|
return $reflect->invoke($handler, $params);
|
||||||
};
|
};
|
||||||
@@ -177,16 +203,15 @@ class Node extends HttpService
|
|||||||
/**
|
/**
|
||||||
* @throws ReflectionException|NotFindClassException
|
* @throws ReflectionException|NotFindClassException
|
||||||
*/
|
*/
|
||||||
private function getAop(): ?IAspect
|
private function getAop($handler): ?IAspect
|
||||||
{
|
{
|
||||||
[$controller, $action] = $this->handler;
|
[$controller, $action] = $handler;
|
||||||
|
|
||||||
$aspect = Snowflake::getDi()->getMethodAttribute($controller::class, $action);
|
$aspect = Snowflake::getDi()->getMethodAttribute($controller::class, $action);
|
||||||
if (empty($aspect)) {
|
if (empty($aspect)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
foreach ($aspect as $value) {
|
foreach ($aspect as $value) {
|
||||||
if ($value instanceof Aspect) {
|
if ($value instanceof Aspect) {
|
||||||
return di($value->aspect);
|
return di($value->aspect);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -337,19 +362,11 @@ class Node extends HttpService
|
|||||||
*/
|
*/
|
||||||
public function dispatch(): mixed
|
public function dispatch(): mixed
|
||||||
{
|
{
|
||||||
if (empty($this->callback)) {
|
$handlerProviders = di(HandlerProviders::class)->get($this->sourcePath, $this->method);
|
||||||
return Json::to(404, $this->errorMsg());
|
if (empty($handlerProviders)) {
|
||||||
|
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
|
||||||
}
|
}
|
||||||
return call_user_func($this->callback, \request());
|
return call_user_func($handlerProviders, \request());
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private function errorMsg(): string
|
|
||||||
{
|
|
||||||
return $this->_error ?? 'Page not found.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ class Router extends HttpService implements RouterInterface
|
|||||||
$path = $this->resolve($path);
|
$path = $this->resolve($path);
|
||||||
$this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method);
|
$this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method);
|
||||||
|
|
||||||
return $this->nodes[$method][$path]->bindHandler($handler);
|
return $this->nodes[$method][$path]->setHandler($handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -147,7 +147,7 @@ class Router extends HttpService implements RouterInterface
|
|||||||
if (!empty($explode)) {
|
if (!empty($explode)) {
|
||||||
$parent = $this->bindNode($parent, $explode, $method);
|
$parent = $this->bindNode($parent, $explode, $method);
|
||||||
}
|
}
|
||||||
return $parent->bindHandler($handler);
|
return $parent->setHandler($handler, $path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user