Skip to content

Commit 3c76436

Browse files
author
Designcise
committed
Added JSONP support
1 parent 0ae3e31 commit 3c76436

File tree

7 files changed

+133
-6
lines changed

7 files changed

+133
-6
lines changed

LICENSE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Portions of the "BitFrame Whoops Middleware" incorporates the work by (as provided under the MIT lincense):
22

33
* Copyright (c) 2015-2017 Franz Liedke
4+
* Copyright (c) 2013-2018 Filipe Dobreira (http://github.com/filp)
45

56
All other copyright for the "BitFrame Whoops Middleware" are held by:
67

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "designcise/bitframe-whoops",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"type": "library",
55
"description": "Whoops error handler middleware for BitFrame microframework",
66
"license": "MIT",

src/FormatNegotiator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Franz Liedke
1010
* @copyright Copyright (c) 2015-2017 Franz Liedke
1111
*
12-
* @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License
12+
* @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License
1313
*/
1414

1515
namespace BitFrame\ErrorHandler;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

src/WhoopsErrorHandler.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Franz Liedke
1010
* @copyright Copyright (c) 2015-2017 Franz Liedke
1111
*
12-
* @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License
12+
* @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License
1313
*/
1414

1515
namespace BitFrame\ErrorHandler;
@@ -18,9 +18,11 @@
1818
use \Psr\Http\Server\{RequestHandlerInterface, MiddlewareInterface};
1919

2020
use \Whoops\Run;
21+
use \Whoops\Util\Misc;
2122
use \Whoops\Handler\{PlainTextHandler, JsonResponseHandler, XmlResponseHandler, PrettyPageHandler};
2223

2324
use BitFrame\Delegate\CallableMiddlewareTrait;
25+
use BitFrame\ErrorHandler\Handler\JsonpResponseHandler;
2426

2527
/**
2628
* Whoops error handler middleware to handle application
@@ -139,13 +141,23 @@ public static function getWhoopsInstance(
139141
// select appropriate handler as per the requested format
140142
$format = ((PHP_SAPI === 'cli')) ? 'text' : (
141143
($format === 'auto') ? (
142-
(\Whoops\Util\Misc::isAjaxRequest()) ? 'json' : FormatNegotiator::getPreferredFormat($request)
144+
(Misc::isAjaxRequest()) ? 'json' : FormatNegotiator::getPreferredFormat($request)
143145
) : $format
144146
);
145147

146148
switch ($format) {
147149
case 'json':
148150
$handler = new JsonResponseHandler;
151+
152+
// is a jsonp request?
153+
if (
154+
$request->getMethod() === 'GET' &&
155+
! empty($callback = $request->getQueryParam('callback', ''))
156+
) {
157+
// use the custom jsonp response handler
158+
$handler = new JsonpResponseHandler($callback);
159+
}
160+
149161
$handler->addTraceToOutput($showTrace);
150162
$handler->setJsonApi(true);
151163
break;

test/FormatNegotiatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Franz Liedke
1010
* @copyright Copyright (c) 2015-2017 Franz Liedke
1111
*
12-
* @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License
12+
* @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License
1313
*/
1414

1515
namespace BitFrame\Test;

test/WhoopsErrorHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Daniyal Hamid
77
* @copyright Copyright (c) 2017-2018 Daniyal Hamid (https://designcise.com)
88
*
9-
* @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License
9+
* @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License
1010
*/
1111

1212
namespace BitFrame\Test;

0 commit comments

Comments
 (0)