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

457 lines
8.3 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace HttpServer\Route;
2020-09-04 17:51:10 +08:00
use Closure;
2020-08-31 01:27:08 +08:00
use HttpServer\Http\Request;
use Exception;
use HttpServer\Application;
2020-11-17 16:45:08 +08:00
use HttpServer\Route\Annotation\Http;
2020-11-27 14:19:07 +08:00
use ReflectionException;
2020-09-08 13:58:22 +08:00
use Snowflake\Core\JSON;
2020-09-09 11:26:25 +08:00
use Snowflake\Event;
2020-11-27 14:19:07 +08:00
use Snowflake\Exception\NotFindClassException;
2020-08-31 12:38:32 +08:00
use Snowflake\Snowflake;
2020-09-09 11:44:14 +08:00
use Swoole\Coroutine;
2020-08-31 01:27:08 +08:00
/**
* Class Node
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Route
2020-08-31 01:27:08 +08:00
*/
class Node extends Application
{
2020-10-29 18:17:25 +08:00
public string $path = '';
public int $index = 0;
public string $method = '';
2020-08-31 01:27:08 +08:00
/** @var Node[] $childes */
2020-10-29 18:17:25 +08:00
public array $childes = [];
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
public array $group = [];
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
private string $_error = '';
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
public array $rules = [];
2020-10-30 01:22:50 +08:00
/** @var ?Closure|?array */
2020-11-27 14:19:07 +08:00
public Closure|array|null $handler;
2020-10-29 18:17:25 +08:00
public string $htmlSuffix = '.html';
public bool $enableHtmlSuffix = false;
public array $namespace = [];
public array $middleware = [];
2020-10-30 01:28:45 +08:00
2020-11-27 14:19:07 +08:00
/** @var array|Closure */
public Closure|array $callback = [];
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
private string $_alias = '';
2020-10-13 12:01:48 +08:00
2020-10-29 18:17:25 +08:00
private array $_interceptors = [];
private array $_after = [];
private array $_limits = [];
2020-08-31 22:33:50 +08:00
2020-08-31 01:27:08 +08:00
/**
* @param $handler
* @return Node
2020-09-04 18:47:22 +08:00
* @throws
2020-08-31 01:27:08 +08:00
*/
2020-10-30 01:17:08 +08:00
public function bindHandler($handler)
2020-08-31 01:27:08 +08:00
{
2020-10-21 11:11:42 +08:00
if ($handler instanceof Closure) {
$this->handler = $handler;
} else if (is_string($handler) && strpos($handler, '@') !== false) {
list($controller, $action) = explode('@', $handler);
if (!empty($this->namespace)) {
$controller = implode('\\', $this->namespace) . '\\' . $controller;
}
$this->handler = $this->getReflect($controller, $action);
} else if ($handler != null && !is_callable($handler, true)) {
$this->_error = 'Controller is con\'t exec.';
} else {
$this->handler = $handler;
}
2020-11-27 14:19:07 +08:00
return $this->restructure();
2020-08-31 01:27:08 +08:00
}
2020-09-01 00:32:48 +08:00
/**
* @return bool
*/
public function hasInterceptor()
{
return count($this->_interceptors) > 0;
}
2020-09-07 02:15:51 +08:00
/**
* @return bool
*/
public function hasLimits()
{
return count($this->_limits) > 0;
}
2020-09-09 11:44:14 +08:00
/**
* @param $response
* @return mixed|null
*/
2020-09-27 14:41:25 +08:00
public function afterDispatch($response = null)
2020-09-09 11:44:14 +08:00
{
2020-09-09 11:47:47 +08:00
return Coroutine::create(function ($request, $response) {
(Reduce::after($this->_after))($request, $response);
2020-09-09 11:48:31 +08:00
}, \request(), $response);
2020-09-09 11:44:14 +08:00
}
2020-09-01 00:32:48 +08:00
/**
* @return array
*/
public function getInterceptor()
{
return $this->_interceptors;
}
2020-09-07 02:15:51 +08:00
2020-09-09 11:44:14 +08:00
/**
* @return array
*/
public function getAfters()
{
return $this->_after;
}
/**
* @return bool
*/
public function hasAfter()
{
return count($this->_after) > 0;
}
2020-09-07 02:15:51 +08:00
/**
* @return array
*/
public function getLimits()
{
return $this->_limits;
}
2020-08-31 01:27:08 +08:00
/**
* @param $request
* @return bool
*/
public function methodAllow(Request $request)
{
if ($this->method == $request->getMethod()) {
return true;
}
return $this->method == 'any';
}
/**
* @return bool
* @throws Exception
*/
public function checkSuffix()
{
if ($this->enableHtmlSuffix) {
$url = request()->getUri();
$nowLength = strlen($this->htmlSuffix);
if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) {
return false;
}
}
return $this->checkRule();
}
/**
* @return bool
* @throws Exception
*/
private function checkRule()
{
if (empty($this->rules)) {
return true;
}
foreach ($this->rules as $rule) {
if (!isset($rule['class'])) {
$rule['class'] = Filter::class;
}
/** @var Filter $object */
2020-08-31 12:38:32 +08:00
$object = Snowflake::createObject($rule);
2020-08-31 01:27:08 +08:00
if (!$object->handler()) {
return false;
};
}
return true;
}
/**
* @param string $controller
* @param string $action
* @return null|array
* @throws Exception
*/
private function getReflect(string $controller, string $action)
{
try {
2020-08-31 22:33:50 +08:00
$reflect = Snowflake::getDi()->getReflect($controller);
2020-08-31 01:27:08 +08:00
if (!$reflect->isInstantiable()) {
throw new Exception($controller . ' Class is con\'t Instantiable.');
}
if (!empty($action) && !$reflect->hasMethod($action)) {
throw new Exception('method ' . $action . ' not exists at ' . $controller . '.');
}
2020-08-31 22:33:50 +08:00
2020-11-17 16:45:08 +08:00
$annotation = Snowflake::app()->annotation->http;
if (!empty($annotations = $annotation->getAnnotation(Http::class))) {
2020-09-04 17:51:10 +08:00
$annotation->read($this, $reflect, $action, $annotations);
2020-08-31 22:33:50 +08:00
}
2020-08-31 01:27:08 +08:00
return [$reflect->newInstance(), $action];
2020-11-06 16:47:17 +08:00
} catch (\Throwable $exception) {
2020-08-31 01:27:08 +08:00
$this->_error = $exception->getMessage();
$this->error($exception->getMessage(), 'router');
return null;
}
}
2020-09-01 00:32:48 +08:00
2020-09-04 17:51:10 +08:00
/**
* @param Closure|array|string $handler
* @throws Exception
*/
2020-11-27 14:19:07 +08:00
public function addInterceptor(Closure|string|array $handler)
2020-09-04 17:51:10 +08:00
{
$this->_interceptors[] = $handler;
2020-11-27 14:19:07 +08:00
$this->restructure();
2020-09-04 17:51:10 +08:00
}
2020-09-08 11:46:22 +08:00
/**
* @param Closure|array|string $handler
* @throws Exception
*/
2020-11-27 14:19:07 +08:00
public function addAfter(Closure|string|array $handler)
2020-09-08 11:46:22 +08:00
{
2020-09-09 11:46:03 +08:00
$this->_after[] = $handler;
2020-11-27 14:19:07 +08:00
$this->restructure();
2020-09-08 11:46:22 +08:00
}
2020-09-07 02:15:51 +08:00
/**
* @param Closure|array|string $handler
* @throws Exception
*/
2020-11-27 14:19:07 +08:00
public function addLimits(Closure|string|array $handler)
2020-09-07 02:15:51 +08:00
{
$this->_limits[] = $handler;
2020-11-27 14:19:07 +08:00
$this->restructure();
2020-09-07 02:15:51 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @return string
* 错误信息
*/
public function getError()
{
return $this->_error;
}
/**
* @param Node $node
* @param string $field
* @return Node
*/
public function addChild(Node $node, string $field)
{
/** @var Node $oLod */
$oLod = $this->childes[$field] ?? null;
if (!empty($oLod)) {
$node = $oLod;
}
$this->childes[$field] = $node;
return $this->childes[$field];
}
/**
* @param $rule
* @return $this
*/
public function filter($rule)
{
if (empty($rule)) {
return $this;
}
if (!isset($rule[0])) {
$rule = [$rule];
}
foreach ($rule as $value) {
if (empty($value)) {
continue;
}
$this->rules[] = $value;
}
return $this;
}
/**
* @param string $search
* @return Node|mixed
*/
public function findNode(string $search)
{
if (empty($this->childes)) {
return null;
}
if (isset($this->childes[$search])) {
return $this->childes[$search];
}
$_searchMatch = '/<(\w+)?:(.+)?>/';
foreach ($this->childes as $key => $val) {
if (preg_match($_searchMatch, $key, $match)) {
\Input()->addGetParam($match[1] ?? '--', $search);
return $this->childes[$key];
}
}
return null;
}
/**
* @param string $alias
* @return $this
* 别称
*/
public function alias(string $alias)
{
2020-10-13 12:01:48 +08:00
$this->_alias = $alias;
2020-08-31 01:27:08 +08:00
return $this;
}
2020-10-13 12:01:48 +08:00
/**
* @return string
*/
public function getAlias()
{
return $this->_alias;
}
2020-08-31 01:27:08 +08:00
/**
* @param int $limit
* @param int $duration
* @param bool $isBindConsumer
* @return $this
* @throws Exception
*/
public function limits(int $limit, int $duration = 60, bool $isBindConsumer = false)
{
2020-09-03 11:39:20 +08:00
$limits = Snowflake::app()->getLimits();
2020-08-31 01:27:08 +08:00
$limits->addLimits($this->path, $limit, $duration, $isBindConsumer);
2020-11-27 14:19:07 +08:00
return $this->restructure();
2020-08-31 01:27:08 +08:00
}
2020-10-20 15:38:45 +08:00
2020-08-31 01:27:08 +08:00
/**
* @param $middles
* @throws
*/
public function bindMiddleware(array $middles)
{
$_tmp = [];
if (empty($middles)) {
return;
}
2020-09-03 17:32:17 +08:00
$this->middleware = $this->each($middles, $_tmp);
2020-11-27 14:19:07 +08:00
$this->restructure();
2020-08-31 01:27:08 +08:00
}
2020-09-04 17:51:10 +08:00
/**
2020-11-27 14:19:07 +08:00
* @param array|Closure|string $class
* @throws ReflectionException
* @throws NotFindClassException
2020-09-04 17:51:10 +08:00
* @throws Exception
*/
2020-11-27 14:19:07 +08:00
public function addMiddleware(Closure|string|array $class)
2020-09-04 17:51:10 +08:00
{
2020-10-15 11:59:42 +08:00
if (!is_callable($class, true)) {
return;
}
2020-09-04 18:13:06 +08:00
if (is_string($class)) {
$class = Snowflake::createObject($class);
if (!($class instanceof \HttpServer\IInterface\Middleware)) {
return;
}
$class = [$class, 'handler'];
}
2020-09-04 17:51:10 +08:00
$this->middleware[] = $class;
2020-11-27 14:19:07 +08:00
$this->restructure();
2020-09-04 17:51:10 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @throws Exception
*/
2020-09-03 17:37:53 +08:00
private function restructure()
2020-08-31 01:27:08 +08:00
{
2020-09-03 17:37:53 +08:00
if (empty($this->handler)) {
return $this;
2020-08-31 01:27:08 +08:00
}
2020-09-03 17:37:53 +08:00
$made = Snowflake::createObject(Middleware::class);
$made->setMiddleWares($this->middleware);
$made->getGenerate($this);
2020-08-31 01:27:08 +08:00
return $this;
}
2020-09-08 11:53:09 +08:00
/**
* @return mixed
2020-10-20 15:37:21 +08:00
* @throws Exception
2020-09-08 11:53:09 +08:00
*/
public function dispatch()
{
2020-10-20 15:37:21 +08:00
if (empty($node->callback)) {
return JSON::to(404, $node->_error ?? 'Page not found.');
2020-09-08 13:58:22 +08:00
}
2020-10-20 15:37:21 +08:00
return call_user_func($node->callback, \request());
2020-09-08 11:53:09 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param $array
* @param $_temp
* @return array
* @throws Exception
*/
private function each($array, $_temp)
{
2020-10-15 10:53:35 +08:00
if (!is_array($array)) {
2020-08-31 01:27:08 +08:00
return $_temp;
}
foreach ($array as $class) {
2020-10-15 10:53:35 +08:00
if (is_array($class)) {
2020-09-04 17:51:10 +08:00
$_temp = $this->each($class, $_temp);
2020-10-15 10:53:35 +08:00
continue;
}
if (!class_exists($class)) {
continue;
2020-08-31 01:27:08 +08:00
}
2020-10-15 10:53:35 +08:00
$_temp[] = Snowflake::createObject($class);
2020-08-31 01:27:08 +08:00
}
return $_temp;
}
}