2012-03-24 03:19:02 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Handle the X-Pjax header that AJAX responses may provide, returning the
|
|
|
|
* fragment, or, in the case of non-AJAX form submissions, redirecting back to the submitter.
|
|
|
|
*
|
|
|
|
* X-Pjax ensures that users won't end up seeing the unstyled form HTML in their browser
|
|
|
|
* If a JS error prevents the Ajax overriding of form submissions from happening.
|
|
|
|
* It also provides better non-JS operation.
|
|
|
|
*
|
|
|
|
* Caution: This API is volatile, and might eventually be replaced by a generic
|
|
|
|
* action helper system for controllers.
|
|
|
|
*/
|
|
|
|
class PjaxResponseNegotiator {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Array See {@link respond()}
|
|
|
|
*/
|
|
|
|
protected $callbacks = array(
|
|
|
|
// TODO Using deprecated functionality, but don't want to duplicate Controller->redirectBack()
|
|
|
|
'default' => array('Director', 'redirectBack'),
|
|
|
|
);
|
|
|
|
|
2012-06-13 00:27:03 +02:00
|
|
|
protected $response = null;
|
|
|
|
|
2012-03-24 03:19:02 +01:00
|
|
|
/**
|
|
|
|
* @param RequestHandler $controller
|
2012-06-13 00:27:03 +02:00
|
|
|
* @param SS_HTTPResponse An existing response to reuse (optional)
|
2012-03-24 03:19:02 +01:00
|
|
|
* @param Array $callbacks
|
|
|
|
*/
|
2012-06-13 00:27:03 +02:00
|
|
|
function __construct($callbacks = array(), $response = null) {
|
2012-03-24 03:19:02 +01:00
|
|
|
$this->callbacks = $callbacks;
|
2012-06-13 00:27:03 +02:00
|
|
|
$this->response = $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getResponse() {
|
|
|
|
if(!$this->response) $this->response = new SS_HTTPResponse();
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setResponse($response) {
|
|
|
|
$this->response = $response;
|
2012-03-24 03:19:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Out of the box, the handler "CurrentForm" value, which will return the rendered form.
|
|
|
|
* Non-Ajax calls will redirect back.
|
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @param array $extraCallbacks List of anonymous functions or callables returning either a string
|
|
|
|
* or SS_HTTPResponse, keyed by their fragment identifier. The 'default' key can
|
|
|
|
* be used as a fallback for non-ajax responses.
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
|
|
|
public function respond(SS_HTTPRequest $request, $extraCallbacks = array()) {
|
|
|
|
// Prepare the default options and combine with the others
|
2012-05-30 11:53:54 +02:00
|
|
|
$callbacks = array_merge($this->callbacks, $extraCallbacks);
|
2012-06-13 00:27:03 +02:00
|
|
|
$response = $this->getResponse();
|
2012-03-24 03:19:02 +01:00
|
|
|
|
2012-04-18 21:15:31 +02:00
|
|
|
$responseParts = array();
|
|
|
|
if($fragmentStr = $request->getHeader('X-Pjax')) {
|
2012-05-30 11:53:54 +02:00
|
|
|
$fragments = explode(',', $fragmentStr);
|
2012-04-18 21:15:31 +02:00
|
|
|
foreach($fragments as $fragment) {
|
|
|
|
if(isset($callbacks[$fragment])) {
|
|
|
|
$responseParts[$fragment] = call_user_func($callbacks[$fragment]);
|
|
|
|
} else {
|
|
|
|
throw new SS_HTTPResponse_Exception("X-Pjax = '$fragment' not supported for this URL.", 400);
|
|
|
|
}
|
|
|
|
}
|
2012-05-30 14:58:24 +02:00
|
|
|
$response->setBody(Convert::raw2json($responseParts));
|
|
|
|
$response->addHeader('Content-Type', 'text/json');
|
2012-03-24 03:19:02 +01:00
|
|
|
} else {
|
|
|
|
if($request->isAjax()) throw new SS_HTTPResponse_Exception("Ajax requests to this URL require an X-Pjax header.", 400);
|
2012-04-18 21:15:31 +02:00
|
|
|
$response->setBody(call_user_func($callbacks['default']));
|
2012-03-24 03:19:02 +01:00
|
|
|
}
|
2012-04-18 21:15:31 +02:00
|
|
|
return $response;
|
2012-03-24 03:19:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $fragment
|
|
|
|
* @param Callable $callback
|
|
|
|
*/
|
|
|
|
public function setCallback($fragment, $callback) {
|
|
|
|
$this->callbacks[$fragment] = $callback;
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|