Skip to content

Commit

Permalink
Added JSONP support
Browse files Browse the repository at this point in the history
  • Loading branch information
Designcise committed May 26, 2018
1 parent 0ae3e31 commit 3c76436
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 6 deletions.
1 change: 1 addition & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Portions of the "BitFrame Whoops Middleware" incorporates the work by (as provided under the MIT lincense):

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

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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "designcise/bitframe-whoops",
"version": "1.0.0",
"version": "1.0.1",
"type": "library",
"description": "Whoops error handler middleware for BitFrame microframework",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/FormatNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Franz Liedke
* @copyright Copyright (c) 2015-2017 Franz Liedke
*
* @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License
* @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License
*/

namespace BitFrame\ErrorHandler;
Expand Down
114 changes: 114 additions & 0 deletions src/Handler/JsonpResponseHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

/**
* BitFrame Framework (https://www.bitframephp.com)
*
* @author Daniyal Hamid
* @copyright Copyright (c) 2017-2018 Daniyal Hamid (https://designcise.com)
*
* @author Filipe Dobreira
* @copyright Copyright (c) 2013-2018 Filipe Dobreira (http://github.com/filp)
*
* @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License
*/

namespace BitFrame\ErrorHandler\Handler;

use \Whoops\Handler\Handler;
use \Whoops\Exception\Formatter;

/**
* Catches an exception and converts it to a JSONP
* response. Additionally can also return exception
* frames for consumption by an API. Based on
* \Whoops\Handler\JsonResponseHandler.
*/
class JsonpResponseHandler extends Handler
{
/** @var bool */
private $returnFrames = false;

/** @var bool */
private $jsonApi = false;

/** @var string */
private $callback;

/**
* @param string $callback JSONP callback
*/
public function __construct(string $callback)
{
$this->callback = $callback;
}

/**
* Returns errors[[]] instead of error[] to be in
* compliance with the json:api spec
*
* @param bool $jsonApi Default is false
*
* @return $this
*/
public function setJsonApi(bool $jsonApi = false): self
{
$this->jsonApi = (bool) $jsonApi;
return $this;
}

/**
* @param bool|null $returnFrames
*
* @return bool|$this
*/
public function addTraceToOutput(?bool $returnFrames = null)
{
if (func_num_args() == 0) {
return $this->returnFrames;
}

$this->returnFrames = (bool) $returnFrames;
return $this;
}

/**
* Handle errors.
*
* @return int
*/
public function handle(): int
{
if ($this->jsonApi === true) {
$response = [
'errors' => [
Formatter::formatExceptionAsDataArray(
$this->getInspector(),
$this->addTraceToOutput()
),
]
];
} else {
$response = [
'error' => Formatter::formatExceptionAsDataArray(
$this->getInspector(),
$this->addTraceToOutput()
),
];
}

$json = json_encode($response, defined('JSON_PARTIAL_OUTPUT_ON_ERROR') ? JSON_PARTIAL_OUTPUT_ON_ERROR : 0);
echo "{$this->callback}($json)";

return Handler::QUIT;
}

/**
* Get content type.
*
* @return string
*/
public function contentType(): string
{
return 'application/json';
}
}
16 changes: 14 additions & 2 deletions src/WhoopsErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Franz Liedke
* @copyright Copyright (c) 2015-2017 Franz Liedke
*
* @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License
* @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License
*/

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

use \Whoops\Run;
use \Whoops\Util\Misc;
use \Whoops\Handler\{PlainTextHandler, JsonResponseHandler, XmlResponseHandler, PrettyPageHandler};

use BitFrame\Delegate\CallableMiddlewareTrait;
use BitFrame\ErrorHandler\Handler\JsonpResponseHandler;

/**
* Whoops error handler middleware to handle application
Expand Down Expand Up @@ -139,13 +141,23 @@ public static function getWhoopsInstance(
// select appropriate handler as per the requested format
$format = ((PHP_SAPI === 'cli')) ? 'text' : (
($format === 'auto') ? (
(\Whoops\Util\Misc::isAjaxRequest()) ? 'json' : FormatNegotiator::getPreferredFormat($request)
(Misc::isAjaxRequest()) ? 'json' : FormatNegotiator::getPreferredFormat($request)
) : $format
);

switch ($format) {
case 'json':
$handler = new JsonResponseHandler;

// is a jsonp request?
if (
$request->getMethod() === 'GET' &&
! empty($callback = $request->getQueryParam('callback', ''))
) {
// use the custom jsonp response handler
$handler = new JsonpResponseHandler($callback);
}

$handler->addTraceToOutput($showTrace);
$handler->setJsonApi(true);
break;
Expand Down
2 changes: 1 addition & 1 deletion test/FormatNegotiatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Franz Liedke
* @copyright Copyright (c) 2015-2017 Franz Liedke
*
* @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License
* @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License
*/

namespace BitFrame\Test;
Expand Down
2 changes: 1 addition & 1 deletion test/WhoopsErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author Daniyal Hamid
* @copyright Copyright (c) 2017-2018 Daniyal Hamid (https://designcise.com)
*
* @license https://github.com/designcise/bitframe/blob/master/LICENSE.md MIT License
* @license https://github.com/designcise/bitframe-whoops/blob/master/LICENSE.md MIT License
*/

namespace BitFrame\Test;
Expand Down

0 comments on commit 3c76436

Please sign in to comment.