Skip to content
This repository
Browse code

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...
commit d4b8db27afddc49f5ff37b25ed13490f6ec5c608 1 parent bbfa54c
Ingo Schommer authored July 16, 2012

Showing 1 changed file with 27 additions and 0 deletions. Show diff stats Hide diff stats

  1. 27  admin/code/LeftAndMain.php
27  admin/code/LeftAndMain.php
@@ -358,6 +358,17 @@ function redirect($url, $code=302) {
358 358
 			if($this->request->getHeader('X-Pjax') && !$this->response->getHeader('X-Pjax')) {
359 359
 				$this->response->addHeader('X-Pjax', $this->request->getHeader('X-Pjax'));
360 360
 			}
  361
+			$oldResponse = $this->response;
  362
+			$newResponse = new LeftAndMain_HTTPResponse(
  363
+				$oldResponse->getBody(), 
  364
+				$oldResponse->getStatusCode(),
  365
+				$oldResponse->getStatusDescription()
  366
+			);
  367
+			foreach($oldResponse->getHeaders() as $k => $v) {
  368
+				$newResponse->addHeader($k, $v);
  369
+			}
  370
+			$newResponse->setIsFinished(true);
  371
+			$this->response = $newResponse;
361 372
 			return ''; // Actual response will be re-requested by client
362 373
 		} else {
363 374
 			parent::redirect($url, $code);
@@ -1471,3 +1482,19 @@ function mark($node) {
1471 1482
 	}
1472 1483
 }
1473 1484
 
  1485
+/**
  1486
+ * Allow overriding finished state for faux redirects.
  1487
+ */
  1488
+class LeftAndMain_HTTPResponse extends SS_HTTPResponse {
  1489
+
  1490
+	protected $isFinished = false;
  1491
+
  1492
+	function isFinished() {
  1493
+		return (parent::isFinished() || $this->isFinished);
  1494
+	}
  1495
+
  1496
+	function setIsFinished($bool) {
  1497
+		$this->isFinished = $bool;
  1498
+	}
  1499
+
  1500
+}

0 notes on commit d4b8db2

Please sign in to comment.
Something went wrong with that request. Please try again.