mirror of
https://github.com/filp/whoops.git
synced 2026-09-14 19:56:23 +00:00
Add ability to trigger JsonResponseHandler for AJAX only
This commit is contained in:
@@ -19,8 +19,10 @@ abstract class Handler implements HandlerInterface
|
||||
* Return constants that can be returned from Handler::handle
|
||||
* to message the handler walker.
|
||||
*/
|
||||
const LAST_HANDLER = 0x10;
|
||||
const QUIT = 0x20;
|
||||
const DONE = 0x10; // returning this is optional, only exists for
|
||||
// semantic purposes
|
||||
const LAST_HANDLER = 0x20;
|
||||
const QUIT = 0x30;
|
||||
|
||||
/**
|
||||
* @var Damnit\Run
|
||||
|
||||
@@ -19,6 +19,11 @@ class JsonResponseHandler extends Handler
|
||||
*/
|
||||
private $returnFrames = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $onlyForAjaxRequests = false;
|
||||
|
||||
/**
|
||||
* @param bool|null $returnFrames
|
||||
* @return null|bool
|
||||
@@ -32,11 +37,40 @@ class JsonResponseHandler extends Handler
|
||||
$this->returnFrames = (bool) $returnFrames;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool|null $onlyForAjaxRequests
|
||||
* @return null|bool
|
||||
*/
|
||||
public function onlyForAjaxRequests($onlyForAjaxRequests = null)
|
||||
{
|
||||
if(func_num_args() == 0) {
|
||||
return $this->onlyForAjaxRequests;
|
||||
}
|
||||
|
||||
$this->onlyForAjaxRequests = (bool) $onlyForAjaxRequests;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check, if possible, that this execution was triggered by an AJAX request.
|
||||
* @param bool
|
||||
*/
|
||||
private function isAjaxRequest()
|
||||
{
|
||||
return (
|
||||
!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
|
||||
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
if($this->onlyForAjaxRequests() && !$this->isAjaxRequest()) {
|
||||
return Handler::DONE;
|
||||
}
|
||||
|
||||
$exception = $this->getException();
|
||||
|
||||
$response = array(
|
||||
|
||||
@@ -33,7 +33,7 @@ class PrettyPageHandler extends Handler
|
||||
// Check conditions for outputting HTML:
|
||||
// @todo: make this more robust
|
||||
if(php_sapi_name() === 'cli') {
|
||||
return;
|
||||
return Handler::DONE;
|
||||
}
|
||||
|
||||
// Get the 'pretty-template.php' template file
|
||||
|
||||
Reference in New Issue
Block a user