Merge remote-tracking branch 'staabm/onlyajax'

This commit is contained in:
Denis Sokolov
2015-11-23 21:32:31 +02:00
4 changed files with 37 additions and 39 deletions
+3
View File
@@ -21,6 +21,9 @@
"psr-4": {
"Whoops\\": "src/Whoops/"
}
"files": [
"src/Whoops/functions.php"
]
},
"extra": {
"branch-alias": {
+18 -5
View File
@@ -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(),
+16
View File
@@ -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');
}