1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nymfonya\Component\Http; |
6
|
|
|
|
7
|
|
|
use Nymfonya\Component\Http\Interfaces\RoutesInterface; |
8
|
|
|
use Nymfonya\Component\Http\Interfaces\RouteInterface; |
9
|
|
|
use Nymfonya\Component\Http\Interfaces\RequestInterface; |
10
|
|
|
use Nymfonya\Component\Http\Interfaces\RouterInterface; |
11
|
|
|
|
12
|
|
|
class Router implements RouterInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* active route |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $activeRoute; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* routes collection |
23
|
|
|
* |
24
|
|
|
* @var RouteInterface[] |
25
|
|
|
*/ |
26
|
|
|
private $routes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* request |
30
|
|
|
* |
31
|
|
|
* @var RequestInterface |
32
|
|
|
*/ |
33
|
|
|
private $request = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* route params |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $params; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* route match expr |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $matchingRoute; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* instanciate |
51
|
|
|
* |
52
|
|
|
* @param RoutesInterface $routes |
53
|
|
|
* @param RequestInterface $request |
54
|
|
|
*/ |
55
|
9 |
|
public function __construct(RoutesInterface $routes, RequestInterface $request) |
56
|
|
|
{ |
57
|
9 |
|
$this->routes = $routes->get(); |
58
|
9 |
|
$this->request = $request; |
59
|
9 |
|
$this->activeRoute = ''; |
60
|
9 |
|
$this->params = []; |
61
|
9 |
|
$this->matchingRoute = ''; |
62
|
9 |
|
$this->activeRoute = (string) substr($this->request->getUri(), 1); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* set routes |
67
|
|
|
* |
68
|
|
|
* @param RoutesInterface $routes |
69
|
|
|
* @return RouterInterface |
70
|
|
|
*/ |
71
|
2 |
|
public function setRoutes(RoutesInterface $routes): RouterInterface |
72
|
|
|
{ |
73
|
2 |
|
$this->routes = $routes->get(); |
74
|
2 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* compile |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
5 |
|
public function compile(): array |
83
|
|
|
{ |
84
|
5 |
|
$routes = $this->routes; |
85
|
5 |
|
$routesLength = count($routes); |
86
|
5 |
|
for ($i = 0; $i < $routesLength; $i++) { |
87
|
5 |
|
$route = $routes[$i]; |
88
|
5 |
|
$matches = []; |
89
|
5 |
|
$pattern = $route->getExpr(); |
90
|
5 |
|
$match = preg_match($pattern, $this->activeRoute, $matches); |
91
|
5 |
|
if ($match) { |
92
|
5 |
|
$this->matchingRoute = $pattern; |
93
|
5 |
|
array_shift($matches); |
94
|
5 |
|
$this->setParams($route, $matches); |
95
|
5 |
|
return $matches; |
96
|
|
|
} |
97
|
|
|
} |
98
|
1 |
|
return []; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* return slugs params |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
5 |
|
public function getParams(): array |
107
|
|
|
{ |
108
|
5 |
|
return $this->params; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* set params from slugs |
113
|
|
|
* |
114
|
|
|
* @param RouteInterface $route |
115
|
|
|
* @param array $matches |
116
|
|
|
* @return RouterInterface |
117
|
|
|
*/ |
118
|
6 |
|
public function setParams(RouteInterface $route, array $matches): RouterInterface |
119
|
|
|
{ |
120
|
6 |
|
$slugs = $route->getSlugs(); |
121
|
6 |
|
$slugCount = count($slugs); |
122
|
6 |
|
if ($slugCount > 0) { |
123
|
2 |
|
for ($c = 0; $c < $slugCount; $c++) { |
124
|
2 |
|
$slug = $slugs[$c]; |
125
|
2 |
|
if (false === empty($slug)) { |
126
|
2 |
|
$this->params[$slug] = $matches[$c]; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} else { |
130
|
4 |
|
if (isset($matches[2])) { |
131
|
3 |
|
$parms = explode('/', $matches[2]); |
132
|
3 |
|
$this->params = []; |
133
|
3 |
|
while (false !== $key = next($parms)) { |
134
|
2 |
|
$this->params[$key] = next($parms); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
6 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* return matching regexp pattern |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
1 |
|
public function getMatchingRoute(): string |
147
|
|
|
{ |
148
|
1 |
|
return $this->matchingRoute; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|