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
|
|
|
|
9
|
|
|
class Route extends EventEmitter |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Regexp ready route |
13
|
|
|
* @var String |
14
|
|
|
*/ |
15
|
|
|
public $parsedRoute; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Route method type |
19
|
|
|
* @var String |
20
|
|
|
*/ |
21
|
|
|
public $method; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Route action |
25
|
|
|
* @var Callable |
26
|
|
|
*/ |
27
|
|
|
public $action; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Route uri |
31
|
|
|
* @var String |
32
|
|
|
*/ |
33
|
|
|
private $uri; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Route filters |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $filters = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param String $method |
43
|
|
|
* @param String $uri |
44
|
|
|
* @param Callable $action |
45
|
|
|
*/ |
46
|
|
|
public function __construct ($method, $uri, $action) |
47
|
|
|
{ |
48
|
|
|
$this->method = $method; |
49
|
|
|
$this->uri = $uri; |
50
|
|
|
$this->action = $action; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create a new filter for current route |
55
|
|
|
* |
56
|
|
|
* @param String $param parameter to filter |
57
|
|
|
* @param String $filter regexp to execute |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function where($param, $filter) |
62
|
|
|
{ |
63
|
|
|
if (is_array($param)) { |
64
|
|
|
$this->filters = array_merge($this->filters, $param); |
65
|
|
|
|
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->filters[$param] = $filter; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Helper to listing to after event |
74
|
|
|
* |
75
|
|
|
* @param Callable $callback |
76
|
|
|
* @return Void |
77
|
|
|
*/ |
78
|
|
|
public function after($callback) |
79
|
|
|
{ |
80
|
|
|
$this->on('after', $callback); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Parse route uri |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function parse() |
89
|
|
|
{ |
90
|
|
|
preg_match_all("#\{(\w+)\}#", $this->uri, $params); |
91
|
|
|
$replace = []; |
92
|
|
|
|
93
|
|
|
foreach ($params[1] as $param) { |
94
|
|
|
$replace['{'.$param.'}'] = '(?<'.$param.'>'. (isset($this->filters[$param]) ? $this->filters[$param] : '[a-zA-Z+0-9-.]+') .')'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->parsedRoute = str_replace(array_keys($replace), array_values($replace), $this->uri); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Check if uri is parsed |
102
|
|
|
* |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
public function isParsed() |
106
|
|
|
{ |
107
|
|
|
return !empty($this->parsedRoute); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Run the current route |
112
|
|
|
* |
113
|
|
|
* @param \React\Http\Request $request |
114
|
|
|
* @param \React\Restify\Response $response |
115
|
|
|
* @param Callable $next |
116
|
|
|
* |
117
|
|
|
* @return Void |
118
|
|
|
*/ |
119
|
|
|
public function run(Request $request, Response $response, $next) |
120
|
|
|
{ |
121
|
|
|
if (is_string($this->action)) { |
122
|
|
|
$this->action = explode(':', $this->action); |
123
|
|
|
$this->action[0] = new $this->action[0](); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
try { |
127
|
|
|
call_user_func_array($this->action, array($request, $response, $next)); |
128
|
|
|
$this->emit('after', [$request, $response, $this]); |
129
|
|
|
} catch (\Exception $e) { |
130
|
|
|
$this->emit('error', [$request, $response, $e->getMessage()]); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|