Permalink
Browse files

Marking fake LeftAndMain->redirect() responses as finished

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...
1 parent bbfa54c commit d4b8db27afddc49f5ff37b25ed13490f6ec5c608 @chillu chillu committed Jul 16, 2012
Showing with 27 additions and 0 deletions.
  1. +27 −0 admin/code/LeftAndMain.php
@@ -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);
@@ -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.