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
|
|
|
|
10
|
|
|
class Router extends EventEmitter |
11
|
|
|
{ |
12
|
|
|
use WaterfallTrait; |
13
|
|
|
/** |
14
|
|
|
* The current routes list |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
public $routes = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The current asked uri |
21
|
|
|
* @var string|boolean |
22
|
|
|
*/ |
23
|
|
|
private $uri = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new routing element |
27
|
|
|
* |
28
|
|
|
* @param array $routes a route array |
29
|
|
|
* |
30
|
|
|
* @throws \InvalidArgumentException |
31
|
|
|
* @return Router |
|
|
|
|
32
|
|
|
*/ |
33
|
|
|
public function __construct($routes = []) |
34
|
|
|
{ |
35
|
|
|
if (!is_array($routes)) { |
36
|
|
|
throw new \InvalidArgumentException("Routes must be an array"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->addRoutes($routes); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Add routes |
44
|
|
|
* |
45
|
|
|
* @param array $routes a route array |
46
|
|
|
* |
47
|
|
|
* @throws \InvalidArgumentException |
48
|
|
|
* @return Void |
49
|
|
|
*/ |
50
|
|
|
public function addRoutes($routes) |
51
|
|
|
{ |
52
|
|
|
if (!is_array($routes)) { |
53
|
|
|
throw new \InvalidArgumentException("Routes must be an array"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$routes = array_filter($routes, function ($route) { |
57
|
|
|
return is_a('Route', $route); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
$this->routes = array_merge($this->routes, $routes); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Add a new route |
65
|
|
|
* |
66
|
|
|
* @param String $method type of route |
67
|
|
|
* @param String $route uri to catch |
68
|
|
|
* @param Callable $callback |
69
|
|
|
*/ |
70
|
|
|
public function addRoute($method, $route, $callback) |
71
|
|
|
{ |
72
|
|
|
return $this->routes[] = new Route(strtoupper($method), $route, $callback); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create a new group of routes |
77
|
|
|
* |
78
|
|
|
* @param String $prefix prefix of thes routes |
79
|
|
|
* |
80
|
|
|
* @return \CapMousse\ReactRestify\Routing\Group |
81
|
|
|
*/ |
82
|
|
|
public function addGroup($prefix, $callback) |
83
|
|
|
{ |
84
|
|
|
$group = new Routes($this, $prefix, $callback); |
85
|
|
|
|
86
|
|
|
return $group; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Launch the route parsing |
91
|
|
|
* |
92
|
|
|
* @param \React\Http\Request $request |
93
|
|
|
* @param \React\Restify\Response $response |
94
|
|
|
* |
95
|
|
|
* @throws \RuntimeException |
96
|
|
|
* @return Void |
97
|
|
|
*/ |
98
|
|
|
public function launch(Request $request, Response $response) |
99
|
|
|
{ |
100
|
|
|
if (count($this->routes) === 0) { |
101
|
|
|
throw new \RuntimeException("No routes defined"); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->uri = $request->httpRequest->getPath(); |
105
|
|
|
|
106
|
|
|
if ($this->uri = null) { |
107
|
|
|
$this->uri = "/"; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$request->on('end', function () use (&$request, &$response) { |
111
|
|
|
$this->matchRoutes($request, $response); |
112
|
|
|
}); |
113
|
|
|
|
114
|
|
|
$request->on('error', function ($error) use (&$request, &$response) { |
115
|
|
|
$this->emit('error', [$request, $response, $error]); |
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
$request->parseData(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Try to match the current uri with all routes |
123
|
|
|
* |
124
|
|
|
* |
125
|
|
|
* @param \React\Http\Request $request |
126
|
|
|
* @param \React\Restify\Response $response |
127
|
|
|
* |
128
|
|
|
* @throws \RuntimeException |
129
|
|
|
* @return Void |
130
|
|
|
*/ |
131
|
|
|
private function matchRoutes(Request $request, Response $response) |
132
|
|
|
{ |
133
|
|
|
$stack = []; |
134
|
|
|
$badMethod = false; |
135
|
|
|
|
136
|
|
|
foreach ($this->routes as $route) { |
137
|
|
|
if (!$route->isParsed()) { |
138
|
|
|
$route->parse(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (preg_match('#'.$route->parsed.'$#', $request->httpRequest->getPath(), $array)) { |
142
|
|
|
if ($route->method != strtoupper($request->httpRequest->getMethod())) { |
143
|
|
|
$badMethod = true; |
144
|
|
|
continue; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$methodArgs = []; |
148
|
|
|
|
149
|
|
|
foreach ($array as $name => $value) { |
150
|
|
|
if (!is_int($name)) { |
151
|
|
|
$methodArgs[$name] = $value; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (count($methodArgs) > 0) { |
156
|
|
|
$request->setData($methodArgs); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$route->on('error', function () { |
160
|
|
|
$this->emit('error', func_get_args()); |
161
|
|
|
}); |
162
|
|
|
|
163
|
|
|
$stack[] = function ($next) use ($route, $request, $response) { |
164
|
|
|
$route->run($request, $response, $next); |
165
|
|
|
}; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (count($stack)) { |
170
|
|
|
$stack[] = function () use ($response) { |
171
|
|
|
$response->end(); |
172
|
|
|
}; |
173
|
|
|
|
174
|
|
|
$this->waterfall($stack); |
175
|
|
|
return; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if ($badMethod) { |
179
|
|
|
$this->emit('MethodNotAllowed', array(&$request, &$response, $next)); |
|
|
|
|
180
|
|
|
return; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$this->emit('NotFound', array($request, $response, $next)); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.