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
98 lines (83 loc) · 3.15 KB
/
RouteHandler.php
File metadata and controls
98 lines (83 loc) · 3.15 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
<?php
namespace FrameworkX;
use FastRoute\DataGenerator\GroupCountBased as RouteGenerator;
use FastRoute\Dispatcher\GroupCountBased as RouteDispatcher;
use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std as RouteParser;
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 (!\is_callable($handler)) {
$handlers[$i] = $container->callable($handler);
}
}
$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($request);
}
if ($this->routeDispatcher === null) {
$this->routeDispatcher = new RouteDispatcher($this->routeCollector->getData());
}
$routeInfo = $this->routeDispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
switch ($routeInfo[0]) {
case \FastRoute\Dispatcher::NOT_FOUND:
return $this->errorHandler->requestNotFound($request);
case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
return $this->errorHandler->requestMethodNotAllowed($routeInfo[1]);
case \FastRoute\Dispatcher::FOUND:
$handler = $routeInfo[1];
$vars = $routeInfo[2];
foreach ($vars as $key => $value) {
$request = $request->withAttribute($key, rawurldecode($value));
}
return $handler($request);
}
} // @codeCoverageIgnore
}