|
1
|
|
|
<?php namespace Comodojo\Dispatcher\Router; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel; |
|
4
|
|
|
use \Comodojo\Dispatcher\Router\Table; |
|
5
|
|
|
use \Comodojo\Dispatcher\Router\Route; |
|
6
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
|
7
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
|
8
|
|
|
use \Comodojo\Dispatcher\Extra\Model as Extra; |
|
9
|
|
|
use \Comodojo\Dispatcher\Components\Configuration; |
|
10
|
|
|
use \Comodojo\Cache\Cache; |
|
11
|
|
|
use \Psr\Log\LoggerInterface; |
|
12
|
|
|
use \Comodojo\Exception\DispatcherException; |
|
13
|
|
|
use \Exception; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @package Comodojo Dispatcher |
|
17
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
18
|
|
|
* @author Marco Castiello <[email protected]> |
|
19
|
|
|
* @license GPL-3.0+ |
|
20
|
|
|
* |
|
21
|
|
|
* LICENSE: |
|
22
|
|
|
* |
|
23
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
24
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
25
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
26
|
|
|
* License, or (at your option) any later version. |
|
27
|
|
|
* |
|
28
|
|
|
* This program is distributed in the hope that it will be useful, |
|
29
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
30
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
31
|
|
|
* GNU Affero General Public License for more details. |
|
32
|
|
|
* |
|
33
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
34
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
35
|
|
|
*/ |
|
36
|
|
|
|
|
37
|
|
|
class Model extends DispatcherClassModel { |
|
38
|
|
|
|
|
39
|
1 |
|
public function __construct( |
|
40
|
|
|
Configuration $configuration, |
|
41
|
|
|
LoggerInterface $logger, |
|
42
|
|
|
Cache $cache, |
|
43
|
1 |
|
Extra $extra |
|
44
|
|
|
) { |
|
45
|
|
|
|
|
46
|
1 |
|
parent::__construct($configuration, $logger); |
|
47
|
|
|
|
|
48
|
1 |
|
$this->route = null; |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
1 |
|
$this->request = null; |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
1 |
|
$this->response = null; |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
1 |
|
$this->table = new Table($cache, $this); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
1 |
|
$this->cache = $cache; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
1 |
|
$this->extra = $extra; |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
1 |
|
$this->bypass_routing = false; |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
1 |
|
$this->bypass_service = false; |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function bypassRouting(Route $route) { |
|
67
|
|
|
|
|
68
|
1 |
|
$this->bypass_routing = true; |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
1 |
|
$this->route = $route; |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
1 |
|
return $this; |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function bypassService() { |
|
77
|
|
|
|
|
78
|
|
|
$this->bypass_service = true; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getRoute() { |
|
85
|
|
|
|
|
86
|
|
|
return $this->route; |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
public function route(Request $request) { |
|
91
|
|
|
|
|
92
|
2 |
|
$method = $request->method->get(); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
2 |
|
$methods = $this->configuration->get('allowed-http-methods'); |
|
95
|
|
|
|
|
96
|
2 |
|
if ( ( $methods != null || !empty($methods) ) && in_array($method, $methods) === false ) { |
|
97
|
|
|
|
|
98
|
|
|
throw new DispatcherException("Method not allowed", 0, null, 405, array( |
|
99
|
|
|
"Allow" => implode(",",$methods) |
|
100
|
|
|
)); |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
$this->request = $request; |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
2 |
|
if (!$this->bypass_routing) { |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
1 |
|
if (!$this->parse()) throw new DispatcherException("Unable to find a valid route for the specified uri", 0, null, 404); |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
return $this->route; |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
public function getServiceInstance() { |
|
117
|
|
|
|
|
118
|
1 |
|
$class = $this->route->getClassName(); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
1 |
|
if (class_exists($class)) { |
|
121
|
|
|
|
|
122
|
|
|
// All the route parameters are also added to the query parameters |
|
123
|
1 |
|
foreach ($this->route->getRequestParameters() as $parameter => $value) { |
|
|
|
|
|
|
124
|
|
|
$this->request->query->set($parameter, $value); |
|
|
|
|
|
|
125
|
1 |
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
return new $class( |
|
128
|
1 |
|
$this->configuration, |
|
129
|
1 |
|
$this->logger, |
|
130
|
1 |
|
$this->request, |
|
|
|
|
|
|
131
|
1 |
|
$this, |
|
132
|
1 |
|
$this->response, |
|
|
|
|
|
|
133
|
1 |
|
$this->extra |
|
|
|
|
|
|
134
|
1 |
|
); |
|
135
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
else return null; |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
public function compose(Response $response) { |
|
142
|
|
|
|
|
143
|
1 |
|
$this->response = $response; |
|
|
|
|
|
|
144
|
|
|
|
|
145
|
1 |
|
if (is_null($this->route)) { |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
throw new DispatcherException("Route has not been loaded!"); |
|
148
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
1 |
|
if ( $this->bypass_service ) { |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
return; |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
1 |
|
$service = $this->getServiceInstance(); |
|
158
|
|
|
|
|
159
|
1 |
|
if (!is_null($service)) { |
|
160
|
|
|
|
|
161
|
1 |
|
$result = ""; |
|
162
|
|
|
|
|
163
|
1 |
|
$method = $this->request->method->get(); |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
1 |
|
$methods = $service->getImplementedMethods(); |
|
166
|
|
|
|
|
167
|
1 |
|
if ( in_array($method, $methods) ) { |
|
168
|
|
|
|
|
169
|
1 |
|
$callable = $service->getMethod($method); |
|
170
|
|
|
|
|
171
|
|
|
try { |
|
172
|
|
|
|
|
173
|
1 |
|
$result = call_user_func(array($service, $callable)); |
|
174
|
|
|
|
|
175
|
1 |
|
} catch (DispatcherException $de) { |
|
176
|
|
|
|
|
177
|
|
|
throw $de; |
|
178
|
|
|
|
|
179
|
|
|
} catch (Exception $e) { |
|
180
|
|
|
|
|
181
|
|
|
throw new DispatcherException(sprintf("Service '%s' execution failed for method '%s': %s", $this->route->getClassName(), $method, $e->getMessage()), 0, $e, 500); |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
1 |
|
} else { |
|
186
|
|
|
|
|
187
|
|
|
throw new DispatcherException(sprintf("Service '%s' doesn't implement method '%s'", $this->route->getServiceName(), $method), 0, null, 501, array( |
|
|
|
|
|
|
188
|
|
|
"Allow" => implode(",", $methods) |
|
189
|
|
|
)); |
|
190
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
1 |
|
$this->response->content->set($result); |
|
|
|
|
|
|
194
|
|
|
|
|
195
|
1 |
|
} else { |
|
196
|
|
|
|
|
197
|
|
|
throw new DispatcherException(sprintf("Unable to execute service '%s'", $this->route->getClassName()), 0, null, 500); |
|
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
1 |
|
} |
|
202
|
|
|
|
|
203
|
1 |
|
private function parse() { |
|
204
|
|
|
|
|
205
|
1 |
|
$path = $this->request->route(); |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
1 |
|
foreach ($this->table->routes as $regex => $value) { |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
// The current uri is checked against all the global regular expressions associated with the routes |
|
210
|
|
|
if (preg_match("/" . $regex . "/", $path, $matches)) { |
|
211
|
|
|
|
|
212
|
|
|
/* If a route is matched, all the bits of the route string are evalued in order to create |
|
213
|
|
|
* new query parameters which will be available for the service class |
|
214
|
|
|
*/ |
|
215
|
|
|
$this->route = $value->path($matches); |
|
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
return true; |
|
218
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
1 |
|
} |
|
222
|
|
|
|
|
223
|
1 |
|
return false; |
|
224
|
|
|
|
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
} |
|
228
|
|
|
|
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@propertyannotation 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.