This commit is contained in:
2021-09-16 14:04:14 +08:00
parent 364e937aa4
commit 8c5d10dc3e
2 changed files with 555 additions and 545 deletions
+3 -6
View File
@@ -157,13 +157,11 @@ class Node
*/ */
private function injectMiddleware(string $method, $handler, $_injectParameters): void private function injectMiddleware(string $method, $handler, $_injectParameters): void
{ {
$callback = (new Pipeline())->overall($this->router->getMiddleware()) $this->callback[$method] = (new Pipeline())->overall($this->router->getMiddleware())
->through($this->middleware[$method] ?? []) ->through($this->middleware[$method] ?? [])
->through(MiddlewareManager::get($handler)) ->through(MiddlewareManager::get($handler))
->send($_injectParameters) ->send($_injectParameters)
->then($handler); ->then($handler);
HandlerProviders::add($method, $this->sourcePath, $callback);
} }
@@ -314,11 +312,10 @@ class Node
if (!in_array($request->getMethod(), $this->method)) { if (!in_array($request->getMethod(), $this->method)) {
throw new RequestException(Constant::STATUS_405_MESSAGE, 405); throw new RequestException(Constant::STATUS_405_MESSAGE, 405);
} }
$handlerProviders = HandlerProviders::get($this->sourcePath, $request->getMethod()); if (empty($this->callback[$request->getMethod()])) {
if (empty($handlerProviders)) {
throw new RequestException(Constant::STATUS_404_MESSAGE, 404); throw new RequestException(Constant::STATUS_404_MESSAGE, 404);
} }
return $handlerProviders->interpreter($request); return $this->callback[$request->getMethod()]->interpreter($request);
} }
} }
+20 -7
View File
@@ -15,7 +15,6 @@ use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
use Kiri\Kiri; use Kiri\Kiri;
use Psr\Http\Message\UriInterface;
use ReflectionException; use ReflectionException;
use Server\Constrict\RequestInterface; use Server\Constrict\RequestInterface;
use Throwable; use Throwable;
@@ -29,7 +28,7 @@ defined('ROUTER_HASH') or define('ROUTER_HASH', 2);
*/ */
class Router extends HttpService implements RouterInterface class Router extends HttpService implements RouterInterface
{ {
/** @var \Http\Route\Node[] $nodes */ /** @var Node[] $nodes */
public array $nodes = []; public array $nodes = [];
public array $groupTacks = []; public array $groupTacks = [];
public ?string $namespace = 'App\\Http\\Controllers'; public ?string $namespace = 'App\\Http\\Controllers';
@@ -151,7 +150,7 @@ class Router extends HttpService implements RouterInterface
* @param $root * @param $root
* @param $method * @param $method
* @param bool $create * @param bool $create
* @return array<\Http\Route\Node, array> * @return array<Node, array>
*/ */
private function getRootNode($root, $method, bool $create = true): array private function getRootNode($root, $method, bool $create = true): array
{ {
@@ -509,10 +508,25 @@ class Router extends HttpService implements RouterInterface
*/ */
public function radix_tree(RequestInterface $request): ?Node public function radix_tree(RequestInterface $request): ?Node
{ {
[$parent, $explode] = $this->getRootNode($request->getUri()->getPath(), $path = $request->getUri()->getPath();
$request->getMethod(), false);
[$parent, $explode] = $this->getRootNode($path, $request->getMethod(), false);
if ($parent && !empty($explode)) { if ($parent && !empty($explode)) {
/** @var \Http\Route\Node $parent */ $parent = $this->searchNode($explode, $parent);
}
return $parent;
}
/**
* @param $explode
* @param $parent
* @return null|Node
* @throws Exception
*/
private function searchNode($explode, $parent): ?Node
{
/** @var Node $parent */
foreach ($explode as $value) { foreach ($explode as $value) {
$node = $parent->findNode($value); $node = $parent->findNode($value);
if (!$node) { if (!$node) {
@@ -520,7 +534,6 @@ class Router extends HttpService implements RouterInterface
} }
$parent = $node; $parent = $node;
} }
}
return $parent; return $parent;
} }