|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: siim |
|
5
|
|
|
* Date: 17.01.19 |
|
6
|
|
|
* Time: 8:03 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Sf4\Api\Request; |
|
10
|
|
|
|
|
11
|
|
|
use Sf4\Api\Dto\DtoInterface; |
|
12
|
|
|
use Sf4\Api\Dto\Traits\DtoTrait; |
|
13
|
|
|
use Sf4\Api\RequestHandler\RequestHandlerTrait; |
|
14
|
|
|
use Sf4\Api\Response\ResponseInterface; |
|
15
|
|
|
use Sf4\Api\Response\ResponseTrait; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
|
|
19
|
|
|
abstract class AbstractRequest implements RequestInterface |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
use ResponseTrait; |
|
23
|
|
|
use DtoTrait; |
|
24
|
|
|
use RequestHandlerTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** @var Request|null $request */ |
|
27
|
|
|
protected $request; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* AbstractRequest constructor. |
|
31
|
|
|
* @param ResponseInterface $response |
|
32
|
|
|
* @param DtoInterface $dto |
|
33
|
|
|
*/ |
|
34
|
|
|
public function init(ResponseInterface $response, DtoInterface $dto = null) |
|
35
|
|
|
{ |
|
36
|
|
|
$response->setRequest($this); |
|
37
|
|
|
$this->setResponse($response); |
|
38
|
|
|
if ($dto) { |
|
39
|
|
|
$this->setDto($dto); |
|
40
|
|
|
$this->attachDtoToResponse(); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function attachDtoToResponse(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$response = $this->getResponse(); |
|
47
|
|
|
$dto = $this->getDto(); |
|
48
|
|
|
if (null !== $dto && null !== $response && null === $response->getDto()) { |
|
49
|
|
|
$response->setDto($dto); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param \Closure $closure |
|
55
|
|
|
* @param string|null $cacheKey |
|
56
|
|
|
* @param array $tags |
|
57
|
|
|
* @param int|null $expiresAfter |
|
58
|
|
|
* @throws \Psr\Cache\CacheException |
|
59
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getCachedResponse( |
|
62
|
|
|
\Closure $closure, |
|
63
|
|
|
string $cacheKey = null, |
|
64
|
|
|
array $tags = [], |
|
65
|
|
|
int $expiresAfter = null |
|
66
|
|
|
) { |
|
67
|
|
|
|
|
68
|
|
|
$requestHandler = $this->getRequestHandler(); |
|
69
|
|
|
if ($cacheKey && $requestHandler && $jsonResponse = $requestHandler->getResponse()) { |
|
70
|
|
|
$data = $requestHandler->getCacheDataOrAdd( |
|
71
|
|
|
$cacheKey, |
|
72
|
|
|
static function () use ($closure, $jsonResponse) { |
|
73
|
|
|
$closure(); |
|
74
|
|
|
return $jsonResponse->getContent(); |
|
75
|
|
|
}, |
|
76
|
|
|
$tags, |
|
77
|
|
|
$expiresAfter |
|
|
|
|
|
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$responseContent = json_decode($data, true); |
|
81
|
|
|
$response = $this->getResponse(); |
|
82
|
|
|
if ($response) { |
|
83
|
|
|
$response->setJsonResponse( |
|
84
|
|
|
new JsonResponse( |
|
85
|
|
|
$responseContent, |
|
86
|
|
|
$response->getResponseStatus(), |
|
87
|
|
|
$response->getResponseHeaders() |
|
88
|
|
|
) |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
} else { |
|
92
|
|
|
/* |
|
93
|
|
|
* Handle request |
|
94
|
|
|
*/ |
|
95
|
|
|
$closure(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @throws \Psr\Cache\CacheException |
|
101
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
|
102
|
|
|
*/ |
|
103
|
|
|
public function handle() |
|
104
|
|
|
{ |
|
105
|
|
|
$response = $this->getResponse(); |
|
106
|
|
|
$requestHandler = $this->getRequestHandler(); |
|
107
|
|
|
if ($response && $requestHandler && $translator = $requestHandler->getTranslator()) { |
|
108
|
|
|
$response->setTranslator($translator); |
|
109
|
|
|
} |
|
110
|
|
|
$this->getCachedResponse( |
|
111
|
|
|
function () use ($response) { |
|
112
|
|
|
$httpRequest = $this->getRequest(); |
|
113
|
|
|
if ($httpRequest) { |
|
114
|
|
|
$requestContent = $httpRequest->getContent(); |
|
115
|
|
|
$dto = $this->getDto(); |
|
116
|
|
|
if ($requestContent && $dto) { |
|
117
|
|
|
$data = json_decode($requestContent, true); |
|
118
|
|
|
$dto->populate($data); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
if ($response) { |
|
122
|
|
|
$response->init(); |
|
123
|
|
|
} |
|
124
|
|
|
}, |
|
125
|
|
|
$this->getCacheKey(), |
|
126
|
|
|
$this->getCacheTags(), |
|
127
|
|
|
$this->getCacheExpiresAfter() |
|
|
|
|
|
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return Request|null |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getRequest(): ?Request |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->request; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param Request|null $request |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setRequest(?Request $request): void |
|
143
|
|
|
{ |
|
144
|
|
|
$this->request = $request; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getRoute(): string |
|
151
|
|
|
{ |
|
152
|
|
|
return static::ROUTE; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return string|null |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function getCacheKey(): ?string |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->getUrl(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
abstract protected function getCacheTags(): array; |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return int|null |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function getCacheExpiresAfter(): ?int |
|
172
|
|
|
{ |
|
173
|
|
|
return null; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function getUrl(): string |
|
180
|
|
|
{ |
|
181
|
|
|
return $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|