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->query = array(); |
|
|
|
|
48
|
|
|
|
49
|
2 |
|
} |
50
|
|
|
|
51
|
2 |
|
public function getType() { |
52
|
|
|
|
53
|
2 |
|
return $this->type; |
|
|
|
|
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
public function setType($type) { |
58
|
|
|
|
59
|
2 |
|
$this->type = $type; |
|
|
|
|
60
|
|
|
|
61
|
2 |
|
return $this; |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getService() { |
66
|
|
|
|
67
|
|
|
return $this->service; |
|
|
|
|
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function getServiceName() { |
72
|
|
|
|
73
|
1 |
|
return (empty($this->service))?"default":implode('.', $this->service); |
|
|
|
|
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setService($service) { |
78
|
|
|
|
79
|
|
|
$this->service = $service; |
|
|
|
|
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function addService($service) { |
86
|
|
|
|
87
|
1 |
|
$this->service = array_merge($this->service, array($service)); |
|
|
|
|
88
|
|
|
|
89
|
1 |
|
return $this; |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function getParameter($key) { |
94
|
|
|
|
95
|
1 |
|
$parameters = $this->parameters; |
|
|
|
|
96
|
|
|
|
97
|
1 |
|
return (isset($parameters[$key]))?$parameters[$key]:null; |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
public function getParameters() { |
102
|
|
|
|
103
|
1 |
|
return $this->parameters; |
|
|
|
|
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function setParameter($key, $value) { |
108
|
|
|
|
109
|
1 |
|
$this->parameters = array_merge($this->parameters, array($key => $value)); |
|
|
|
|
110
|
|
|
|
111
|
1 |
|
return $this; |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
public function setParameters($parameters) { |
116
|
|
|
|
117
|
1 |
|
$this->parameters = $parameters; |
|
|
|
|
118
|
|
|
|
119
|
1 |
|
return $this; |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function setQuery($key, $regex, $required = false) { |
124
|
|
|
|
125
|
1 |
|
$this->query = array_merge($this->query, array( |
|
|
|
|
126
|
|
|
$key => array( |
127
|
1 |
|
"regex" => $regex, |
128
|
|
|
"required" => $required |
129
|
1 |
|
) |
130
|
1 |
|
)); |
131
|
|
|
|
132
|
1 |
|
return $this; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
View Code Duplication |
public function isQueryRequired($key) { |
|
|
|
|
137
|
|
|
|
138
|
1 |
|
$query = $this->query; |
|
|
|
|
139
|
|
|
|
140
|
1 |
|
return isset($query[$key])?$query[$key]["required"]:false; |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
View Code Duplication |
public function getQueryRegex($key) { |
|
|
|
|
145
|
|
|
|
146
|
1 |
|
$query = $this->query; |
|
|
|
|
147
|
|
|
|
148
|
1 |
|
return isset($query[$key])?$query[$key]["regex"]:null; |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getQueries() { |
153
|
|
|
|
154
|
|
|
return $this->query; |
|
|
|
|
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function setQueries($query) { |
159
|
|
|
|
160
|
|
|
$this->query = $query; |
|
|
|
|
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
public function getClassName() { |
167
|
|
|
|
168
|
2 |
|
return $this->classname; |
|
|
|
|
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
2 |
|
public function setClassName($class) { |
173
|
|
|
|
174
|
2 |
|
$this->classname = $class; |
|
|
|
|
175
|
|
|
|
176
|
2 |
|
return $this; |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
public function path($path) { |
181
|
|
|
|
182
|
|
|
// Because of the nature of the global regular expression, all the bits of the matched route are associated with a parameter key |
183
|
1 |
|
foreach ($this->query as $key => $value) { |
|
|
|
|
184
|
|
|
|
185
|
1 |
|
if (isset($path[$key])) { |
186
|
|
|
/* if it's available a bit associated with the parameter name, it is compared against |
187
|
|
|
* it's regular expression in order to extrect backreferences |
188
|
|
|
*/ |
189
|
1 |
|
if (preg_match('/^' . $value['regex'] . '$/', $path[$key], $matches)) { |
190
|
|
|
|
191
|
1 |
|
if (count($matches) == 1) $matches = $matches[0]; // This is the case where no backreferences are present or available. |
192
|
|
|
|
193
|
|
|
// The extracted value (with any backreference available) is added to the query parameters. |
194
|
1 |
|
$this->setParameter($key, $matches); |
195
|
|
|
|
196
|
1 |
|
} |
197
|
|
|
|
198
|
1 |
|
} elseif ($value['required']) { |
199
|
|
|
|
200
|
|
|
throw new DispatcherException(sprintf("Required parameter '%s' not specified.", $key), 1, null, 500); |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
} |
205
|
|
|
|
206
|
1 |
|
return $this; |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
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.