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 Cake\Core\Configure; |
16
|
|
|
use Exception; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class RawRenderer |
20
|
|
|
* Raw unformatted content negotiation Renderer. |
21
|
|
|
* |
22
|
|
|
* @package CakeDC\Api\Service\Renderer |
23
|
|
|
*/ |
24
|
|
|
class RawRenderer 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
|
1 |
|
public function response(Result $result = null) |
34
|
|
|
{ |
35
|
1 |
|
$response = $this->_service->getResponse(); |
36
|
1 |
|
$this->_service->setResponse($response->withStringBody((string)$result->getData())->withStatus($result->getCode()) |
|
|
|
|
37
|
1 |
|
->withType('text/plain')); |
38
|
|
|
|
39
|
1 |
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Processes an exception thrown while processing the request. |
44
|
|
|
* |
45
|
|
|
* @param Exception $exception The exception object. |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
1 |
|
public function error(Exception $exception) |
49
|
|
|
{ |
50
|
1 |
|
$response = $this->_service->getResponse(); |
51
|
1 |
|
$message = (Configure::read('debug') > 0) ? $exception->getMessage() . ' on line ' . $exception->getLine() . ' in ' . $exception->getFile() : $exception->getMessage(); |
52
|
1 |
|
$trace = $exception->getTrace(); |
53
|
1 |
|
$debug = (Configure::read('debug') > 0) ? "\n" . print_r($trace, true) : ''; |
54
|
1 |
|
$this->_service->setResponse($response->withStringBody($message . $debug)->withType('text/plain')); |
55
|
1 |
|
} |
56
|
|
|
} |
57
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: