This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
/
Abstract.php
338 lines (291 loc) · 9.75 KB
/
Abstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
use CM\Url\Url;
abstract class CM_Http_Response_Abstract extends CM_Class_Abstract implements CM_Service_ManagerAwareInterface {
use CM_Service_ManagerAwareTrait;
/** @var CM_Http_Request_Abstract */
protected $_request;
/** @var CM_Frontend_Render */
private $_render = null;
/** @var CM_Site_Abstract */
protected $_site = null;
/** @var array */
private $_headers = array();
/** @var array */
private $_rawHeaders = array();
/** @var array */
private $_cookies = array();
/** @var null|string */
private $_content = null;
/** @var string|null */
private $_stringRepresentation;
/**
* @param CM_Http_Request_Abstract $request
* @param CM_Site_Abstract $site
* @param CM_Service_Manager $serviceManager
*/
public function __construct(CM_Http_Request_Abstract $request, CM_Site_Abstract $site, CM_Service_Manager $serviceManager) {
$this->setServiceManager($serviceManager);
$this->_request = $request;
$this->_site = $site;
}
abstract protected function _process();
public function process() {
$this->getServiceManager()->getLogger()->getContext()->setUserWithClosure(function () {
return $this->getViewer();
});
$this->_process();
if ($this->getRequest()->hasSession()) {
$session = $this->getRequest()->getSession();
if (!$session->isEmpty()) {
$sessionExpiration = $session->hasLifetime() ? time() + $session->getLifetime() : null;
$this->setCookie('sessionId', $session->getId(), $sessionExpiration);
} elseif ($this->getRequest()->getCookie('sessionId')) {
$this->deleteCookie('sessionId');
}
}
if ($this->getRequest()->hasClientId()) {
$requestClientId = $this->getRequest()->getClientId();
if ($this->getRequest()->getCookie('clientId') != $requestClientId) {
$this->setCookie('clientId', (string) $requestClientId, time() + (20 * 365 * 24 * 60 * 60));
}
}
$name = $this->_getStringRepresentation();
$this->getServiceManager()->getNewrelic()->setNameTransaction($name);
}
/**
* @return CM_Http_Request_Abstract
*/
public function getRequest() {
return $this->_request;
}
/**
* @return CM_Site_Abstract
*/
public function getSite() {
return $this->_site;
}
/**
* @return Url
*/
public function getUrl() {
$request = $this->getRequest();
$url = Url::create($request->getPath(), $this->getRender()->getEnvironment());
/** @var Url $url */
$url = $url->withParams($request->getQuery());
return $url;
}
/**
* @param bool $needed OPTIONAL Throw an CM_Exception_AuthRequired if not authenticated
* @return CM_Model_User|null
* @throws CM_Exception_AuthRequired
*/
public function getViewer($needed = false) {
return $this->getRequest()->getViewer($needed);
}
/**
* @return string|null
*/
public function getContent() {
return $this->_content;
}
/**
* @return CM_Frontend_Render
*/
public function getRender() {
if (!$this->_render) {
$this->_render = $this->createRender();
}
return $this->_render;
}
/**
* @return CM_Frontend_Render
*/
public function createRender() {
return new CM_Frontend_Render($this->getEnvironment(), $this->getServiceManager());
}
/**
* @return CM_Frontend_Environment
* @throws CM_Exception_AuthRequired
*/
public function getEnvironment() {
$request = $this->getRequest();
$location = $request->getLocation();
$viewer = $request->getViewer();
$currency = null;
if (null === $currency && null !== $viewer) {
$currency = $viewer->getCurrency();
}
if (null === $currency && null !== $location) {
$currency = CM_Model_Currency::findByLocation($location);
}
$clientDevice = new CM_Http_ClientDevice($request);
return new CM_Frontend_Environment($this->getSite(), $viewer, $request->getLanguage(), $request->getTimeZone(), null, $location, $currency, $clientDevice);
}
/**
* @return array
*/
public function getCookies() {
return $this->_cookies;
}
/**
* @return array
*/
public function getHeaders() {
$headers = $this->_rawHeaders;
foreach ($this->_headers as $key => $value) {
$headers[] = $key . ': ' . $value;
}
foreach ($this->_cookies as $name => $cookieParameters) {
$cookie = 'Set-Cookie: ' . $name . '=' . urlencode($cookieParameters['value']);
if (null !== $cookieParameters['expire']) {
$cookie .= '; Expires=' . date('D\, d\-M\-Y h:i:s e', (int) $cookieParameters['expire']);
}
$cookie .= '; Path=' . $cookieParameters['path'];
$headers[] = $cookie;
}
return $headers;
}
/**
* Sets not found header (can be server specific)
*/
public function setHeaderNotfound() {
$this->addHeaderRaw('HTTP/1.0 404 Not Found');
}
public function setHeaderDisableCache() {
# Via http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers/5493543#5493543
$this->setHeader('Cache-Control', 'no-store, must-revalidate');
}
/**
* @param int $maxAge
*/
public function setHeaderExpires($maxAge) {
$maxAge = (int) $maxAge;
$this->setHeader('Cache-Control', 'max-age=' . $maxAge);
$this->setHeader('Expires', gmdate('D, d M Y H:i:s \G\M\T', time() + $maxAge));
}
/**
* @param string $key
* @param string $value
*/
public function setHeader($key, $value) {
$this->_headers[$key] = $value;
}
/**
* @param string $name
* @param string $value
* @param int $expire
* @param string|null $path
*/
public function setCookie($name, $value, $expire = null, $path = null) {
if (null === $path) {
$path = '/';
}
if (null !== $expire) {
$expire = (int) $expire;
}
$name = (string) $name;
$value = (string) $value;
$path = (string) $path;
$this->_cookies[$name] = array('value' => $value, 'expire' => $expire, 'path' => $path);
}
/**
* @param string $header
*/
public function addHeaderRaw($header) {
$this->_rawHeaders[] = $header;
}
/**
* @param string $name
*/
public function deleteCookie($name) {
$this->setCookie($name, '', 1);
}
/**
* Processes all headers and sends them
*/
public function sendHeaders() {
foreach ($this->getHeaders() as $header) {
header($header, false);
}
}
public function sendContent() {
echo $this->getContent();
}
public function send() {
$this->sendHeaders();
$this->sendContent();
}
/**
* @param string $content
*/
protected function _setContent($content) {
$this->_content = (string) $content;
}
/**
* @param string $string
*/
protected function _setStringRepresentation($string) {
$this->_stringRepresentation = (string) $string;
}
/**
* @return string
*/
protected function _getStringRepresentation() {
if (null === $this->_stringRepresentation) {
return get_class($this);
}
return $this->_stringRepresentation;
}
/**
* @param Closure $regularCode
* @param Closure $errorCode
* @return mixed
* @throws CM_Exception
*/
protected function _runWithCatching(Closure $regularCode, Closure $errorCode) {
try {
return $regularCode();
} catch (CM_Exception $ex) {
$config = self::_getConfig();
$exceptionsToCatch = $config->exceptionsToCatch;
$catchPublicExceptions = !empty($config->catchPublicExceptions);
$errorOptions = \Functional\first($exceptionsToCatch, function ($options, $exceptionClass) use ($ex) {
return is_a($ex, $exceptionClass);
});
$catchException = null !== $errorOptions;
if ($catchException && isset($errorOptions['log']) && true === $errorOptions['log']) {
$logLevel = isset($errorOptions['level']) ? $errorOptions['level'] : null;
if (null === $logLevel) {
$logLevel = CM_Log_Logger::exceptionToLevel($ex);
}
$context = new CM_Log_Context();
$context->setUser($this->getViewer());
$context->setException($ex);
$this->getServiceManager()->getLogger()->addMessage('Response processing error', $logLevel, $context);
}
if (!$catchException && ($catchPublicExceptions && $ex->isPublic())) {
$errorOptions = [];
$catchException = true;
}
if ($catchException) {
return $errorCode($ex, $errorOptions);
}
throw $ex;
}
}
/**
* @param CM_Http_Request_Abstract $request
* @param CM_Site_Abstract $site
* @param CM_Service_Manager $serviceManager
* @return CM_Http_Response_Abstract|null
*/
public static function createFromRequest(CM_Http_Request_Abstract $request, CM_Site_Abstract $site, CM_Service_Manager $serviceManager) {
return null;
}
/**
* @return bool
*/
public static function catchAll() {
return false;
}
}