Files
kiri-core/HttpServer/Route/Middleware.php
T

144 lines
2.8 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019-03-20
* Time: 02:17
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace HttpServer\Route;
2020-12-16 15:20:51 +08:00
use Annotation\Route\After;
use Annotation\Route\Interceptor;
use Annotation\Route\Middleware as RMiddleware;
2020-08-31 01:27:08 +08:00
use Exception;
2020-12-16 15:20:51 +08:00
use Annotation\Route\Limits;
2020-08-31 01:27:08 +08:00
use HttpServer\Route\Dispatch\Dispatch;
2020-12-16 15:20:51 +08:00
use Snowflake\Snowflake;
2020-08-31 01:27:08 +08:00
/**
* Class Middleware
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Route
2020-08-31 01:27:08 +08:00
*/
class Middleware
{
/** @var array */
2020-10-29 18:17:25 +08:00
private array $middleWares = [];
2020-08-31 01:27:08 +08:00
/**
* @param $call
* @return $this
*/
2020-12-14 17:42:48 +08:00
public function set($call): static
2020-08-31 01:27:08 +08:00
{
$this->middleWares[] = $call;
return $this;
}
/**
* @param array $array
* @return $this
*/
2020-12-14 17:42:48 +08:00
public function setMiddleWares(array $array): static
2020-08-31 01:27:08 +08:00
{
$this->middleWares = $array;
return $this;
}
/**
2020-09-01 00:32:48 +08:00
* @param Node $node
2020-12-14 17:44:56 +08:00
* @return mixed
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-12-14 17:44:56 +08:00
public function getGenerate(Node $node): mixed
2020-08-31 01:27:08 +08:00
{
2020-12-16 16:41:47 +08:00
$this->set_attributes($node);
return $node->callback = Reduce::reduce(function () use ($node) {
return Dispatch::create($node->handler, func_get_args())->dispatch();
}, $this->annotation($node));
2020-08-31 01:27:08 +08:00
}
2020-09-01 00:32:48 +08:00
2020-12-16 15:20:51 +08:00
/**
* @param $node
* @throws Exception
*/
private function set_attributes(Node $node)
{
2020-12-16 16:34:15 +08:00
if (!is_array($node->handler)) {
return;
}
2020-12-16 15:20:51 +08:00
[$controller, $action] = $node->handler;
$attributes = Snowflake::app()->getAttributes();
2021-03-03 23:46:05 +08:00
$annotation = $attributes->getMethods(get_class($controller), $action);
if (empty($annotation)) {
2020-12-16 16:37:42 +08:00
return;
}
2021-03-04 00:30:33 +08:00
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);
2020-12-16 15:20:51 +08:00
}
}
}
2020-09-01 00:32:48 +08:00
/**
* @param Node $node
* @return array
*/
2020-12-14 17:42:48 +08:00
protected function annotation(Node $node): array
2020-09-01 00:32:48 +08:00
{
2021-02-08 17:20:43 +08:00
$middleWares = $node->getMiddleWares();
$middleWares = $this->annotation_limit($node, $middleWares);
2020-12-16 10:52:08 +08:00
$middleWares = $this->annotation_interceptor($node, $middleWares);
return $middleWares;
}
/**
* @param Node $node
* @param $middleWares
* @return array
*/
protected function annotation_interceptor(Node $node, $middleWares = []): array
{
2020-09-01 00:32:48 +08:00
if (!$node->hasInterceptor()) {
2020-12-16 10:52:08 +08:00
return $middleWares;
2020-09-01 00:32:48 +08:00
}
foreach ($node->getInterceptor() as $item) {
2020-09-07 17:19:02 +08:00
$middleWares[] = $item;
2020-09-01 00:32:48 +08:00
}
2020-12-16 10:52:08 +08:00
return $middleWares;
2020-09-07 02:15:51 +08:00
}
/**
* @param Node $node
* @param $middleWares
* @return array
*/
2020-12-16 10:52:08 +08:00
protected function annotation_limit(Node $node, $middleWares = []): array
2020-09-07 02:15:51 +08:00
{
if (!$node->hasLimits()) {
return $middleWares;
}
foreach ($node->getLimits() as $item) {
2020-09-07 17:19:02 +08:00
$middleWares[] = $item;
2020-09-07 02:15:51 +08:00
}
2020-09-01 00:32:48 +08:00
return $middleWares;
}
2020-08-31 01:27:08 +08:00
}