modify
This commit is contained in:
@@ -9,10 +9,10 @@ use Snowflake\Abstracts\BaseObject;
|
||||
|
||||
|
||||
/**
|
||||
* Class Middlewares
|
||||
* Class MiddlewareManager
|
||||
* @package HttpServer\Route
|
||||
*/
|
||||
class Middlewares extends BaseObject
|
||||
class MiddlewareManager extends BaseObject
|
||||
{
|
||||
|
||||
private static array $_middlewares = [];
|
||||
@@ -26,7 +26,7 @@ class Middlewares extends BaseObject
|
||||
public function addMiddlewares($class, $method, array|string $middlewares)
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = get_class($class);
|
||||
$class = $class::class;
|
||||
}
|
||||
if (!isset(static::$_middlewares[$class . '::' . $method])) {
|
||||
static::$_middlewares[$class . '::' . $method] = [];
|
||||
@@ -45,6 +45,20 @@ class Middlewares extends BaseObject
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMiddleware($class, $method)
|
||||
{
|
||||
if (is_object($class)) {
|
||||
$class = $class::class;
|
||||
}
|
||||
return isset(static::$_middlewares[$class . '::' . $method]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @param $method
|
||||
@@ -9,6 +9,7 @@ use Annotation\Route\RpcProducer;
|
||||
use Closure;
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\HttpService;
|
||||
use HttpServer\Controller;
|
||||
use HttpServer\Http\Context;
|
||||
use HttpServer\Http\Request;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
@@ -95,7 +96,7 @@ class Node extends HttpService
|
||||
$this->handler = $handler;
|
||||
}
|
||||
if (!empty($this->handler) && is_array($this->handler)) {
|
||||
$this->callback = di(Middlewares::class)->callerMiddlewares(
|
||||
$this->callback = di(MiddlewareManager::class)->callerMiddlewares(
|
||||
$this->handler[0], $this->handler[1], $this->createDispatch()
|
||||
);
|
||||
}
|
||||
@@ -114,10 +115,12 @@ class Node extends HttpService
|
||||
|
||||
/** @var Aop $aop */
|
||||
$aop = Snowflake::app()->get('aop');
|
||||
if (!is_array($this->handler) || !$aop->hasAop($this->handler)) {
|
||||
if ($this->handler instanceof Closure || !$aop->hasAop($this->handler)) {
|
||||
return static function () use ($application) {
|
||||
$dispatchParam = Context::getContext('dispatch-param', [\request()]);
|
||||
|
||||
if (is_array($this->handler)){
|
||||
Snowflake::injectProperty($this->handler[0]);
|
||||
}
|
||||
return call_user_func($application->handler, ...$dispatchParam);
|
||||
};
|
||||
}
|
||||
@@ -129,7 +132,9 @@ class Node extends HttpService
|
||||
$dispatchParam = Context::getContext('dispatch-param', [\request()]);
|
||||
|
||||
$asp = $reflect->newInstance($this->handler);
|
||||
|
||||
if (is_array($this->handler)){
|
||||
Snowflake::injectProperty($this->handler[0]);
|
||||
}
|
||||
call_user_func($callback, $asp, $dispatchParam);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -370,7 +370,7 @@ class Router extends HttpService implements RouterInterface
|
||||
$array[] = $this->getMiddlewareInstance($value);
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
return array_filter($array);
|
||||
}
|
||||
|
||||
|
||||
@@ -382,15 +382,14 @@ class Router extends HttpService implements RouterInterface
|
||||
*/
|
||||
private function getMiddlewareInstance($value): null|Closure|array
|
||||
{
|
||||
if (is_string($value)) {
|
||||
$value = Snowflake::createObject($value);
|
||||
if (!($value instanceof Middleware)) {
|
||||
return null;
|
||||
}
|
||||
return [$value, 'onHandler'];
|
||||
} else {
|
||||
if (!is_string($value)) {
|
||||
return $value;
|
||||
}
|
||||
$value = Snowflake::createObject($value);
|
||||
if (!($value instanceof Middleware)) {
|
||||
return null;
|
||||
}
|
||||
return [$value, 'onHandler'];
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user