1
|
|
|
<?php namespace Comodojo\Dispatcher\Router; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\DataAccess as DataAccessTrait; |
4
|
|
|
use \Comodojo\Dispatcher\Components\DataSerialization as DataSerializationTrait; |
5
|
|
|
use \Comodojo\Exception\DispatcherException; |
6
|
|
|
use \Serializable; |
7
|
|
|
use \Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @package Comodojo Dispatcher |
11
|
|
|
* @author Marco Giovinazzi <[email protected]> |
12
|
|
|
* @author Marco Castiello <[email protected]> |
13
|
|
|
* @license GPL-3.0+ |
14
|
|
|
* |
15
|
|
|
* LICENSE: |
16
|
|
|
* |
17
|
|
|
* This program is free software: you can redistribute it and/or modify |
18
|
|
|
* it under the terms of the GNU Affero General Public License as |
19
|
|
|
* published by the Free Software Foundation, either version 3 of the |
20
|
|
|
* License, or (at your option) any later version. |
21
|
|
|
* |
22
|
|
|
* This program is distributed in the hope that it will be useful, |
23
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
24
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25
|
|
|
* GNU Affero General Public License for more details. |
26
|
|
|
* |
27
|
|
|
* You should have received a copy of the GNU Affero General Public License |
28
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
class Route implements Serializable { |
32
|
|
|
|
33
|
|
|
use DataAccessTrait; |
34
|
|
|
|
35
|
|
|
use DataSerializationTrait; |
36
|
|
|
|
37
|
2 |
|
public function __construct() { |
38
|
|
|
|
39
|
2 |
|
$this->classname = ""; |
|
|
|
|
40
|
|
|
|
41
|
2 |
|
$this->type = ""; |
|
|
|
|
42
|
|
|
|
43
|
2 |
|
$this->service = array(); |
|
|
|
|
44
|
|
|
|
45
|
2 |
|
$this->parameters = array(); |
|
|
|
|
46
|
|
|
|
47
|
2 |
|
$this->request = array(); |
|
|
|
|
48
|
|
|
|
49
|
2 |
|
$this->query = array(); |
|
|
|
|
50
|
|
|
|
51
|
2 |
|
} |
52
|
|
|
|
53
|
2 |
|
public function getType() { |
54
|
|
|
|
55
|
2 |
|
return $this->type; |
|
|
|
|
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
public function setType($type) { |
60
|
|
|
|
61
|
2 |
|
$this->type = $type; |
|
|
|
|
62
|
|
|
|
63
|
2 |
|
return $this; |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getService() { |
68
|
|
|
|
69
|
|
|
return $this->service; |
|
|
|
|
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function getServiceName() { |
74
|
|
|
|
75
|
1 |
|
return (empty($this->service))?"default":implode('.', $this->service); |
|
|
|
|
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function setService($service) { |
80
|
|
|
|
81
|
|
|
$this->service = $service; |
|
|
|
|
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
public function addService($service) { |
88
|
|
|
|
89
|
1 |
|
$this->service = array_merge($this->service, array($service)); |
|
|
|
|
90
|
|
|
|
91
|
1 |
|
return $this; |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getParameter($key) { |
96
|
|
|
|
97
|
|
|
$parameters = $this->parameters; |
|
|
|
|
98
|
|
|
|
99
|
|
|
return (isset($parameters[$key]))?$parameters[$key]:null; |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getParameters() { |
104
|
|
|
|
105
|
|
|
return $this->parameters; |
|
|
|
|
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function setParameter($key, $value) { |
110
|
|
|
|
111
|
|
|
$this->parameters = array_merge($this->parameters, array($key => $value)); |
|
|
|
|
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
public function setParameters($parameters) { |
118
|
|
|
|
119
|
1 |
|
$this->parameters = $parameters; |
|
|
|
|
120
|
|
|
|
121
|
1 |
|
return $this; |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
public function getRequestParameter($key) { |
126
|
|
|
|
127
|
1 |
|
$parameters = $this->request; |
|
|
|
|
128
|
|
|
|
129
|
1 |
|
return (isset($parameters[$key]))?$parameters[$key]:null; |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
public function getRequestParameters() { |
134
|
|
|
|
135
|
1 |
|
return $this->request; |
|
|
|
|
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function setRequestParameter($key, $value) { |
140
|
|
|
|
141
|
1 |
|
$this->request = array_merge($this->request, array($key => $value)); |
|
|
|
|
142
|
|
|
|
143
|
1 |
|
return $this; |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function setRequestParameters($parameters) { |
148
|
|
|
|
149
|
|
|
$this->request = $parameters; |
|
|
|
|
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
public function setQuery($key, $regex, $required = false) { |
156
|
|
|
|
157
|
1 |
|
$this->query = array_merge($this->query, array( |
|
|
|
|
158
|
|
|
$key => array( |
159
|
1 |
|
"regex" => $regex, |
160
|
|
|
"required" => $required |
161
|
1 |
|
) |
162
|
1 |
|
)); |
163
|
|
|
|
164
|
1 |
|
return $this; |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
View Code Duplication |
public function isQueryRequired($key) { |
|
|
|
|
169
|
|
|
|
170
|
1 |
|
$query = $this->query; |
|
|
|
|
171
|
|
|
|
172
|
1 |
|
return isset($query[$key])?$query[$key]["required"]:false; |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
View Code Duplication |
public function getQueryRegex($key) { |
|
|
|
|
177
|
|
|
|
178
|
1 |
|
$query = $this->query; |
|
|
|
|
179
|
|
|
|
180
|
1 |
|
return isset($query[$key])?$query[$key]["regex"]:null; |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function getQueries() { |
185
|
|
|
|
186
|
|
|
return $this->query; |
|
|
|
|
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function setQueries($query) { |
191
|
|
|
|
192
|
|
|
$this->query = $query; |
|
|
|
|
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
2 |
|
public function getClassName() { |
199
|
|
|
|
200
|
2 |
|
return $this->classname; |
|
|
|
|
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
2 |
|
public function setClassName($class) { |
205
|
|
|
|
206
|
2 |
|
$this->classname = $class; |
|
|
|
|
207
|
|
|
|
208
|
2 |
|
return $this; |
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->setRequestParameter($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
|
|
|
} |
243
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.