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
+39 -4
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace HttpServer\Route;
use Annotation\Route\Middleware as RMiddleware;
use Closure;
use HttpServer\Abstracts\HttpService;
use HttpServer\Http\Request;
@@ -79,7 +80,7 @@ class Node extends HttpService
} else {
$this->handler = $handler;
}
return $this;
return $this->restructure();
}
@@ -212,6 +213,9 @@ class Node extends HttpService
if (!empty($action) && !$reflect->hasMethod($action)) {
throw new Exception('method ' . $action . ' not exists at ' . $controller . '.');
}
$this->annotationInject($reflect->getName(), $action);
return [$reflect->newInstance(), $action];
} catch (Throwable $exception) {
$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
* @throws Exception
@@ -471,9 +509,6 @@ class Node extends HttpService
*/
public function dispatch(): mixed
{
if (!empty($this->callback)) {
return $this->runWith(...func_get_args());
}
if (empty($this->restructure()->callback)) {
return Json::to(404, $this->errorMsg());
}