mirror of
https://github.com/filp/whoops.git
synced 2026-09-07 16:26:24 +00:00
Merge remote-tracking branch 'staabm/onlyajax'
This commit is contained in:
@@ -21,6 +21,9 @@
|
||||
"psr-4": {
|
||||
"Whoops\\": "src/Whoops/"
|
||||
}
|
||||
"files": [
|
||||
"src/Whoops/functions.php"
|
||||
]
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
|
||||
@@ -12,6 +12,10 @@
|
||||
- [`Whoops\Handler\JsonResponseHandler`](#handler-json) - Formats errors and exceptions as a JSON payload
|
||||
- [`Whoops\Handler\PrettyPageHandler`](#handler-pretty) - Outputs a detailed, fancy error page
|
||||
|
||||
### Core Functions:
|
||||
- [`Whoops\isAjaxRequest()`](#fn-ajax) - Determines whether the current request was triggered by XMLHttpRequest
|
||||
|
||||
|
||||
# Core Classes:
|
||||
|
||||
## <a name="whoops-run"></a> `Whoops\Run`
|
||||
@@ -239,6 +243,7 @@ Frame::getComments(string $filter = null)
|
||||
#=> array
|
||||
```
|
||||
|
||||
|
||||
# Core Handlers
|
||||
|
||||
## <a name="handler-callback"></a> `Whoops\Handler\CallbackHandler`
|
||||
@@ -307,11 +312,6 @@ The `JSON` body has the following format:
|
||||
JsonResponseHandler::addTraceToOutput(bool $yes = null)
|
||||
#=> bool
|
||||
|
||||
// Should output only be sent if the current request is an
|
||||
// AJAX request?
|
||||
JsonResponseHandler::onlyForAjaxRequests(bool $yes = null)
|
||||
#=> bool
|
||||
|
||||
JsonResponseHandler::handle()
|
||||
#=> int | null
|
||||
```
|
||||
@@ -402,3 +402,16 @@ PrettyPageHandler::addEditor(string $editor, $resolver)
|
||||
PrettyPageHandler::handle()
|
||||
#=> int | null
|
||||
```
|
||||
|
||||
|
||||
# Core Functions:
|
||||
|
||||
## <a name="fn-ajax"></a> `Whoops\isAjaxRequest()`
|
||||
#=> boolean
|
||||
|
||||
```php
|
||||
// Use a certain handler only in AJAX triggered requests
|
||||
if (Whoops\isAjaxRequest()){
|
||||
$run->addHandler($myHandler)
|
||||
}
|
||||
```
|
||||
@@ -20,11 +20,6 @@ class JsonResponseHandler extends Handler
|
||||
*/
|
||||
private $returnFrames = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $onlyForAjaxRequests = false;
|
||||
|
||||
/**
|
||||
* @param bool|null $returnFrames
|
||||
* @return bool|$this
|
||||
@@ -39,40 +34,11 @@ class JsonResponseHandler extends Handler
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*
|
||||
* @return 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;
|
||||
}
|
||||
|
||||
$response = array(
|
||||
'error' => Formatter::formatExceptionAsDataArray(
|
||||
$this->getInspector(),
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: staabm
|
||||
* Date: 21.11.2015
|
||||
* Time: 16:08
|
||||
*/
|
||||
|
||||
namespace Whoops;
|
||||
|
||||
function isAjaxRequest()
|
||||
{
|
||||
return (
|
||||
!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
|
||||
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
|
||||
}
|
||||
Reference in New Issue
Block a user