Skip to content
This repository
Browse code

ENHANCEMENT Allowing multiple fragments to be returned by PjaxRespons…

…eNegotiator through JSON
  • Loading branch information...
commit 473eda43cb24a66964d932a4b29376326b5b5587 1 parent cbf4ad3
Ingo Schommer authored April 18, 2012
31  control/PjaxResponseNegotiator.php
@@ -44,19 +44,36 @@ public function respond(SS_HTTPRequest $request, $extraCallbacks = array()) {
44 44
 			array_change_key_case($this->callbacks, CASE_LOWER),
45 45
 			array_change_key_case($extraCallbacks, CASE_LOWER)
46 46
 		);
  47
+
  48
+		$response = new SS_HTTPResponse();
47 49
 		
48  
-		if($fragment = $request->getHeader('X-Pjax')) {
49  
-			$fragment = strtolower($fragment);
50  
-			if(isset($callbacks[$fragment])) {
51  
-				return call_user_func($callbacks[$fragment]);
  50
+		$responseParts = array();
  51
+		if($fragmentStr = $request->getHeader('X-Pjax')) {
  52
+			$fragments = explode(',', strtolower($fragmentStr));
  53
+			foreach($fragments as $fragment) {
  54
+				if(isset($callbacks[$fragment])) {
  55
+					$responseParts[$fragment] = call_user_func($callbacks[$fragment]);
  56
+				} else {
  57
+					throw new SS_HTTPResponse_Exception("X-Pjax = '$fragment' not supported for this URL.", 400);
  58
+				}
  59
+			}
  60
+			if(count($responseParts) == 1) {
  61
+				$response->setBody(array_pop($responseParts));
52 62
 			} else {
53  
-				throw new SS_HTTPResponse_Exception("X-Pjax = '$fragment' not supported for this URL.", 400);
  63
+				if($request->getHeader('Accept') != 'text/json') {
  64
+					throw new SS_HTTPResponse_Exception(
  65
+						'Multiple comma-separated fragments can only be returne with an "Accept: text/json" header',
  66
+						400
  67
+					);
  68
+				}
  69
+				$response->setBody(Convert::raw2json($responseParts));
  70
+				$response->addHeader('Content-Type', 'text/json');
54 71
 			}
55 72
 		} else {
56 73
 			if($request->isAjax()) throw new SS_HTTPResponse_Exception("Ajax requests to this URL require an X-Pjax header.", 400);
57  
-			return call_user_func($callbacks['default']);
  74
+			$response->setBody(call_user_func($callbacks['default']));
58 75
 		}
59  
-		
  76
+		return $response;
60 77
 	}
61 78
 
62 79
 	/**
16  tests/control/PjaxResponseNegotiatorTest.php
@@ -19,4 +19,20 @@ function testSelectsFragmentByHeader() {
19 19
 		$this->assertEquals('myfragment response', $negotiator->respond($request));
20 20
 	}
21 21
 
  22
+	function testMultipleFragments() {
  23
+		$negotiator = new PjaxResponseNegotiator(array(
  24
+			'default' => function() {return 'default response';},
  25
+			'myfragment' => function() {return 'myfragment response';},
  26
+			'otherfragment' => function() {return 'otherfragment response';},
  27
+		));
  28
+		$request = new SS_HTTPRequest('GET', '/');
  29
+		$request->addHeader('X-Pjax', 'myfragment,otherfragment');
  30
+		$request->addHeader('Accept', 'text/json');
  31
+		$json = json_decode($negotiator->respond($request));
  32
+		$this->assertObjectHasAttribute('myfragment', $json);
  33
+		$this->assertEquals('myfragment response', $json->myfragment);
  34
+		$this->assertObjectHasAttribute('otherfragment', $json);
  35
+		$this->assertEquals('otherfragment response', $json->otherfragment);
  36
+	}
  37
+
22 38
 }

0 notes on commit 473eda4

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