1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace PTS\PSR15Routing; |
5
|
|
|
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use PTS\Tools\Collection; |
8
|
|
|
use PTS\Tools\DuplicateKeyException; |
9
|
|
|
|
10
|
|
|
class Router extends Collection |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $name |
14
|
|
|
* @param Route $route |
15
|
|
|
* @param int $priority |
16
|
|
|
* |
17
|
|
|
* @return Router |
18
|
|
|
* @throws DuplicateKeyException |
19
|
|
|
*/ |
20
|
3 |
|
public function add(string $name, Route $route, int $priority = 50): self |
21
|
|
|
{ |
22
|
3 |
|
return $this->addItem($name, $route, $priority); |
23
|
|
|
} |
24
|
|
|
|
25
|
1 |
|
public function remove(string $name): self |
26
|
|
|
{ |
27
|
1 |
|
return $this->removeItemWithoutPriority($name); |
28
|
|
|
} |
29
|
|
|
|
30
|
3 |
|
public function getRoutes(bool $sortByPriority = true): array |
31
|
|
|
{ |
32
|
3 |
|
return $this->getFlatItems($sortByPriority); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ServerRequestInterface $request |
37
|
|
|
* |
38
|
|
|
* @return Route |
39
|
|
|
* |
40
|
|
|
* @throws NotFoundRouteException |
41
|
|
|
*/ |
42
|
2 |
|
public function match(ServerRequestInterface $request): Route |
43
|
|
|
{ |
44
|
2 |
|
$uri = $request->getUri()->getPath(); |
45
|
2 |
|
$method = $request->getMethod(); |
46
|
|
|
|
47
|
2 |
|
foreach ($this->getRoutes() as $route) { |
48
|
2 |
|
$activeRoute = $this->matchRoute($route, $uri, $method); |
49
|
2 |
|
if ($activeRoute !== null) { |
50
|
2 |
|
return $activeRoute; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
throw new NotFoundRouteException('Route not found'); |
55
|
|
|
} |
56
|
|
|
|
57
|
12 |
|
protected function isAllowMethod(Route $route, string $method): bool |
58
|
|
|
{ |
59
|
12 |
|
$allows = $route->getMethods(); |
60
|
12 |
|
return \count($allows) ? \in_array($method, $allows, true) : true; |
61
|
|
|
} |
62
|
|
|
|
63
|
5 |
|
protected function matchRoute(Route $route, string $pathUrl, string $method): ?Route |
64
|
|
|
{ |
65
|
5 |
|
if (!$this->isAllowMethod($route, $method)) { |
66
|
1 |
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
5 |
|
$regexp = $this->makeRegExp($route); |
70
|
|
|
|
71
|
5 |
|
if (preg_match('~^'.$regexp.'$~Uiu', $pathUrl, $values)) { |
72
|
3 |
|
$filterValues = array_filter(array_keys($values), '\is_string'); |
73
|
3 |
|
$matches = array_intersect_key($values, array_flip($filterValues)); |
74
|
3 |
|
return $route->setMatches($matches); |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
8 |
|
public function makeRegExp(Route $route): string |
81
|
|
|
{ |
82
|
8 |
|
$regexp = $route->getPath(); |
83
|
8 |
|
$restrictions = $route->getRestrictions(); |
84
|
8 |
|
$placeholders = []; |
85
|
|
|
|
86
|
8 |
|
if (preg_match_all('~{(.*)}~Uu', $regexp, $placeholders)) { |
87
|
6 |
|
foreach ($placeholders[0] as $index => $match) { |
88
|
6 |
|
$name = $placeholders[1][$index]; |
89
|
6 |
|
$replace = array_key_exists($name, $restrictions) ? $restrictions[$name] : '[^\/]+'; |
90
|
6 |
|
$replace = '(?<'.$name.'>'.$replace.')'; |
91
|
6 |
|
$regexp = str_replace($match, $replace, $regexp); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
8 |
|
return $regexp; |
96
|
|
|
} |
97
|
|
|
} |