1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
20
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
21
|
|
|
use Sunrise\Http\Router\Exception\ExceptionInterface; |
22
|
|
|
use Sunrise\Http\Router\Exception\MethodNotAllowedException; |
23
|
|
|
use Sunrise\Http\Router\Exception\RouteNotFoundException; |
24
|
|
|
use Sunrise\Http\Router\RequestHandler\RoutableRequestHandler; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Import functions |
28
|
|
|
*/ |
29
|
|
|
use function array_keys; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Router |
33
|
|
|
*/ |
34
|
|
|
class Router extends RouteCollection implements MiddlewareInterface, RequestHandlerInterface |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Server Request attribute name for routing error instance |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public const ATTR_NAME_FOR_ROUTING_ERROR = '@routing-error'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Generates a URI for the given named route |
46
|
|
|
* |
47
|
|
|
* @param string $name |
48
|
|
|
* @param array $attributes |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function generateUri(string $name, array $attributes = []) : string |
53
|
|
|
{ |
54
|
|
|
$route = $this->getRoute($name); |
55
|
|
|
|
56
|
|
|
$attributes += $route->getAttributes(); |
57
|
|
|
|
58
|
|
|
return path_build($route->getPath(), $attributes, false); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Looks for a route that matches the given request |
63
|
|
|
* |
64
|
|
|
* @param ServerRequestInterface $request |
65
|
|
|
* |
66
|
|
|
* @return RouteInterface |
67
|
|
|
* |
68
|
|
|
* @throws MethodNotAllowedException |
69
|
|
|
* @throws RouteNotFoundException |
70
|
|
|
*/ |
71
|
9 |
|
public function match(ServerRequestInterface $request) : RouteInterface |
72
|
|
|
{ |
73
|
9 |
|
$routes = []; |
74
|
9 |
|
foreach ($this->getRoutes() as $route) { |
75
|
9 |
|
foreach ($route->getMethods() as $method) { |
76
|
9 |
|
$routes[$method][] = $route; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
9 |
|
$method = $request->getMethod(); |
81
|
9 |
|
if (!isset($routes[$method])) { |
82
|
3 |
|
throw new MethodNotAllowedException( |
83
|
3 |
|
array_keys($routes) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
$target = $request->getUri()->getPath(); |
88
|
6 |
|
foreach ($routes[$method] as $route) { |
89
|
6 |
|
if (path_match($route->getPath(), $target, $attributes)) { |
|
|
|
|
90
|
6 |
|
return $route->withAddedAttributes($attributes); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
throw new RouteNotFoundException(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
*/ |
100
|
6 |
|
public function handle(ServerRequestInterface $request) : ResponseInterface |
101
|
|
|
{ |
102
|
6 |
|
$route = $this->match($request); |
103
|
|
|
|
104
|
2 |
|
$requestHandler = new RoutableRequestHandler($route); |
105
|
|
|
|
106
|
2 |
|
return $requestHandler->handle($request); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
3 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
113
|
|
|
{ |
114
|
|
|
try { |
115
|
3 |
|
return $this->handle($request); |
116
|
2 |
|
} catch (ExceptionInterface $e) { |
117
|
2 |
|
return $handler->handle( |
118
|
2 |
|
$request->withAttribute(self::ATTR_NAME_FOR_ROUTING_ERROR, $e) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|