1
|
|
|
<?php namespace Comodojo\Dispatcher\Router; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\AbstractModel; |
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\Foundation\Base\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 AbstractModel { |
38
|
|
|
|
39
|
|
|
protected $mode = self::PROTECTDATA; |
40
|
|
|
|
41
|
1 |
|
public function __construct( |
42
|
|
|
Configuration $configuration, |
43
|
|
|
LoggerInterface $logger, |
44
|
1 |
|
Cache $cache, |
45
|
|
|
Extra $extra |
46
|
|
|
) { |
47
|
|
|
|
48
|
1 |
|
parent::__construct($configuration, $logger); |
49
|
|
|
|
50
|
1 |
|
$this->setRaw('route', null); |
51
|
1 |
|
$this->setRaw('request', null); |
52
|
1 |
|
$this->setRaw('response', null); |
53
|
1 |
|
$this->setRaw('table', new Table($cache, $this)); |
54
|
1 |
|
$this->setRaw('cache', $cache); |
55
|
1 |
|
$this->setRaw('extra', $extra); |
56
|
1 |
|
$this->setRaw('bypass_routing', false); |
57
|
1 |
|
$this->setRaw('bypass_service', false); |
58
|
|
|
|
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
public function bypassRouting(Route $route) { |
62
|
|
|
|
63
|
1 |
|
$this->bypass_routing = true; |
|
|
|
|
64
|
|
|
|
65
|
1 |
|
$this->route = $route; |
|
|
|
|
66
|
|
|
|
67
|
1 |
|
return $this; |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function bypassService() { |
72
|
|
|
|
73
|
|
|
$this->bypass_service = true; |
|
|
|
|
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getRoute() { |
80
|
|
|
|
81
|
|
|
return $this->route; |
|
|
|
|
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
public function route(Request $request) { |
86
|
|
|
|
87
|
2 |
|
$method = $request->method->get(); |
|
|
|
|
88
|
|
|
|
89
|
2 |
|
$methods = $this->configuration->get('allowed-http-methods'); |
|
|
|
|
90
|
|
|
|
91
|
2 |
|
if ( ( $methods != null || !empty($methods) ) && in_array($method, $methods) === false ) { |
92
|
|
|
|
93
|
|
|
throw new DispatcherException("Method not allowed", 0, null, 405, array( |
94
|
|
|
"Allow" => implode(",",$methods) |
95
|
|
|
)); |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
$this->request = $request; |
|
|
|
|
100
|
|
|
|
101
|
2 |
|
if (!$this->bypass_routing) { |
|
|
|
|
102
|
|
|
|
103
|
1 |
|
if (!$this->parse()) throw new DispatcherException("Unable to find a valid route for the specified uri", 0, null, 404); |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
return $this->route; |
|
|
|
|
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
public function getServiceInstance() { |
112
|
|
|
|
113
|
1 |
|
$class = $this->route->getClassName(); |
|
|
|
|
114
|
|
|
|
115
|
1 |
|
if (class_exists($class)) { |
116
|
|
|
|
117
|
|
|
// All the route parameters are also added to the query parameters |
118
|
1 |
|
foreach ($this->route->getRequestParameters() as $parameter => $value) { |
|
|
|
|
119
|
|
|
$this->request->query->set($parameter, $value); |
|
|
|
|
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
return new $class( |
123
|
1 |
|
$this->configuration, |
|
|
|
|
124
|
1 |
|
$this->logger, |
|
|
|
|
125
|
1 |
|
$this->request, |
|
|
|
|
126
|
1 |
|
$this, |
127
|
1 |
|
$this->response, |
|
|
|
|
128
|
1 |
|
$this->extra |
|
|
|
|
129
|
1 |
|
); |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
else return null; |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function compose(Response $response) { |
137
|
|
|
|
138
|
1 |
|
$this->response = $response; |
|
|
|
|
139
|
|
|
|
140
|
1 |
|
if (is_null($this->route)) { |
|
|
|
|
141
|
|
|
|
142
|
|
|
throw new DispatcherException("Route has not been loaded!"); |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
if ( $this->bypass_service ) { |
|
|
|
|
147
|
|
|
|
148
|
|
|
return; |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
$service = $this->getServiceInstance(); |
153
|
|
|
|
154
|
1 |
|
if (!is_null($service)) { |
155
|
|
|
|
156
|
1 |
|
$result = ""; |
157
|
|
|
|
158
|
1 |
|
$method = $this->request->method->get(); |
|
|
|
|
159
|
|
|
|
160
|
1 |
|
$methods = $service->getImplementedMethods(); |
161
|
|
|
|
162
|
1 |
|
if ( in_array($method, $methods) ) { |
163
|
|
|
|
164
|
1 |
|
$callable = $service->getMethod($method); |
165
|
|
|
|
166
|
|
|
try { |
167
|
|
|
|
168
|
1 |
|
$result = call_user_func(array($service, $callable)); |
169
|
|
|
|
170
|
1 |
|
} catch (DispatcherException $de) { |
171
|
|
|
|
172
|
|
|
throw $de; |
173
|
|
|
|
174
|
|
|
} catch (Exception $e) { |
175
|
|
|
|
176
|
|
|
throw new DispatcherException(sprintf("Service '%s' execution failed for method '%s': %s", $this->route->getClassName(), $method, $e->getMessage()), 0, $e, 500); |
|
|
|
|
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
} else { |
181
|
|
|
|
182
|
|
|
throw new DispatcherException(sprintf("Service '%s' doesn't implement method '%s'", $this->route->getServiceName(), $method), 0, null, 501, array( |
|
|
|
|
183
|
|
|
"Allow" => implode(",", $methods) |
184
|
|
|
)); |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
$this->response->content->set($result); |
|
|
|
|
189
|
|
|
|
190
|
1 |
|
} else { |
191
|
|
|
|
192
|
|
|
throw new DispatcherException(sprintf("Unable to execute service '%s'", $this->route->getClassName()), 0, null, 500); |
|
|
|
|
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
} |
197
|
|
|
|
198
|
1 |
|
private function parse() { |
199
|
|
|
|
200
|
1 |
|
$path = $this->request->route(); |
|
|
|
|
201
|
|
|
|
202
|
1 |
|
foreach ($this->table->routes as $regex => $value) { |
|
|
|
|
203
|
|
|
|
204
|
|
|
// The current uri is checked against all the global regular expressions associated with the routes |
205
|
|
|
if (preg_match("/" . $regex . "/", $path, $matches)) { |
206
|
|
|
|
207
|
|
|
/* If a route is matched, all the bits of the route string are evalued in order to create |
208
|
|
|
* new query parameters which will be available for the service class |
209
|
|
|
*/ |
210
|
|
|
$this->route = $value->path($matches); |
|
|
|
|
211
|
|
|
|
212
|
|
|
return true; |
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
1 |
|
} |
217
|
|
|
|
218
|
1 |
|
return false; |
219
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
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.