Files
kiri-core/Annotation/Route/Node.php
T

70 lines
1.5 KiB
PHP
Raw Normal View History

2020-12-15 12:24:38 +08:00
<?php
namespace Annotation\Route;
2020-12-15 18:52:50 +08:00
use HttpServer\IInterface\After;
use HttpServer\IInterface\Interceptor;
use HttpServer\IInterface\Limits;
use HttpServer\IInterface\Middleware;
2020-12-15 18:42:54 +08:00
use HttpServer\Route\Node as RNode;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
2020-12-15 12:24:38 +08:00
trait Node
{
/**
2020-12-15 18:42:54 +08:00
* @param RNode $node
* @return RNode
* @throws
2020-12-15 12:24:38 +08:00
*/
2020-12-15 18:42:54 +08:00
public function add(RNode $node): RNode
2020-12-15 12:24:38 +08:00
{
if (!empty($this->middleware)) {
2020-12-15 18:42:54 +08:00
$node->addMiddleware($this->reflectClass($this->middleware));
2020-12-15 12:24:38 +08:00
}
if (!empty($this->interceptor)) {
2020-12-15 18:42:54 +08:00
$node->addInterceptor($this->reflectClass($this->interceptor));
2020-12-15 12:24:38 +08:00
}
if (!empty($this->limits)) {
2020-12-15 18:42:54 +08:00
$node->addLimits($this->reflectClass($this->limits));
2020-12-15 12:24:38 +08:00
}
if (!empty($this->after)) {
2020-12-15 18:42:54 +08:00
$node->addAfter($this->reflectClass($this->after));
2020-12-15 12:24:38 +08:00
}
return $node;
}
2020-12-15 18:42:54 +08:00
/**
* @param array $classes
* @return array
* @throws ReflectionException
* @throws NotFindClassException
*/
private function reflectClass(array $classes): array
{
$di = Snowflake::getDi();
foreach ($classes as $key => $class) {
2020-12-15 18:52:50 +08:00
$object = $di->get($class);
if ($object instanceof Interceptor) {
$classes[$key] = [$object, 'Interceptor'];
}
if ($object instanceof Limits) {
$classes[$key] = [$object, 'next'];
}
if ($object instanceof After) {
$classes[$key] = [$object, 'onHandler'];
}
if ($object instanceof Middleware) {
$classes[$key] = [$object, 'onHandler'];
}
2020-12-15 18:42:54 +08:00
}
return $classes;
}
2020-12-15 12:24:38 +08:00
}