forked from clue/framework-x
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteHandler.php
More file actions
113 lines (94 loc) · 3.83 KB
/
Copy pathRouteHandler.php
File metadata and controls
113 lines (94 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace FrameworkX\Io;
use FastRoute\DataGenerator\GroupCountBased as RouteGenerator;
use FastRoute\Dispatcher\GroupCountBased as RouteDispatcher;
use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std as RouteParser;
use FrameworkX\AccessLogHandler;
use FrameworkX\Container;
use FrameworkX\ErrorHandler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Promise\PromiseInterface;
/**
* @internal
*/
class RouteHandler
{
/** @var RouteCollector */
private $routeCollector;
/** @var ?RouteDispatcher */
private $routeDispatcher;
/** @var ErrorHandler */
private $errorHandler;
/** @var Container */
private $container;
public function __construct(Container $container = null)
{
$this->routeCollector = new RouteCollector(new RouteParser(), new RouteGenerator());
$this->errorHandler = new ErrorHandler();
$this->container = $container ?? new Container();
}
/**
* @param string[] $methods
* @param string $route
* @param callable|class-string $handler
* @param callable|class-string ...$handlers
*/
public function map(array $methods, string $route, $handler, ...$handlers): void
{
if ($handlers) {
\array_unshift($handlers, $handler);
\end($handlers);
} else {
$handlers = [$handler];
}
$last = key($handlers);
$container = $this->container;
foreach ($handlers as $i => $handler) {
if ($handler instanceof Container && $i !== $last) {
$container = $handler;
unset($handlers[$i]);
} elseif ($handler instanceof AccessLogHandler || $handler === AccessLogHandler::class) {
throw new \TypeError('AccessLogHandler may currently only be passed as a global middleware');
} elseif (!\is_callable($handler)) {
$handlers[$i] = $container->callable($handler);
}
}
/** @var non-empty-array<callable> $handlers */
$handler = \count($handlers) > 1 ? new MiddlewareHandler(array_values($handlers)) : \reset($handlers);
$this->routeDispatcher = null;
$this->routeCollector->addRoute($methods, $route, $handler);
}
/**
* @return ResponseInterface|PromiseInterface<ResponseInterface>|\Generator
*/
public function __invoke(ServerRequestInterface $request)
{
if ($request->getRequestTarget()[0] !== '/' && $request->getRequestTarget() !== '*') {
return $this->errorHandler->requestProxyUnsupported();
}
if ($this->routeDispatcher === null) {
$this->routeDispatcher = new RouteDispatcher($this->routeCollector->getData());
}
$routeInfo = $this->routeDispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
assert(\is_array($routeInfo) && isset($routeInfo[0]));
// happy path: matching route found, assign route attributes and invoke request handler
if ($routeInfo[0] === \FastRoute\Dispatcher::FOUND) {
$handler = $routeInfo[1];
$vars = $routeInfo[2];
foreach ($vars as $key => $value) {
$request = $request->withAttribute($key, rawurldecode($value));
}
return $handler($request);
}
// no matching route found: report error `404 Not Found`
if ($routeInfo[0] === \FastRoute\Dispatcher::NOT_FOUND) {
return $this->errorHandler->requestNotFound();
}
// unexpected request method for route: report error `405 Method Not Allowed`
assert($routeInfo[0] === \FastRoute\Dispatcher::METHOD_NOT_ALLOWED);
assert(\is_array($routeInfo[1]) && \count($routeInfo[1]) > 0);
return $this->errorHandler->requestMethodNotAllowed($routeInfo[1]);
}
}