|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * BitFrame Framework (https://www.bitframephp.com) |
| 5 | + * |
| 6 | + * @author Daniyal Hamid |
| 7 | + * @copyright Copyright (c) 2017-2018 Daniyal Hamid (https://designcise.com) |
| 8 | + * |
| 9 | + * @author Filipe Dobreira |
| 10 | + * @copyright Copyright (c) 2013-2018 Filipe Dobreira (http://github.com/filp) |
| 11 | + * |
| 12 | + * @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License |
| 13 | + */ |
| 14 | + |
| 15 | +namespace BitFrame\ErrorHandler\Handler; |
| 16 | + |
| 17 | +use \Whoops\Handler\Handler; |
| 18 | +use \Whoops\Exception\Formatter; |
| 19 | + |
| 20 | +/** |
| 21 | + * Catches an exception and converts it to a JSONP |
| 22 | + * response. Additionally can also return exception |
| 23 | + * frames for consumption by an API. Based on |
| 24 | + * \Whoops\Handler\JsonResponseHandler. |
| 25 | + */ |
| 26 | +class JsonpResponseHandler extends Handler |
| 27 | +{ |
| 28 | + /** @var bool */ |
| 29 | + private $returnFrames = false; |
| 30 | + |
| 31 | + /** @var bool */ |
| 32 | + private $jsonApi = false; |
| 33 | + |
| 34 | + /** @var string */ |
| 35 | + private $callback; |
| 36 | + |
| 37 | + /** |
| 38 | + * @param string $callback JSONP callback |
| 39 | + */ |
| 40 | + public function __construct(string $callback) |
| 41 | + { |
| 42 | + $this->callback = $callback; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns errors[[]] instead of error[] to be in |
| 47 | + * compliance with the json:api spec |
| 48 | + * |
| 49 | + * @param bool $jsonApi Default is false |
| 50 | + * |
| 51 | + * @return $this |
| 52 | + */ |
| 53 | + public function setJsonApi(bool $jsonApi = false): self |
| 54 | + { |
| 55 | + $this->jsonApi = (bool) $jsonApi; |
| 56 | + return $this; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param bool|null $returnFrames |
| 61 | + * |
| 62 | + * @return bool|$this |
| 63 | + */ |
| 64 | + public function addTraceToOutput(?bool $returnFrames = null) |
| 65 | + { |
| 66 | + if (func_num_args() == 0) { |
| 67 | + return $this->returnFrames; |
| 68 | + } |
| 69 | + |
| 70 | + $this->returnFrames = (bool) $returnFrames; |
| 71 | + return $this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Handle errors. |
| 76 | + * |
| 77 | + * @return int |
| 78 | + */ |
| 79 | + public function handle(): int |
| 80 | + { |
| 81 | + if ($this->jsonApi === true) { |
| 82 | + $response = [ |
| 83 | + 'errors' => [ |
| 84 | + Formatter::formatExceptionAsDataArray( |
| 85 | + $this->getInspector(), |
| 86 | + $this->addTraceToOutput() |
| 87 | + ), |
| 88 | + ] |
| 89 | + ]; |
| 90 | + } else { |
| 91 | + $response = [ |
| 92 | + 'error' => Formatter::formatExceptionAsDataArray( |
| 93 | + $this->getInspector(), |
| 94 | + $this->addTraceToOutput() |
| 95 | + ), |
| 96 | + ]; |
| 97 | + } |
| 98 | + |
| 99 | + $json = json_encode($response, defined('JSON_PARTIAL_OUTPUT_ON_ERROR') ? JSON_PARTIAL_OUTPUT_ON_ERROR : 0); |
| 100 | + echo "{$this->callback}($json)"; |
| 101 | + |
| 102 | + return Handler::QUIT; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get content type. |
| 107 | + * |
| 108 | + * @return string |
| 109 | + */ |
| 110 | + public function contentType(): string |
| 111 | + { |
| 112 | + return 'application/json'; |
| 113 | + } |
| 114 | +} |
0 commit comments