|
1
|
|
|
<?php namespace Comodojo\Dispatcher\Router; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel; |
|
4
|
|
|
use \Comodojo\Dispatcher\Router\Model as Router; |
|
5
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
|
6
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
|
7
|
|
|
use \Comodojo\Dispatcher\Extra\Model as Extra; |
|
8
|
|
|
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait; |
|
9
|
|
|
use \Comodojo\Exception\DispatcherException; |
|
10
|
|
|
use \Exception; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @package Comodojo Dispatcher |
|
14
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
15
|
|
|
* @author Marco Castiello <[email protected]> |
|
16
|
|
|
* @license GPL-3.0+ |
|
17
|
|
|
* |
|
18
|
|
|
* LICENSE: |
|
19
|
|
|
* |
|
20
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
21
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
22
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
23
|
|
|
* License, or (at your option) any later version. |
|
24
|
|
|
* |
|
25
|
|
|
* This program is distributed in the hope that it will be useful, |
|
26
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
27
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
28
|
|
|
* GNU Affero General Public License for more details. |
|
29
|
|
|
* |
|
30
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
31
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
class Route extends DispatcherClassModel { |
|
35
|
|
|
|
|
36
|
|
|
use TimestampTrait; |
|
37
|
|
|
|
|
38
|
|
|
private $classname = ""; |
|
39
|
|
|
|
|
40
|
|
|
private $type = ""; |
|
41
|
|
|
|
|
42
|
|
|
private $service = array(); |
|
43
|
|
|
|
|
44
|
|
|
private $parameters = array(); |
|
45
|
|
|
|
|
46
|
|
|
private $query = array(); |
|
47
|
|
|
|
|
48
|
|
|
private $router; |
|
49
|
|
|
|
|
50
|
2 |
|
public function __construct( |
|
51
|
|
|
Router $router |
|
52
|
|
|
) { |
|
53
|
|
|
|
|
54
|
2 |
|
parent::__construct($router->configuration(), $router->logger()); |
|
55
|
|
|
|
|
56
|
2 |
|
$this->router = $router; |
|
57
|
|
|
|
|
58
|
2 |
|
$this->setTimestamp(); |
|
59
|
|
|
|
|
60
|
2 |
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
public function getType() { |
|
63
|
|
|
|
|
64
|
2 |
|
return $this->type; |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
public function setType($type) { |
|
69
|
|
|
|
|
70
|
2 |
|
$this->type = $type; |
|
71
|
|
|
|
|
72
|
2 |
|
return $this; |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getService() { |
|
77
|
|
|
|
|
78
|
|
|
return $this->service; |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function getServiceName() { |
|
83
|
|
|
|
|
84
|
1 |
|
return (empty($this->service))?"default":implode('.', $this->service); |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function setService($service) { |
|
89
|
|
|
|
|
90
|
|
|
$this->service = $service; |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
public function addService($service) { |
|
97
|
|
|
|
|
98
|
1 |
|
array_push($this->service, $service); |
|
99
|
|
|
|
|
100
|
1 |
|
return $this; |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
public function getParameter($key) { |
|
105
|
|
|
|
|
106
|
1 |
|
return (isset($this->parameters[$key]))?$this->parameters[$key]:null; |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
public function getParameters() { |
|
111
|
|
|
|
|
112
|
2 |
|
return $this->parameters; |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
public function setParameter($key, $value) { |
|
117
|
|
|
|
|
118
|
1 |
|
$this->parameters[$key] = $value; |
|
119
|
|
|
|
|
120
|
1 |
|
return $this; |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
public function setParameters($parameters) { |
|
125
|
|
|
|
|
126
|
1 |
|
$this->parameters = $parameters; |
|
127
|
|
|
|
|
128
|
1 |
|
return $this; |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
public function setQuery($key, $regex, $required = false) { |
|
133
|
|
|
|
|
134
|
1 |
|
$this->query[$key] = array( |
|
135
|
1 |
|
"regex" => $regex, |
|
136
|
|
|
"required" => $required |
|
137
|
1 |
|
); |
|
138
|
|
|
|
|
139
|
1 |
|
return $this; |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
public function isQueryRequired($key) { |
|
144
|
|
|
|
|
145
|
1 |
|
return isset($this->query[$key])?$this->query[$key]["required"]:false; |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
public function getQueryRegex($key) { |
|
150
|
|
|
|
|
151
|
1 |
|
return isset($this->query[$key])?$this->query[$key]["regex"]:null; |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function getQueries() { |
|
156
|
|
|
|
|
157
|
|
|
return $this->query; |
|
158
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function setQueries($query) { |
|
162
|
|
|
|
|
163
|
|
|
$this->query = $query; |
|
164
|
|
|
|
|
165
|
|
|
return $this; |
|
166
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
2 |
|
public function getClassName() { |
|
170
|
|
|
|
|
171
|
2 |
|
return $this->classname; |
|
172
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
2 |
|
public function setClassName($class) { |
|
176
|
|
|
|
|
177
|
2 |
|
$this->classname = $class; |
|
178
|
|
|
|
|
179
|
2 |
|
return $this; |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
2 |
|
public function getInstance( |
|
184
|
|
|
Request $request, |
|
185
|
|
|
Response $response, |
|
186
|
|
|
Extra $extra |
|
187
|
|
|
) { |
|
188
|
|
|
|
|
189
|
2 |
|
$class = $this->classname; |
|
190
|
|
|
|
|
191
|
2 |
|
if (class_exists($class)) { |
|
192
|
|
|
|
|
193
|
|
|
// All the route parameters are also added to the query parameters |
|
194
|
2 |
|
foreach ($this->getParameters() as $parameter => $value) { |
|
195
|
1 |
|
$request->query()->set($parameter, $value); |
|
196
|
2 |
|
} |
|
197
|
|
|
|
|
198
|
2 |
|
return new $class( |
|
199
|
2 |
|
$this->configuration, |
|
200
|
2 |
|
$this->logger, |
|
201
|
2 |
|
$request, |
|
202
|
2 |
|
$this->router, |
|
203
|
2 |
|
$response, |
|
204
|
|
|
$extra |
|
205
|
2 |
|
); |
|
206
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
else return null; |
|
209
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
1 |
|
public function path($path) { |
|
213
|
|
|
|
|
214
|
|
|
// Because of the nature of the global regular expression, all the bits of the matched route are associated with a parameter key |
|
215
|
1 |
|
foreach ($this->query as $key => $value) { |
|
216
|
|
|
|
|
217
|
1 |
|
if (isset($path[$key])) { |
|
218
|
|
|
/* if it's available a bit associated with the parameter name, it is compared against |
|
219
|
|
|
* it's regular expression in order to extrect backreferences |
|
220
|
|
|
*/ |
|
221
|
1 |
|
if (preg_match('/^' . $value['regex'] . '$/', $path[$key], $matches)) { |
|
222
|
|
|
|
|
223
|
1 |
|
if (count($matches) == 1) $matches = $matches[0]; // This is the case where no backreferences are present or available. |
|
224
|
|
|
|
|
225
|
|
|
// The extracted value (with any backreference available) is added to the query parameters. |
|
226
|
1 |
|
$this->setParameter($key, $matches); |
|
227
|
|
|
|
|
228
|
1 |
|
} |
|
229
|
|
|
|
|
230
|
1 |
|
} elseif ($value['required']) { |
|
231
|
|
|
|
|
232
|
|
|
throw new DispatcherException(sprintf("Required parameter '%s' not specified.", $key), 1, null, 500); |
|
233
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
1 |
|
} |
|
237
|
|
|
|
|
238
|
1 |
|
return $this; |
|
239
|
|
|
|
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function getData() { |
|
243
|
|
|
|
|
244
|
|
|
return array( |
|
245
|
|
|
"type" => $this->getType(), |
|
246
|
|
|
"class" => $this->getClassName(), |
|
247
|
|
|
"service" => $this->getService(), |
|
248
|
|
|
"parameters" => $this->getParameters(), |
|
249
|
|
|
"query" => $this->getQueries() |
|
250
|
|
|
); |
|
251
|
|
|
|
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
public function setData($data) { |
|
255
|
|
|
|
|
256
|
|
|
$this->setType($data['type']) |
|
257
|
|
|
->setClassName($data['class']) |
|
258
|
|
|
->setService($data['service']) |
|
259
|
|
|
->setParameters($data['parameters']) |
|
260
|
|
|
->setQueries($data['query']); |
|
261
|
|
|
|
|
262
|
|
|
return $this; |
|
263
|
|
|
|
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
} |
|
267
|
|
|
|