|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Bluz Framework Component |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Bluz PHP Team |
|
6
|
|
|
* @link https://github.com/bluzphp/framework |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @namespace |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace Bluz\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Bluz\Application\Exception\BadRequestException; |
|
15
|
|
|
use Bluz\Application\Exception\NotFoundException; |
|
16
|
|
|
use Bluz\Application\Exception\NotImplementedException; |
|
17
|
|
|
use Bluz\Proxy\Response; |
|
18
|
|
|
use Bluz\Proxy\Request; |
|
19
|
|
|
use Bluz\Proxy\Router; |
|
20
|
|
|
use Bluz\Validator\Exception\ValidatorException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Controller |
|
24
|
|
|
* |
|
25
|
|
|
* @package Bluz\Controller |
|
26
|
|
|
* @author Anton Shevchuk |
|
27
|
|
|
* @link https://github.com/bluzphp/framework/wiki/Controller-Rest |
|
28
|
|
|
*/ |
|
29
|
|
|
class Rest extends AbstractController |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string relation list |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $relation; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string relation Id |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $relationId; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array params of query |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $params = array(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array query data |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $data = array(); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Prepare request for processing |
|
53
|
|
|
*/ |
|
54
|
25 |
|
public function __construct() |
|
55
|
|
|
{ |
|
56
|
25 |
|
parent::__construct(); |
|
57
|
|
|
|
|
58
|
|
|
// get path |
|
59
|
|
|
// %module% / %controller% / %id% / %relation% / %id% |
|
60
|
25 |
|
$path = Router::getCleanUri(); |
|
61
|
|
|
|
|
62
|
25 |
|
$params = explode('/', rtrim($path, '/')); |
|
63
|
|
|
|
|
64
|
|
|
// module |
|
65
|
25 |
|
array_shift($params); |
|
66
|
|
|
|
|
67
|
|
|
// controller |
|
68
|
25 |
|
array_shift($params); |
|
69
|
|
|
|
|
70
|
25 |
|
if (sizeof($params)) { |
|
71
|
13 |
|
$this->primary = explode('-', array_shift($params)); |
|
72
|
13 |
|
} |
|
73
|
25 |
|
if (sizeof($params)) { |
|
74
|
2 |
|
$this->relation = array_shift($params); |
|
75
|
2 |
|
} |
|
76
|
25 |
|
if (sizeof($params)) { |
|
77
|
1 |
|
$this->relationId = array_shift($params); |
|
78
|
1 |
|
} |
|
79
|
25 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
* |
|
84
|
|
|
* Everyone method can return: |
|
85
|
|
|
* 401 Unauthorized - if authorization is required |
|
86
|
|
|
* 403 Forbidden - if user don't have permissions |
|
87
|
|
|
* 501 Not Implemented - if something not exists |
|
88
|
|
|
* |
|
89
|
|
|
* Methods can return: |
|
90
|
|
|
* HEAD /module/rest/ -> 200 // return overview of collection |
|
91
|
|
|
* HEAD /module/rest/id -> 200 // return overview of item |
|
92
|
|
|
* -> 404 // not found |
|
93
|
|
|
* GET /module/rest/ -> 200 // return collection or |
|
94
|
|
|
* -> 206 // return part of collection |
|
95
|
|
|
* GET /module/rest/id -> 200 // return one item or |
|
96
|
|
|
* -> 404 // not found |
|
97
|
|
|
* POST /module/rest/ -> 201 // item created or |
|
98
|
|
|
* -> 400 // bad request, validation error |
|
99
|
|
|
* POST /module/rest/id -> 501 // error, not used in REST |
|
100
|
|
|
* PATCH /module/rest/ |
|
101
|
|
|
* PUT /module/rest/ -> 200 // all items was updated or |
|
102
|
|
|
* -> 207 // multi-status ? |
|
103
|
|
|
* PATCH /module/rest/id |
|
104
|
|
|
* PUT /module/rest/id -> 200 // item was updated or |
|
105
|
|
|
* -> 304 // item not modified or |
|
106
|
|
|
* -> 400 // bad request, validation error or |
|
107
|
|
|
* -> 404 // not found |
|
108
|
|
|
* DELETE /module/rest/ -> 204 // all items was deleted or |
|
109
|
|
|
* -> 207 // multi-status ? |
|
110
|
|
|
* DELETE /module/rest/id -> 204 // item was deleted |
|
111
|
|
|
* -> 404 // not found |
|
112
|
|
|
* |
|
113
|
|
|
* @return mixed |
|
114
|
|
|
* @throws NotImplementedException |
|
115
|
|
|
* @throws NotFoundException |
|
116
|
|
|
* @throws BadRequestException |
|
117
|
|
|
*/ |
|
118
|
25 |
|
public function __invoke() |
|
119
|
|
|
{ |
|
120
|
25 |
|
switch ($this->method) { |
|
121
|
25 |
|
case Request::METHOD_HEAD: |
|
122
|
25 |
|
case Request::METHOD_GET: |
|
123
|
8 |
|
return $this->methodGet(); |
|
124
|
|
|
// break |
|
125
|
17 |
|
case Request::METHOD_POST: |
|
126
|
4 |
|
return $this->methodPost(); |
|
127
|
|
|
// break |
|
128
|
13 |
|
case Request::METHOD_PATCH: |
|
129
|
13 |
|
case Request::METHOD_PUT: |
|
130
|
6 |
|
return $this->methodPut(); |
|
131
|
|
|
// break |
|
132
|
7 |
|
case Request::METHOD_DELETE: |
|
133
|
4 |
|
return $this->methodDelete(); |
|
134
|
|
|
// break |
|
135
|
3 |
|
case Request::METHOD_OPTIONS: |
|
136
|
2 |
|
return $this->methodOptions(); |
|
137
|
|
|
// break |
|
138
|
1 |
|
default: |
|
139
|
1 |
|
throw new NotImplementedException(); |
|
140
|
1 |
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Method HEAD and GET |
|
145
|
|
|
* |
|
146
|
|
|
* @return mixed |
|
147
|
|
|
*/ |
|
148
|
8 |
|
public function methodGet() |
|
149
|
|
|
{ |
|
150
|
8 |
|
if (!empty($this->primary)) { |
|
151
|
|
|
// @throws NotFoundException |
|
152
|
4 |
|
$result = $this->readOne($this->primary); |
|
153
|
3 |
|
return [$result]; |
|
154
|
|
|
} else { |
|
155
|
|
|
// setup default offset and limit - safe way |
|
156
|
4 |
|
$offset = isset($this->params['offset'])?$this->params['offset']:0; |
|
157
|
4 |
|
$limit = isset($this->params['limit'])?$this->params['limit']:10; |
|
158
|
|
|
|
|
159
|
4 |
|
if ($range = Request::getHeader('Range')) { |
|
160
|
1 |
|
list(, $offset, $last) = preg_split('/[-=]/', $range); |
|
161
|
|
|
// for better compatibility |
|
162
|
1 |
|
$limit = $last - $offset; |
|
163
|
1 |
|
} |
|
164
|
4 |
|
return $this->readSet($offset, $limit, $this->params); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Method POST |
|
170
|
|
|
* |
|
171
|
|
|
* @return array|false |
|
172
|
|
|
* @throws BadRequestException |
|
173
|
|
|
* @throws NotImplementedException |
|
174
|
|
|
*/ |
|
175
|
4 |
|
public function methodPost() |
|
176
|
|
|
{ |
|
177
|
4 |
|
if (!empty($this->primary)) { |
|
178
|
|
|
// POST + ID is incorrect behaviour |
|
179
|
1 |
|
throw new NotImplementedException(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
try { |
|
183
|
3 |
|
$result = $this->createOne($this->data); |
|
184
|
1 |
|
if (!$result) { |
|
185
|
|
|
// system can't create record with this data |
|
186
|
|
|
throw new BadRequestException(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
1 |
|
if (is_array($result)) { |
|
190
|
1 |
|
$result = join('-', array_values($result)); |
|
191
|
1 |
|
} |
|
192
|
|
|
|
|
193
|
3 |
|
} catch (ValidatorException $e) { |
|
194
|
2 |
|
Response::setStatusCode(400); |
|
195
|
2 |
|
return ['errors' => $e->getErrors()]; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
1 |
|
Response::setStatusCode(201); |
|
199
|
1 |
|
Response::setHeader( |
|
200
|
1 |
|
'Location', |
|
201
|
1 |
|
Router::getUrl(Request::getModule(), Request::getController()).'/'.$result |
|
202
|
1 |
|
); |
|
203
|
1 |
|
return false; // disable view |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Method PUT |
|
208
|
|
|
* |
|
209
|
|
|
* @return array|false |
|
210
|
|
|
* @throws BadRequestException |
|
211
|
|
|
*/ |
|
212
|
6 |
|
public function methodPut() |
|
213
|
|
|
{ |
|
214
|
6 |
|
if (!sizeof($this->data)) { |
|
215
|
|
|
// data not found |
|
216
|
1 |
|
throw new BadRequestException(); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
try { |
|
220
|
5 |
|
if (!empty($this->primary)) { |
|
221
|
|
|
// update one item |
|
222
|
4 |
|
$result = $this->updateOne($this->primary, $this->data); |
|
223
|
2 |
|
} else { |
|
224
|
|
|
// update collection |
|
225
|
1 |
|
$result = $this->updateSet($this->data); |
|
226
|
|
|
} |
|
227
|
|
|
// if $result === 0 it's means a update is not apply |
|
228
|
|
|
// or records not found |
|
229
|
2 |
|
if (0 === $result) { |
|
230
|
1 |
|
Response::setStatusCode(304); |
|
231
|
1 |
|
} |
|
232
|
5 |
|
} catch (ValidatorException $e) { |
|
233
|
1 |
|
Response::setStatusCode(400); |
|
234
|
1 |
|
return ['errors' => $e->getErrors()]; |
|
235
|
|
|
} |
|
236
|
2 |
|
return false; // disable view |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Method DELETE |
|
241
|
|
|
* |
|
242
|
|
|
* @return false |
|
243
|
|
|
* @throws BadRequestException |
|
244
|
|
|
*/ |
|
245
|
4 |
|
public function methodDelete() |
|
246
|
|
|
{ |
|
247
|
4 |
|
if (!empty($this->primary)) { |
|
248
|
|
|
// delete one |
|
249
|
|
|
// @throws NotFoundException |
|
250
|
2 |
|
$this->deleteOne($this->primary); |
|
251
|
1 |
|
} else { |
|
252
|
|
|
// delete collection |
|
253
|
|
|
// @throws NotFoundException |
|
254
|
2 |
|
if (!sizeof($this->data)) { |
|
255
|
|
|
// data not exist |
|
256
|
1 |
|
throw new BadRequestException(); |
|
257
|
|
|
} |
|
258
|
1 |
|
$this->deleteSet($this->data); |
|
259
|
|
|
} |
|
260
|
1 |
|
Response::setStatusCode(204); |
|
261
|
1 |
|
return false; // disable view |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Method OPTIONS |
|
266
|
|
|
* |
|
267
|
|
|
* @return false |
|
268
|
|
|
*/ |
|
269
|
2 |
|
public function methodOptions() |
|
270
|
|
|
{ |
|
271
|
2 |
|
$allow = $this->getMethods(sizeof($this->primary)); |
|
272
|
2 |
|
Response::setHeader('Allow', join(',', $allow)); |
|
273
|
2 |
|
return false; // no body |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Get allowed methods by CRUD |
|
278
|
|
|
* |
|
279
|
|
|
* @param bool $primary |
|
280
|
|
|
* @return array |
|
281
|
|
|
*/ |
|
282
|
2 |
|
protected function getMethods($primary = false) |
|
283
|
|
|
{ |
|
284
|
2 |
|
$methods = $this->getCrud()->getMethods(); |
|
285
|
|
|
|
|
286
|
2 |
|
$allow = [Request::METHOD_HEAD, Request::METHOD_OPTIONS]; |
|
287
|
|
|
|
|
288
|
2 |
|
if ($primary) { |
|
289
|
1 |
|
if (in_array('readOne', $methods)) { |
|
290
|
1 |
|
$allow[] = Request::METHOD_GET; |
|
291
|
1 |
|
} |
|
292
|
1 |
|
if (in_array('updateOne', $methods)) { |
|
293
|
1 |
|
$allow[] = Request::METHOD_PATCH; |
|
294
|
1 |
|
$allow[] = Request::METHOD_PUT; |
|
295
|
1 |
|
} |
|
296
|
1 |
|
if (in_array('deleteOne', $methods)) { |
|
297
|
1 |
|
$allow[] = Request::METHOD_DELETE; |
|
298
|
1 |
|
} |
|
299
|
1 |
|
} else { |
|
300
|
1 |
|
if (in_array('readSet', $methods)) { |
|
301
|
1 |
|
$allow[] = Request::METHOD_GET; |
|
302
|
1 |
|
} |
|
303
|
1 |
|
if (in_array('createOne', $methods) |
|
304
|
1 |
|
|| in_array('createSet', $methods)) { |
|
305
|
1 |
|
$allow[] = Request::METHOD_POST; |
|
306
|
1 |
|
} |
|
307
|
1 |
|
if (in_array('updateSet', $methods)) { |
|
308
|
|
|
$allow[] = Request::METHOD_PATCH; |
|
309
|
|
|
$allow[] = Request::METHOD_PUT; |
|
310
|
|
|
} |
|
311
|
1 |
|
if (in_array('deleteSet', $methods)) { |
|
312
|
|
|
$allow[] = Request::METHOD_DELETE; |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
2 |
|
return $allow; |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|