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\Container\Container; |
9
|
|
|
use CapMousse\ReactRestify\Traits\EventTrait; |
10
|
|
|
|
11
|
|
|
class Route extends EventEmitter |
12
|
|
|
{ |
13
|
|
|
use EventTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Regexp ready route |
17
|
|
|
* @var String |
18
|
|
|
*/ |
19
|
|
|
public $parsedRoute; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Route method type |
23
|
|
|
* @var String |
24
|
|
|
*/ |
25
|
|
|
public $method; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Route action |
29
|
|
|
* @var Callable |
30
|
|
|
*/ |
31
|
|
|
public $action; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Route uri |
35
|
|
|
* @var String |
36
|
|
|
*/ |
37
|
|
|
private $uri; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Route filters |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $filters = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param String $method |
47
|
|
|
* @param String $uri |
48
|
|
|
* @param Callable $action |
49
|
|
|
*/ |
50
|
|
|
public function __construct ($method, $uri, $action) |
51
|
|
|
{ |
52
|
|
|
$this->method = $method; |
53
|
|
|
$this->uri = $uri; |
54
|
|
|
$this->action = $action; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create a new filter for current route |
59
|
|
|
* |
60
|
|
|
* @param String $param parameter to filter |
61
|
|
|
* @param String $filter regexp to execute |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
public function where($param, $filter) |
66
|
|
|
{ |
67
|
|
|
if (is_array($param)) { |
68
|
|
|
$this->filters = array_merge($this->filters, $param); |
69
|
|
|
|
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->filters[$param] = $filter; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Parse route uri |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function parse() |
82
|
|
|
{ |
83
|
|
|
preg_match_all("#\{(\w+)\}#", $this->uri, $params); |
84
|
|
|
$replace = []; |
85
|
|
|
|
86
|
|
|
foreach ($params[1] as $param) { |
87
|
|
|
$replace['{'.$param.'}'] = '(?<'.$param.'>'. (isset($this->filters[$param]) ? $this->filters[$param] : '[a-zA-Z+0-9-.]+') .')'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->parsedRoute = str_replace(array_keys($replace), array_values($replace), $this->uri); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check if uri is parsed |
95
|
|
|
* |
96
|
|
|
* @return boolean |
97
|
|
|
*/ |
98
|
|
|
public function isParsed() |
99
|
|
|
{ |
100
|
|
|
return !empty($this->parsedRoute); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Check if path match route uri |
105
|
|
|
* @param String $path |
106
|
|
|
* @param String $method |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function match($path, $method) |
110
|
|
|
{ |
111
|
|
|
if (!$this->isParsed()) $this->parse(); |
112
|
|
|
|
113
|
|
|
if (!preg_match('#'.$this->parsedRoute.'$#', $path)) return false; |
114
|
|
|
if (strtoupper($method) !== $this->method) return false; |
115
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Parse route arguments |
121
|
|
|
* @param String $path |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function getArgs($path) |
125
|
|
|
{ |
126
|
|
|
if (!$this->isParsed()) $this->parse(); |
127
|
|
|
|
128
|
|
|
$data = []; |
129
|
|
|
$args = []; |
130
|
|
|
preg_match('#'.$this->parsedRoute.'$#', $path, $data); |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
foreach ($data as $name => $value) { |
134
|
|
|
if (is_int($name)) continue; |
135
|
|
|
$args[$name] = $value; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $args; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Run the current route |
143
|
|
|
* |
144
|
|
|
* @param Callable $next |
145
|
|
|
* @param \React\Http\Request $request |
146
|
|
|
* @param \React\Restify\Response $response |
147
|
|
|
* |
148
|
|
|
* @return Void |
149
|
|
|
*/ |
150
|
|
|
public function run(Callable $next, Request $request, Response $response) |
151
|
|
|
{ |
152
|
|
|
$container = Container::getInstance(); |
153
|
|
|
$parameters = array_merge([ |
154
|
|
|
"request" => $request, |
155
|
|
|
"response" => $response, |
156
|
|
|
"next" => $next |
157
|
|
|
], $request->getData()); |
158
|
|
|
|
159
|
|
|
try { |
160
|
|
|
$container->call($this->action, $parameters); |
161
|
|
|
$this->emit('after', [$request, $response, $this]); |
162
|
|
|
} catch (\Exception $e) { |
163
|
|
|
$this->emit('error', [$request, $response, $e->getMessage()]); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|