1
|
|
|
<?php namespace Comodojo\Dispatcher\Router; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Components\Model as DispatcherClassModel; |
4
|
|
|
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait; |
5
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
6
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* |
10
|
|
|
* @package Comodojo dispatcher |
11
|
|
|
* @author Marco Giovinazzi <[email protected]> |
12
|
|
|
* @license GPL-3.0+ |
13
|
|
|
* |
14
|
|
|
* LICENSE: |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
19
|
|
|
* License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Affero General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
class Collector extends DispatcherClassModel { |
31
|
|
|
|
32
|
|
|
use TimestampTrait; |
33
|
|
|
|
34
|
|
|
private $bypass = false; |
35
|
|
|
|
36
|
|
|
private $classname = ""; |
37
|
|
|
|
38
|
|
|
private $type = ""; |
39
|
|
|
|
40
|
|
|
private $service = ""; |
41
|
|
|
|
42
|
|
|
private $cache = null; |
43
|
|
|
|
44
|
|
|
private $request = null; |
45
|
|
|
|
46
|
|
|
private $response = null; |
47
|
|
|
|
48
|
|
|
private $table = array(); |
49
|
|
|
|
50
|
|
|
public function __construct($routing_table, $configuration = null, $logger = null, $cache = null) { |
51
|
|
|
|
52
|
|
|
parent::__construct($configuration, $logger); |
53
|
|
|
|
54
|
|
|
$this->table = $routing_table->get(); |
55
|
|
|
|
56
|
|
|
$this->cache = $cache; |
57
|
|
|
|
58
|
|
|
$this->setTimestamp(); |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getType() { |
63
|
|
|
|
64
|
|
|
return $this->type; |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getService() { |
69
|
|
|
|
70
|
|
|
return $this->service; |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getParameters() { |
75
|
|
|
|
76
|
|
|
return $this->parameters; |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getClassName() { |
81
|
|
|
|
82
|
|
|
return $this->classname; |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getInstance() { |
87
|
|
|
|
88
|
|
|
$class = $this->classname; |
89
|
|
|
|
90
|
|
|
if (class_exists($class)) { |
91
|
|
|
|
92
|
|
|
return new $class( |
93
|
|
|
$this->service, |
94
|
|
|
$this->logger, |
95
|
|
|
$this, |
96
|
|
|
$this->response |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
else return null; |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function bypass($mode = true) { |
105
|
|
|
|
106
|
|
|
$this->bypass = filter_var($mode, FILTER_VALIDATE_BOOLEAN); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function route(Request $request) { |
113
|
|
|
|
114
|
|
|
$this->request = $request; |
115
|
|
|
|
116
|
|
|
if (!$this->parse()) { |
117
|
|
|
|
118
|
|
|
throw new DispatcherException("Unable to find a valid route for the specified uri"); |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function compose(Response $response) { |
|
|
|
|
126
|
|
|
|
127
|
|
|
$this->response = $response; |
128
|
|
|
|
129
|
|
|
$service = $this->getInstance(); |
130
|
|
|
|
131
|
|
|
if (!is_null($service)) { |
132
|
|
|
|
133
|
|
|
$result = ""; |
|
|
|
|
134
|
|
|
|
135
|
|
|
switch( $_SERVER['REQUEST_METHOD'] ) { |
136
|
|
|
|
137
|
|
|
case 'POST': |
138
|
|
|
|
139
|
|
|
$result = $service->post(); |
140
|
|
|
|
141
|
|
|
break; |
142
|
|
|
|
143
|
|
|
case 'PUT': |
144
|
|
|
|
145
|
|
|
$result = $service->put(); |
146
|
|
|
|
147
|
|
|
break; |
148
|
|
|
|
149
|
|
|
case 'DELETE': |
150
|
|
|
|
151
|
|
|
$result = $service->delete(); |
152
|
|
|
|
153
|
|
|
break; |
154
|
|
|
|
155
|
|
|
default: |
156
|
|
|
|
157
|
|
|
$result = $service->get(); |
158
|
|
|
|
159
|
|
|
break; |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$this->response->content()->set($result); |
164
|
|
|
|
165
|
|
|
} else { |
166
|
|
|
|
167
|
|
|
throw new DispatcherException(sprintf("Unable to execute service '%s'", $this->service)); |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
private function parse() { |
175
|
|
|
|
176
|
|
|
$path = $this->request->uri()->getPath(); |
177
|
|
|
|
178
|
|
|
foreach ($this->table as $regex => $value) { |
179
|
|
|
|
180
|
|
|
if (preg_match("/" . $regex . "/", $path, $matches)) { |
181
|
|
|
|
182
|
|
|
$this->evalUri($value['query'], $matches); |
183
|
|
|
|
184
|
|
|
foreach ($value['parameters'] as $parameter => $value) { |
185
|
|
|
|
186
|
|
|
$this->request->query()->set($parameter, $value); |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$this->classname = $value['class']; |
191
|
|
|
$this->type = $value['type']; |
192
|
|
|
$this->service = implode('.', $value['service']); |
193
|
|
|
$this->service = empty($this->service)?"default":$this->service; |
194
|
|
|
|
195
|
|
|
return true; |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $false; |
|
|
|
|
202
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
private function evalUri($parameters, $bits) { |
206
|
|
|
|
207
|
|
|
$count = 0; |
|
|
|
|
208
|
|
|
|
209
|
|
|
foreach ($parameters as $key => $value) { |
210
|
|
|
|
211
|
|
|
if (isset($bits[$key])) { |
212
|
|
|
|
213
|
|
|
if (preg_match('/^' . $value['regex'] . '$/', $bits[$key], $matches)) { |
214
|
|
|
|
215
|
|
|
if (count($matches) == 1) $matches = $matches[0]; |
216
|
|
|
|
217
|
|
|
$this->request->query()->set($key, $matches); |
218
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
} elseif ($value['required']) { |
222
|
|
|
|
223
|
|
|
throw new DispatcherException(sprintf("Required parameter '%s' not specified.", $key)); |
224
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: