From 6fed92afb57570c678f0425b9e417834b965eee4 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 8 Oct 2011 00:55:19 -0400 Subject: [PATCH] Refactor the response type guessing in OutputFilter into a method --- http/output_filter.php | 54 +++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/http/output_filter.php b/http/output_filter.php index 45aba48..98460b1 100644 --- a/http/output_filter.php +++ b/http/output_filter.php @@ -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. */ -- 2.22.5