This commit is contained in:
2021-03-05 16:10:36 +08:00
parent cfeaf36b27
commit 7c6b5fb71f
2 changed files with 39 additions and 36 deletions
-32
View File
@@ -54,44 +54,12 @@ class Middleware
*/ */
public function getGenerate(Node $node): mixed public function getGenerate(Node $node): mixed
{ {
$this->set_attributes($node);
return $node->callback = Reduce::reduce(function () use ($node) { return $node->callback = Reduce::reduce(function () use ($node) {
return Dispatch::create($node->handler, func_get_args())->dispatch(); return Dispatch::create($node->handler, func_get_args())->dispatch();
}, $this->annotation($node)); }, $this->annotation($node));
} }
/**
* @param $node
* @throws Exception
*/
private function set_attributes(Node $node)
{
if (!is_array($node->handler)) {
return;
}
[$controller, $action] = $node->handler;
$attributes = Snowflake::app()->getAttributes();
$annotation = $attributes->getMethods(get_class($controller), $action);
if (empty($annotation)) {
return;
}
foreach ($annotation as $name => $attribute) {
if ($attribute instanceof Interceptor) {
$node->addInterceptor($attribute->interceptor);
}
if ($attribute instanceof After) {
$node->addAfter($attribute->after);
}
if ($attribute instanceof RMiddleware) {
$node->addMiddleware($attribute->middleware);
}
if ($attribute instanceof Limits) {
$node->addLimits($attribute->limits);
}
}
}
/** /**
* @param Node $node * @param Node $node
+39 -4
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace HttpServer\Route; namespace HttpServer\Route;
use Annotation\Route\Middleware as RMiddleware;
use Closure; use Closure;
use HttpServer\Abstracts\HttpService; use HttpServer\Abstracts\HttpService;
use HttpServer\Http\Request; use HttpServer\Http\Request;
@@ -79,7 +80,7 @@ class Node extends HttpService
} else { } else {
$this->handler = $handler; $this->handler = $handler;
} }
return $this; return $this->restructure();
} }
@@ -212,6 +213,9 @@ class Node extends HttpService
if (!empty($action) && !$reflect->hasMethod($action)) { if (!empty($action) && !$reflect->hasMethod($action)) {
throw new Exception('method ' . $action . ' not exists at ' . $controller . '.'); throw new Exception('method ' . $action . ' not exists at ' . $controller . '.');
} }
$this->annotationInject($reflect->getName(), $action);
return [$reflect->newInstance(), $action]; return [$reflect->newInstance(), $action];
} catch (Throwable $exception) { } catch (Throwable $exception) {
$this->_error = $exception->getMessage(); $this->_error = $exception->getMessage();
@@ -221,6 +225,40 @@ class Node extends HttpService
} }
/**
* @param string $className
* @param string $action
* @return $this
* @throws ComponentException
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
private function annotationInject(string $className, string $action): static
{
$attributes = Snowflake::app()->getAttributes();
$annotation = $attributes->getMethods($className, $action);
if (empty($annotation)) {
return $this;
}
foreach ($annotation as $name => $attribute) {
if ($attribute instanceof \Annotation\Route\Interceptor) {
$this->addInterceptor($attribute->interceptor);
}
if ($attribute instanceof \Annotation\Route\After) {
$this->addAfter($attribute->after);
}
if ($attribute instanceof RMiddleware) {
$this->addMiddleware($attribute->middleware);
}
if ($attribute instanceof \Annotation\Route\Limits) {
$this->addLimits($attribute->limits);
}
}
return $this;
}
/** /**
* @param Closure|array|string $handler * @param Closure|array|string $handler
* @throws Exception * @throws Exception
@@ -471,9 +509,6 @@ class Node extends HttpService
*/ */
public function dispatch(): mixed public function dispatch(): mixed
{ {
if (!empty($this->callback)) {
return $this->runWith(...func_get_args());
}
if (empty($this->restructure()->callback)) { if (empty($this->restructure()->callback)) {
return Json::to(404, $this->errorMsg()); return Json::to(404, $this->errorMsg());
} }