Only check the Request#data[format] key if it is set
[hoplite.git] / http / output_filter.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\http;
18
19 require_once HOPLITE_ROOT . '/http/response_code.php';
20
21 /*!
22 The OutputFilter is executed after all Actions have been processed. The
23 primary function is to generate the actual HTTP response body from the model
24 data contained within the http\Response. Depending on how the Request was
25 sent, this
26 */
27 class OutputFilter
28 {
29 /*! @var RootController */
30 private $controller;
31
32 /*! @const The key in Response#context that indicates which type of output to
33 produce, regardless of the request type.
34 */
35 const RESPONSE_TYPE = 'response_type';
36
37 /*!
38 Constructor that takes a reference to the RootController.
39 */
40 public function __construct(RootController $controller)
41 {
42 $this->controller = $controller;
43 }
44
45 /*! Accessor for the RootController. */
46 public function controller() { return $this->controller; }
47
48 /*! @brief Main entry point for output filtering
49 This is called from the RootController to begin processing the output and
50 generating the response.
51 */
52 public function FilterOutput(Request $request, Response $response)
53 {
54 // If there was an error during the processing of an action, allow hooking
55 // custom logic.
56 if ($response->response_code != ResponseCode::OK)
57 if (!$this->_ContinueHandlingResponseForCode($request, $response))
58 return;
59
60 // If there's already raw data for the body, just output that.
61 if ($response->body)
62 return;
63
64 // Otherwise, construct the body based on how the Request was received and
65 // any other information in the response.
66 $this->_CreateBodyForResponse($request, $response);
67
68 // Now just output the response.
69 header("Status: {$response->response_code}", true, $response->response_code);
70 foreach ($response->headers as $header => $value)
71 header("$header: $value");
72 print $response->body;
73 }
74
75 /*!
76 If the request did not generate an 200 response code, the filter gives the
77 client an opportunity to override the normal output control flow and perform
78 some other task. If you want the control flow to continue executing as
79 normal, return TRUE; otherwise, return FALSE to exit from ::FilterOutput().
80 @return boolean
81 */
82 protected function _ContinueHandlingResponseForCode(Request $request,
83 Response $response)
84 {
85 return TRUE;
86 }
87
88 /*!
89 Fills out the Response#data field. This could be an evaluated HTML template,
90 a JSON payload, XML, or any other type of response for the client.
91 */
92 protected function _CreateBodyForResponse(Request $request,
93 Response $response)
94 {
95 $type = NULL;
96
97 // See if the HTTP request contains the desired output format.
98 if (isset($request->data['format'])) {
99 if ($request->data['format'] == 'xml')
100 $type = 'xml';
101 else if ($request->data['format'] == 'json')
102 $type = 'json';
103 }
104
105 // If the request didn't specify a type, try and figure it out using
106 // heuristics.
107
108 // If this was from an XHR, assume JSON.
109 if (!$type && isset($request->data['_SERVER']['X_REQUESTED_WITH']))
110 $type = 'json';
111
112 // Check if an Action specified an overriding response type.
113 if (isset($response->context[self::RESPONSE_TYPE]))
114 $type = $response->context[self::RESPONSE_TYPE];
115
116 // If no type has been determined, just assume HTML.
117 if (!$type)
118 $type = 'html';
119
120 if ($type == 'json') {
121 $response->headers['Content-Type'] = 'application/json';
122 $response->body = json_encode($response->data);
123 } else if ($type == 'xml') {
124 $response->headers['Content-Type'] = 'application/xml';
125 $response->body = $this->_EncodeXML($response->data);
126 } else if ($type == 'html') {
127 $response->headers['Content-Type'] = 'text/html';
128 }
129 }
130
131 /*!
132 Creates an XML tree from an array. Equivalent to json_encode.
133 */
134 protected function _EncodeXML($data)
135 {
136 $response = new \SimpleXMLElement('<response/>');
137
138 $writer = function($elm, $parent) use (&$writer) {
139 foreach ($elm as $key => $value) {
140 if (is_scalar($value)) {
141 $parent->AddChild($key, $value);
142 } else {
143 $new_parent = $parent->AddChild($key);
144 $writer($value, $new_parent);
145 }
146 }
147 };
148
149 $writer($data, $response);
150 return $response->AsXML();
151 }
152 }