Refactor the response type guessing in OutputFilter into a method
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 8 Oct 2011 04:55:19 +0000 (00:55 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 8 Oct 2011 04:55:19 +0000 (00:55 -0400)
http/output_filter.php

index 45aba4831c22e6acca0b084499dca7c16c7a582e..98460b1726f93a5214fa5b3f2258e9a8a2cd9395 100644 (file)
@@ -96,31 +96,7 @@ class OutputFilter
   protected function _CreateBodyForResponse(Request $request,
                                             Response $response)
   {
-    $type = NULL;
-
-    // See if the HTTP request contains the desired output format.
-    if (isset($request->data['format'])) {
-      if ($request->data['format'] == 'xml')
-        $type = 'xml';
-      else if ($request->data['format'] == 'json')
-        $type = 'json';
-    }
-
-    // If the request didn't specify a type, try and figure it out using
-    // heuristics.
-
-    // If this was from an XHR, assume JSON.
-    if (!$type && isset($request->data['_SERVER']['HTTP_X_REQUESTED_WITH']))
-      $type = 'json';
-
-    // Check if an Action specified an overriding response type.
-    if (isset($response->context[self::RESPONSE_TYPE]))
-      $type = $response->context[self::RESPONSE_TYPE];
-
-    // If no type has been determined, just assume HTML.
-    if (!$type)
-      $type = 'html';
-
+    $type = $this->_GetResponseType($request, $response);
     if ($type == 'json') {
       $response->headers['Content-Type'] = 'application/json';
       $response->body = json_encode($response->data, JSON_NUMERIC_CHECK);
@@ -136,6 +112,34 @@ class OutputFilter
     }
   }
 
+  /*!
+    Determines based on the Request what format the response should be in.
+  */
+  protected function _GetResponseType(Request $request, Response $response)
+  {
+    // Check if an Action specified an overriding response type.
+    if (isset($response->context[self::RESPONSE_TYPE]))
+      return $response->context[self::RESPONSE_TYPE];
+
+    // See if the HTTP request contains the desired output format.
+    if (isset($request->data['format'])) {
+      if ($request->data['format'] == 'xml')
+        return 'xml';
+      else if ($request->data['format'] == 'json')
+        return 'json';
+    }
+
+    // If the request didn't specify a type, try and figure it out using
+    // heuristics.
+
+    // If this was from an XHR, assume JSON.
+    if (isset($request->data['_SERVER']['HTTP_X_REQUESTED_WITH']))
+      return 'json';
+
+    // If no type has been determined, just assume HTML.
+    return 'html';
+  }
+
   /*!
     Creates an XML tree from an array. Equivalent to json_encode.
   */