This commit is contained in:
as2252258@163.com
2021-08-28 02:08:20 +08:00
parent 567d440dcd
commit 5e1a3f7148
5 changed files with 20 additions and 32 deletions
+5 -5
View File
@@ -11,7 +11,7 @@ use Kiri\Abstracts\BaseObject;
class HandlerProviders extends BaseObject class HandlerProviders extends BaseObject
{ {
private array $handlers = []; private static array $handlers = [];
/** /**
@@ -19,9 +19,9 @@ class HandlerProviders extends BaseObject
* @param $method * @param $method
* @return mixed * @return mixed
*/ */
public function get($path, $method): mixed public static function get($path, $method): mixed
{ {
return $this->handlers[$method][$path] ?? null; return static::$handlers[$method][$path] ?? null;
} }
@@ -30,9 +30,9 @@ class HandlerProviders extends BaseObject
* @param $path * @param $path
* @param $handler * @param $handler
*/ */
public function add($method, $path, $handler) public static function add($method, $path, $handler)
{ {
$this->handlers[$method][$path] = $handler; static::$handlers[$method][$path] = $handler;
} }
} }
+4 -4
View File
@@ -24,7 +24,7 @@ class MiddlewareManager extends BaseObject
* @param $method * @param $method
* @param array|string $middlewares * @param array|string $middlewares
*/ */
public function addMiddlewares($class, $method, array|string $middlewares) public static function addMiddlewares($class, $method, array|string $middlewares)
{ {
if (is_object($class)) { if (is_object($class)) {
$class = $class::class; $class = $class::class;
@@ -51,7 +51,7 @@ class MiddlewareManager extends BaseObject
* @param $method * @param $method
* @return bool * @return bool
*/ */
public function hasMiddleware($class, $method): bool public static function hasMiddleware($class, $method): bool
{ {
if (is_object($class)) { if (is_object($class)) {
$class = $class::class; $class = $class::class;
@@ -66,7 +66,7 @@ class MiddlewareManager extends BaseObject
* @param $caller * @param $caller
* @return mixed * @return mixed
*/ */
public function callerMiddlewares($class, $method, $caller): mixed public static function callerMiddlewares($class, $method, $caller): mixed
{ {
if (is_object($class)) { if (is_object($class)) {
$class = $class::class; $class = $class::class;
@@ -91,7 +91,7 @@ class MiddlewareManager extends BaseObject
* @param Closure $caller * @param Closure $caller
* @return Closure * @return Closure
*/ */
public function closureMiddlewares($middlewares, Closure $caller): Closure public static function closureMiddlewares($middlewares, Closure $caller): Closure
{ {
return array_reduce(array_reverse($middlewares), function ($stack, $pipe) { return array_reduce(array_reverse($middlewares), function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) { return function ($passable) use ($stack, $pipe) {
+9 -20
View File
@@ -129,27 +129,16 @@ class Node
*/ */
private function injectMiddleware($handler): static private function injectMiddleware($handler): static
{ {
$manager = di(MiddlewareManager::class);
if (!($handler instanceof Closure)) { if (!($handler instanceof Closure)) {
$callback = $this->injectControllerMiddleware($manager, $handler); $callback = $this->injectControllerMiddleware($handler);
} else { } else {
$callback = $this->injectClosureMiddleware($manager, $handler); $callback = $this->injectClosureMiddleware($handler);
} }
$this->getHandlerProviders()->add($this->method, $this->sourcePath, $callback); HandlerProviders::add($this->method, $this->sourcePath, $callback);
return $this; return $this;
} }
/**
* @return HandlerProviders
* @throws ReflectionException
*/
private function getHandlerProviders(): HandlerProviders
{
return Kiri::getDi()->get(HandlerProviders::class);
}
/** /**
* @param $manager * @param $manager
* @param $handler * @param $handler
@@ -157,10 +146,10 @@ class Node
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
private function injectControllerMiddleware($manager, $handler): mixed private function injectControllerMiddleware($handler): mixed
{ {
$manager->addMiddlewares($handler[0], $handler[1], $this->middleware); MiddlewareManager::addMiddlewares($handler[0], $handler[1], $this->middleware);
return $manager->callerMiddlewares( return MiddlewareManager::callerMiddlewares(
$handler[0], $handler[1], $this->aopHandler($this->getAop($handler), $handler) $handler[0], $handler[1], $this->aopHandler($this->getAop($handler), $handler)
); );
} }
@@ -171,10 +160,10 @@ class Node
* @param $handler * @param $handler
* @return mixed * @return mixed
*/ */
private function injectClosureMiddleware($manager, $handler): mixed private function injectClosureMiddleware($handler): mixed
{ {
if (!empty($this->middleware)) { if (!empty($this->middleware)) {
return $manager->closureMiddlewares($this->middleware, $this->normalHandler($handler)); return MiddlewareManager::closureMiddlewares($this->middleware, $this->normalHandler($handler));
} else { } else {
return $this->normalHandler($handler); return $this->normalHandler($handler);
} }
@@ -389,7 +378,7 @@ class Node
*/ */
public function dispatch(): mixed public function dispatch(): mixed
{ {
$handlerProviders = $this->getHandlerProviders()->get($this->sourcePath, $this->method); $handlerProviders = HandlerProviders::get($this->sourcePath, $this->method);
if (empty($handlerProviders)) { if (empty($handlerProviders)) {
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404); throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
} }
+1
View File
@@ -37,6 +37,7 @@ class Response implements ResponseInterface
/** /**
* @param string $name * @param string $name
* @return mixed
*/ */
public function __get(string $name) public function __get(string $name)
{ {
+1 -3
View File
@@ -7,7 +7,6 @@ namespace Annotation\Route;
use Annotation\Attribute; use Annotation\Attribute;
use Http\Route\MiddlewareManager; use Http\Route\MiddlewareManager;
use ReflectionException; use ReflectionException;
use Kiri\Kiri;
use Http\IInterface\MiddlewareInterface ; use Http\IInterface\MiddlewareInterface ;
/** /**
@@ -49,8 +48,7 @@ use Http\IInterface\MiddlewareInterface ;
*/ */
public function execute(mixed $class, mixed $method = null): static public function execute(mixed $class, mixed $method = null): static
{ {
$middleware = Kiri::getDi()->get(MiddlewareManager::class); MiddlewareManager::addMiddlewares($class, $method, $this->middleware);
$middleware->addMiddlewares($class, $method, $this->middleware);
return $this; return $this;
} }