111
This commit is contained in:
@@ -11,7 +11,7 @@ use Kiri\Abstracts\BaseObject;
|
||||
class HandlerProviders extends BaseObject
|
||||
{
|
||||
|
||||
private array $handlers = [];
|
||||
private static array $handlers = [];
|
||||
|
||||
|
||||
/**
|
||||
@@ -19,9 +19,9 @@ class HandlerProviders extends BaseObject
|
||||
* @param $method
|
||||
* @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 $handler
|
||||
*/
|
||||
public function add($method, $path, $handler)
|
||||
public static function add($method, $path, $handler)
|
||||
{
|
||||
$this->handlers[$method][$path] = $handler;
|
||||
static::$handlers[$method][$path] = $handler;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class MiddlewareManager extends BaseObject
|
||||
* @param $method
|
||||
* @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)) {
|
||||
$class = $class::class;
|
||||
@@ -51,7 +51,7 @@ class MiddlewareManager extends BaseObject
|
||||
* @param $method
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMiddleware($class, $method): bool
|
||||
public static function hasMiddleware($class, $method): bool
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
@@ -66,7 +66,7 @@ class MiddlewareManager extends BaseObject
|
||||
* @param $caller
|
||||
* @return mixed
|
||||
*/
|
||||
public function callerMiddlewares($class, $method, $caller): mixed
|
||||
public static function callerMiddlewares($class, $method, $caller): mixed
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
@@ -91,7 +91,7 @@ class MiddlewareManager extends BaseObject
|
||||
* @param Closure $caller
|
||||
* @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 function ($passable) use ($stack, $pipe) {
|
||||
|
||||
@@ -129,27 +129,16 @@ class Node
|
||||
*/
|
||||
private function injectMiddleware($handler): static
|
||||
{
|
||||
$manager = di(MiddlewareManager::class);
|
||||
if (!($handler instanceof Closure)) {
|
||||
$callback = $this->injectControllerMiddleware($manager, $handler);
|
||||
$callback = $this->injectControllerMiddleware($handler);
|
||||
} 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 HandlerProviders
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function getHandlerProviders(): HandlerProviders
|
||||
{
|
||||
return Kiri::getDi()->get(HandlerProviders::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $manager
|
||||
* @param $handler
|
||||
@@ -157,10 +146,10 @@ class Node
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function injectControllerMiddleware($manager, $handler): mixed
|
||||
private function injectControllerMiddleware($handler): mixed
|
||||
{
|
||||
$manager->addMiddlewares($handler[0], $handler[1], $this->middleware);
|
||||
return $manager->callerMiddlewares(
|
||||
MiddlewareManager::addMiddlewares($handler[0], $handler[1], $this->middleware);
|
||||
return MiddlewareManager::callerMiddlewares(
|
||||
$handler[0], $handler[1], $this->aopHandler($this->getAop($handler), $handler)
|
||||
);
|
||||
}
|
||||
@@ -171,10 +160,10 @@ class Node
|
||||
* @param $handler
|
||||
* @return mixed
|
||||
*/
|
||||
private function injectClosureMiddleware($manager, $handler): mixed
|
||||
private function injectClosureMiddleware($handler): mixed
|
||||
{
|
||||
if (!empty($this->middleware)) {
|
||||
return $manager->closureMiddlewares($this->middleware, $this->normalHandler($handler));
|
||||
return MiddlewareManager::closureMiddlewares($this->middleware, $this->normalHandler($handler));
|
||||
} else {
|
||||
return $this->normalHandler($handler);
|
||||
}
|
||||
@@ -389,7 +378,7 @@ class Node
|
||||
*/
|
||||
public function dispatch(): mixed
|
||||
{
|
||||
$handlerProviders = $this->getHandlerProviders()->get($this->sourcePath, $this->method);
|
||||
$handlerProviders = HandlerProviders::get($this->sourcePath, $this->method);
|
||||
if (empty($handlerProviders)) {
|
||||
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ class Response implements ResponseInterface
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get(string $name)
|
||||
{
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace Annotation\Route;
|
||||
use Annotation\Attribute;
|
||||
use Http\Route\MiddlewareManager;
|
||||
use ReflectionException;
|
||||
use Kiri\Kiri;
|
||||
use Http\IInterface\MiddlewareInterface ;
|
||||
|
||||
/**
|
||||
@@ -49,8 +48,7 @@ use Http\IInterface\MiddlewareInterface ;
|
||||
*/
|
||||
public function execute(mixed $class, mixed $method = null): static
|
||||
{
|
||||
$middleware = Kiri::getDi()->get(MiddlewareManager::class);
|
||||
$middleware->addMiddlewares($class, $method, $this->middleware);
|
||||
MiddlewareManager::addMiddlewares($class, $method, $this->middleware);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user