|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service\Renderer; |
|
13
|
|
|
|
|
14
|
|
|
use CakeDC\Api\Service\Action\Result; |
|
15
|
|
|
use CakeDC\Api\Service\Renderer\BaseRenderer; |
|
16
|
|
|
use Cake\Core\Configure; |
|
17
|
|
|
use Exception; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class FileRenderer |
|
21
|
|
|
* |
|
22
|
|
|
* @package CakeDC\Api\Service\Renderer |
|
23
|
|
|
*/ |
|
24
|
|
|
class FileRenderer extends BaseRenderer |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Builds the HTTP response. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Result $result The result object returned by the Service. |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
|
|
public function response(Result $result = null) |
|
34
|
|
|
{ |
|
35
|
|
|
$response = $this->_service |
|
36
|
|
|
->getResponse() |
|
37
|
|
|
->withFile($result->getData()) |
|
|
|
|
|
|
38
|
|
|
->withStatus($result->getCode()); |
|
39
|
|
|
$this->_service->setResponse($response); |
|
40
|
|
|
|
|
41
|
|
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Processes an exception thrown while processing the request. |
|
46
|
|
|
* |
|
47
|
|
|
* @param Exception $exception The exception object. |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
public function error(Exception $exception) |
|
51
|
|
|
{ |
|
52
|
|
|
$response = $this->_service->getResponse(); |
|
53
|
|
|
$data = [ |
|
54
|
|
|
'error' => [ |
|
55
|
|
|
'code' => $exception->getCode(), |
|
56
|
|
|
'message' => $this->_buildMessage($exception) |
|
57
|
|
|
] |
|
58
|
|
|
]; |
|
59
|
|
|
if (Configure::read('debug') > 0) { |
|
60
|
|
|
$data['error']['trace'] = $this->_stackTrace($exception); |
|
61
|
|
|
} |
|
62
|
|
|
if ($exception instanceof ValidationException) { |
|
|
|
|
|
|
63
|
|
|
$data['error']['validation'] = $exception->getValidationErrors(); |
|
64
|
|
|
} |
|
65
|
|
|
$this->_service->setResponse($response->withStringBody($this->_encode($data))->withType('application/json')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Encoded object as json. In debug mode used pretty printed objects. |
|
70
|
|
|
* |
|
71
|
|
|
* @param mixed $data Encoded data. |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function _encode($data) |
|
75
|
|
|
{ |
|
76
|
|
|
$format = (Configure::read('debug') > 0) ? JSON_PRETTY_PRINT : 0; |
|
77
|
|
|
|
|
78
|
|
|
return json_encode($data, $format); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: