1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Controller\Rest; |
13
|
|
|
|
14
|
|
|
use Phalcon\Http\ResponseInterface; |
15
|
|
|
use Phalcon\Mvc\Dispatcher; |
16
|
|
|
use Zemit\Mvc\Controller\AbstractTrait\AbstractInjectable; |
17
|
|
|
use Zemit\Http\StatusCode; |
18
|
|
|
use Zemit\Support\Utils; |
19
|
|
|
|
20
|
|
|
trait Response { |
21
|
|
|
|
22
|
|
|
use AbstractInjectable; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Set the REST response error |
26
|
|
|
* |
27
|
|
|
* @param int $code The HTTP status code (default: 400) |
28
|
|
|
* @param string $status The status message (default: 'Bad Request') |
29
|
|
|
* @param mixed $response The response body (default: null) |
30
|
|
|
* @return ResponseInterface The REST response object |
31
|
|
|
*/ |
32
|
|
|
public function setRestErrorResponse(int $code = 400, string $status = 'Bad Request', mixed $response = null): ResponseInterface |
33
|
|
|
{ |
34
|
|
|
return $this->setRestResponse($response, $code, $status); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sending rest response as a http response |
39
|
|
|
* |
40
|
|
|
* @param mixed $response |
41
|
|
|
* @param ?int $code |
42
|
|
|
* @param ?string $status |
43
|
|
|
* @param int $jsonOptions |
44
|
|
|
* @param int $depth |
45
|
|
|
* |
46
|
|
|
* @return ResponseInterface |
47
|
|
|
*/ |
48
|
|
|
public function setRestResponse(mixed $response = null, int $code = null, string $status = null, int $jsonOptions = 0, int $depth = 512): ResponseInterface |
49
|
|
|
{ |
50
|
|
|
$debug = $this->isDebugEnabled(); |
|
|
|
|
51
|
|
|
|
52
|
|
|
// keep forced status code or set our own |
53
|
|
|
$statusCode = $this->response->getStatusCode(); |
|
|
|
|
54
|
|
|
$reasonPhrase = $this->response->getReasonPhrase(); |
55
|
|
|
$code ??= (int)$statusCode ?: 200; |
56
|
|
|
$status ??= $reasonPhrase ?: StatusCode::getMessage($code); |
57
|
|
|
|
58
|
|
|
$view = $this->view->getParamsToView(); |
|
|
|
|
59
|
|
|
$hash = hash('sha512', json_encode($view)); // @todo store hash in cache layer with response content |
60
|
|
|
|
61
|
|
|
// set response status code |
62
|
|
|
$this->response->setStatusCode($code, $code . ' ' . $status); |
63
|
|
|
|
64
|
|
|
// @todo handle this correctly |
65
|
|
|
// @todo private vs public cache type |
66
|
|
|
$cache = $this->getCache(); |
|
|
|
|
67
|
|
|
if (!empty($cache['lifetime'])) { |
68
|
|
|
if ($this->response->getStatusCode() === 200) { |
69
|
|
|
$this->response->setCache($cache['lifetime']); |
70
|
|
|
$this->response->setEtag($hash); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
else { |
74
|
|
|
$this->response->setCache(0); |
75
|
|
|
$this->response->setHeader('Cache-Control', 'no-cache, max-age=0'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$ret = []; |
79
|
|
|
$ret['api'] = []; |
80
|
|
|
$ret['api']['version'] = ['0.1']; // @todo |
81
|
|
|
$ret['timestamp'] = date('c'); |
82
|
|
|
$ret['hash'] = $hash; |
83
|
|
|
$ret['status'] = $status; |
84
|
|
|
$ret['code'] = $code; |
85
|
|
|
$ret['response'] = $response; |
86
|
|
|
$ret['view'] = $view; |
87
|
|
|
|
88
|
|
|
if ($debug) { |
89
|
|
|
$ret['api']['php'] = phpversion(); |
90
|
|
|
$ret['api']['phalcon'] = $this->config->path('phalcon.version'); |
91
|
|
|
$ret['api']['zemit'] = $this->config->path('core.version'); |
92
|
|
|
$ret['api']['core'] = $this->config->path('core.name'); |
93
|
|
|
$ret['api']['app'] = $this->config->path('app.version'); |
94
|
|
|
$ret['api']['name'] = $this->config->path('app.name'); |
95
|
|
|
|
96
|
|
|
$ret['identity'] = $this->identity ? $this->identity->getIdentity() : null; |
|
|
|
|
97
|
|
|
$ret['profiler'] = $this->profiler ? $this->profiler->toArray() : null; |
98
|
|
|
$ret['request'] = $this->request ? $this->request->toArray() : null; |
|
|
|
|
99
|
|
|
$ret['dispatcher'] = $this->dispatcher ? $this->dispatcher->toArray() : null; |
|
|
|
|
100
|
|
|
$ret['router'] = $this->router ? $this->router->toArray() : null; |
|
|
|
|
101
|
|
|
$ret['memory'] = Utils::getMemoryUsage(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->response->setJsonContent($ret, $jsonOptions, $depth); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Update the Dispatcher after executing the route. |
109
|
|
|
* |
110
|
|
|
* @param Dispatcher $dispatcher The Dispatcher instance. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function afterExecuteRoute(Dispatcher $dispatcher): void |
115
|
|
|
{ |
116
|
|
|
$response = $dispatcher->getReturnedValue(); |
117
|
|
|
|
118
|
|
|
// Avoid breaking default phalcon behaviour |
119
|
|
|
if ($response instanceof \Phalcon\Http\Response) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Merge response into view variables |
124
|
|
|
if (is_array($response)) { |
125
|
|
|
$this->view->setVars($response, true); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Return our Rest normalized response |
129
|
|
|
$dispatcher->setReturnedValue($this->setRestResponse(is_array($response) ? null : $response)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|