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
|
|
|
*/ |
32
|
|
|
public function __construct($routes = []) |
33
|
|
|
{ |
34
|
|
|
if (!is_array($routes)) { |
35
|
|
|
throw new \InvalidArgumentException("Routes must be an array"); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->addRoutes($routes); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add routes |
43
|
|
|
* |
44
|
|
|
* @param array $routes a route array |
45
|
|
|
* |
46
|
|
|
* @throws \InvalidArgumentException |
47
|
|
|
* @return Void |
48
|
|
|
*/ |
49
|
|
|
public function addRoutes($routes) |
50
|
|
|
{ |
51
|
|
|
if (!is_array($routes)) { |
52
|
|
|
throw new \InvalidArgumentException("Routes must be an array"); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$routes = array_filter($routes, function ($route) { |
56
|
|
|
return is_a('Route', $route); |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
$this->routes = array_merge($this->routes, $routes); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add a new route |
64
|
|
|
* |
65
|
|
|
* @param String $method type of route |
66
|
|
|
* @param String $route uri to catch |
67
|
|
|
* @param Callable $callback |
68
|
|
|
*/ |
69
|
|
|
public function addRoute($method, $route, $callback) |
70
|
|
|
{ |
71
|
|
|
return $this->routes[] = new Route(strtoupper($method), $route, $callback); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create a new group of routes |
76
|
|
|
* |
77
|
|
|
* @param String $prefix prefix of thes routes |
78
|
|
|
* |
79
|
|
|
* @return \CapMousse\ReactRestify\Routing\Group |
80
|
|
|
*/ |
81
|
|
|
public function addGroup($prefix, $callback) |
82
|
|
|
{ |
83
|
|
|
$group = new Routes($this, $prefix, $callback); |
84
|
|
|
|
85
|
|
|
return $group; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Launch the route parsing |
90
|
|
|
* |
91
|
|
|
* @param \React\Http\Request $request |
92
|
|
|
* @param \React\Restify\Response $response |
93
|
|
|
* |
94
|
|
|
* @throws \RuntimeException |
95
|
|
|
* @return Void |
96
|
|
|
*/ |
97
|
|
|
public function launch(Request $request, Response $response) |
98
|
|
|
{ |
99
|
|
|
if (count($this->routes) === 0) { |
100
|
|
|
throw new \RuntimeException("No routes defined"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->uri = $request->httpRequest->getPath(); |
104
|
|
|
|
105
|
|
|
if ($this->uri = null) { |
106
|
|
|
$this->uri = "/"; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$request->on('end', function () use (&$request, &$response) { |
110
|
|
|
$this->matchRoutes($request, $response); |
111
|
|
|
}); |
112
|
|
|
|
113
|
|
|
$request->on('error', function ($error) use (&$request, &$response) { |
114
|
|
|
$this->emit('error', [$request, $response, $error]); |
115
|
|
|
}); |
116
|
|
|
|
117
|
|
|
$request->parseData(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Try to match the current uri with all routes |
122
|
|
|
* |
123
|
|
|
* |
124
|
|
|
* @param \React\Http\Request $request |
125
|
|
|
* @param \React\Restify\Response $response |
126
|
|
|
* |
127
|
|
|
* @throws \RuntimeException |
128
|
|
|
* @return Void |
129
|
|
|
*/ |
130
|
|
|
private function matchRoutes(Request $request, Response $response) |
131
|
|
|
{ |
132
|
|
|
$stack = []; |
133
|
|
|
$badMethod = false; |
134
|
|
|
|
135
|
|
|
foreach ($this->routes as $route) { |
136
|
|
|
if (!$route->match($request->httpRequest->getPath())) { |
137
|
|
|
continue; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($route->method != strtoupper($request->httpRequest->getMethod())) { |
141
|
|
|
$badMethod = true; |
142
|
|
|
continue; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$methodArgs = $route->getArgs($request->httpRequest->getPath()); |
146
|
|
|
$request->setData($methodArgs); |
147
|
|
|
|
148
|
|
|
$route->on('error', function () { |
149
|
|
|
$this->emit('error', func_get_args()); |
150
|
|
|
}); |
151
|
|
|
|
152
|
|
|
$stack[] = function ($next) use ($route, $request, $response) { |
153
|
|
|
$route->run($request, $response, $next); |
154
|
|
|
}; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (count($stack)) { |
158
|
|
|
$stack[] = function () use ($response) { |
159
|
|
|
$response->end(); |
160
|
|
|
}; |
161
|
|
|
|
162
|
|
|
$this->waterfall($stack); |
163
|
|
|
return; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if ($badMethod) { |
167
|
|
|
$this->emit('MethodNotAllowed', array(&$request, &$response)); |
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->emit('NotFound', array($request, $response)); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|