Skip to content

Commit

Permalink
Marking fake LeftAndMain->redirect() responses as finished
Browse files Browse the repository at this point in the history
Introduce new LeftAndMain_HTTPResponse class for this purpose,
to mark a response as finished regardless of HTTP status.
This is required for ajax responses which do redirects on app layer
rather than HTTP (to avoid double processing).

Specifically required to decorate LeftAndMain->init()
in the 'translatable' module (TranslatableCMSMainExtension),
which marks the response as finished through its redirect,
avoiding further processing after init().
  • Loading branch information
chillu committed Jul 16, 2012
1 parent bbfa54c commit d4b8db2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions admin/code/LeftAndMain.php
Expand Up @@ -358,6 +358,17 @@ function redirect($url, $code=302) {
if($this->request->getHeader('X-Pjax') && !$this->response->getHeader('X-Pjax')) {
$this->response->addHeader('X-Pjax', $this->request->getHeader('X-Pjax'));
}
$oldResponse = $this->response;
$newResponse = new LeftAndMain_HTTPResponse(
$oldResponse->getBody(),
$oldResponse->getStatusCode(),
$oldResponse->getStatusDescription()
);
foreach($oldResponse->getHeaders() as $k => $v) {
$newResponse->addHeader($k, $v);
}
$newResponse->setIsFinished(true);
$this->response = $newResponse;
return ''; // Actual response will be re-requested by client
} else {
parent::redirect($url, $code);
Expand Down Expand Up @@ -1471,3 +1482,19 @@ function mark($node) {
}
}

/**
* Allow overriding finished state for faux redirects.
*/
class LeftAndMain_HTTPResponse extends SS_HTTPResponse {

protected $isFinished = false;

function isFinished() {
return (parent::isFinished() || $this->isFinished);
}

function setIsFinished($bool) {
$this->isFinished = $bool;
}

}

0 comments on commit d4b8db2

Please sign in to comment.