1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CapMousse\ReactRestify\Routing; |
4
|
|
|
|
5
|
|
|
use CapMousse\ReactRestify\Evenement\EventEmitter; |
6
|
|
|
use CapMousse\ReactRestify\Http\Request; |
7
|
|
|
use CapMousse\ReactRestify\Http\Response; |
8
|
|
|
use CapMousse\ReactRestify\Traits\WaterfallTrait; |
9
|
|
|
use CapMousse\ReactRestify\Container\Container; |
10
|
|
|
|
11
|
|
|
class Router extends EventEmitter |
12
|
|
|
{ |
13
|
|
|
use WaterfallTrait; |
14
|
|
|
/** |
15
|
|
|
* The current routes list |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
public $routes = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $middlewares = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The current asked uri |
27
|
|
|
* @var string|boolean |
28
|
|
|
*/ |
29
|
|
|
private $uri = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new routing element |
33
|
|
|
* |
34
|
|
|
* @param array $routes a route array |
35
|
|
|
* |
36
|
|
|
* @throws \InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
public function __construct($routes = []) |
39
|
|
|
{ |
40
|
|
|
if (!is_array($routes)) { |
41
|
|
|
throw new \InvalidArgumentException("Routes must be an array"); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->addRoutes($routes); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add routes |
49
|
|
|
* |
50
|
|
|
* @param array $routes a route array |
51
|
|
|
* |
52
|
|
|
* @throws \InvalidArgumentException |
53
|
|
|
* @return Void |
54
|
|
|
*/ |
55
|
|
|
public function addRoutes($routes) |
56
|
|
|
{ |
57
|
|
|
if (!is_array($routes)) { |
58
|
|
|
throw new \InvalidArgumentException("Routes must be an array"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$routes = array_filter($routes, function ($route) { |
62
|
|
|
return is_a('Route', $route); |
63
|
|
|
}); |
64
|
|
|
|
65
|
|
|
$this->routes = array_merge($this->routes, $routes); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Add a new route |
70
|
|
|
* |
71
|
|
|
* @param String $method type of route |
72
|
|
|
* @param String $route uri to catch |
73
|
|
|
* @param Callable $callback |
74
|
|
|
*/ |
75
|
|
|
public function addRoute($method, $route, $callback) |
76
|
|
|
{ |
77
|
|
|
return $this->routes[] = new Route(strtoupper($method), $route, $callback); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create a new group of routes |
82
|
|
|
* |
83
|
|
|
* @param String $prefix prefix of thes routes |
84
|
|
|
* |
85
|
|
|
* @return \CapMousse\ReactRestify\Routing\Group |
86
|
|
|
*/ |
87
|
|
|
public function addGroup($prefix, $callback) |
88
|
|
|
{ |
89
|
|
|
$group = new Routes($this, $prefix, $callback); |
90
|
|
|
|
91
|
|
|
return $group; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Add a middleware |
96
|
|
|
* @param string|Callable $callback |
97
|
|
|
*/ |
98
|
|
|
public function addMiddleware($callback) |
99
|
|
|
{ |
100
|
|
|
$this->middlewares[] = function (Callable $next, Request $request, Response $response) use ($callback) { |
101
|
|
|
$container = Container::getInstance(); |
102
|
|
|
$parameters = array_merge([ |
103
|
|
|
"request" => $request, |
104
|
|
|
"response" => $response, |
105
|
|
|
"next" => $next |
106
|
|
|
], $request->getData()); |
107
|
|
|
|
108
|
|
|
$container->call($callback, $parameters); |
109
|
|
|
}; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Launch the route parsing |
114
|
|
|
* |
115
|
|
|
* @param \React\Http\Request $request |
116
|
|
|
* @param \React\Restify\Response $response |
117
|
|
|
* |
118
|
|
|
* @throws \RuntimeException |
119
|
|
|
* @return Void |
120
|
|
|
*/ |
121
|
|
|
public function launch(Request $request, Response $response) |
122
|
|
|
{ |
123
|
|
|
if (count($this->routes) === 0) { |
124
|
|
|
throw new \RuntimeException("No routes defined"); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->uri = $request->httpRequest->getPath(); |
128
|
|
|
|
129
|
|
|
if ($this->uri = null) { |
130
|
|
|
$this->uri = "/"; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$request->on('end', function () use (&$request, &$response) { |
134
|
|
|
$this->matchRoutes($request, $response); |
135
|
|
|
}); |
136
|
|
|
|
137
|
|
|
$request->on('error', function ($error) use (&$request, &$response) { |
138
|
|
|
$this->emit('error', [$request, $response, $error]); |
139
|
|
|
}); |
140
|
|
|
|
141
|
|
|
$request->parseData(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Try to match the current uri with all routes |
146
|
|
|
* |
147
|
|
|
* |
148
|
|
|
* @param \React\Http\Request $request |
149
|
|
|
* @param \React\Restify\Response $response |
150
|
|
|
* |
151
|
|
|
* @throws \RuntimeException |
152
|
|
|
* @return Void |
153
|
|
|
*/ |
154
|
|
|
private function matchRoutes(Request $request, Response $response) |
155
|
|
|
{ |
156
|
|
|
$stack = []; |
157
|
|
|
$path = $request->httpRequest->getPath(); |
158
|
|
|
$method = $request->httpRequest->getMethod(); |
159
|
|
|
|
160
|
|
|
foreach ($this->routes as $route) { |
161
|
|
|
if (!$route->match($path, $method)) { |
162
|
|
|
continue; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$methodArgs = $route->getArgs($path); |
166
|
|
|
$request->setData($methodArgs); |
167
|
|
|
|
168
|
|
|
$route->on('error', function (...$args) { |
169
|
|
|
$this->emit('error', $args); |
170
|
|
|
}); |
171
|
|
|
|
172
|
|
|
$stack[] = function (...$params) use ($route) { |
173
|
|
|
$route->run(...$params); |
174
|
|
|
}; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (count($stack) == 0) { |
178
|
|
|
$this->emit('NotFound', array($request, $response)); |
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$this->runStack($stack, $request, $response); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Launch route stack |
187
|
|
|
* @param array $stack |
188
|
|
|
* @param Request $request |
189
|
|
|
* @param Response $response |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
protected function runStack(array $stack, Request $request, Response $response) |
193
|
|
|
{ |
194
|
|
|
$stack[] = function () use ($response) { |
195
|
|
|
$response->end(); |
196
|
|
|
}; |
197
|
|
|
|
198
|
|
|
$finalStack = array_merge($this->middlewares, $stack); |
199
|
|
|
$this->waterfall($finalStack, [$request, $response]); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|