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

96 lines
1.6 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-15 15:14:25 +08:00
// declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace HttpServer\Route;
use Exception;
use HttpServer\Route\Dispatch\Dispatch;
/**
* 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 */
private $middleWares = [];
/**
* @param $call
* @return $this
*/
public function set($call)
{
$this->middleWares[] = $call;
return $this;
}
/**
* @param array $array
* @return $this
*/
public function setMiddleWares(array $array)
{
$this->middleWares = $array;
return $this;
}
/**
2020-09-01 00:32:48 +08:00
* @param Node $node
2020-08-31 01:27:08 +08:00
* @return mixed
* @throws Exception
*/
2020-10-15 15:14:25 +08:00
public function getGenerate(Node $node)
2020-08-31 01:27:08 +08:00
{
2020-09-04 18:49:36 +08:00
$last = function ($passable) use ($node) {
2020-09-09 11:05:28 +08:00
return Dispatch::create($node->handler, $passable)->dispatch();
2020-09-04 18:49:36 +08:00
};
2020-09-09 10:42:16 +08:00
return $node->callback = Reduce::reduce($last, $this->annotation($node));
2020-08-31 01:27:08 +08:00
}
2020-09-01 00:32:48 +08:00
/**
* @param Node $node
* @return array
*/
2020-10-15 15:14:25 +08:00
protected function annotation(Node $node)
2020-09-01 00:32:48 +08:00
{
$middleWares = $this->middleWares;
2020-09-08 11:46:22 +08:00
$this->middleWares = [];
2020-09-01 00:32:48 +08:00
if (!$node->hasInterceptor()) {
2020-09-07 02:15:51 +08:00
return $this->annotation_limit($node, $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-09-07 02:15:51 +08:00
return $this->annotation_limit($node, $middleWares);
}
/**
* @param Node $node
* @param $middleWares
* @return array
*/
2020-10-15 15:14:25 +08:00
protected function annotation_limit(Node $node, $middleWares)
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
}