Add ability to trigger JsonResponseHandler for AJAX only

This commit is contained in:
filp
2013-03-16 16:34:14 +00:00
parent 1117328b36
commit 456b300052
3 changed files with 39 additions and 3 deletions
+4 -2
View File
@@ -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(
+1 -1
View File
@@ -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