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-02-23 14:16:08 +08:00
|
|
|
foreach ($annotation as $attribute) {
|
|
|
|
|
if ($attribute instanceof Interceptor) {
|
2021-03-03 18:35:04 +08:00
|
|
|
$node->addInterceptor($attribute->interceptor);
|
2020-12-17 10:17:27 +08:00
|
|
|
}
|
2021-02-23 14:16:08 +08:00
|
|
|
if ($attribute instanceof After) {
|
2021-03-03 18:35:04 +08:00
|
|
|
$node->addAfter($attribute->after);
|
2021-02-23 14:16:08 +08:00
|
|
|
}
|
|
|
|
|
if ($attribute instanceof RMiddleware) {
|
2021-03-03 18:35:04 +08:00
|
|
|
$node->addMiddleware($attribute->middleware);
|
2021-02-23 14:16:08 +08:00
|
|
|
}
|
|
|
|
|
if ($attribute instanceof Limits) {
|
2021-03-03 18:35:04 +08:00
|
|
|
$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
|
|
|
}
|