]>
src.bluestatic.org Git - hoplite.git/blob - http/output_filter.php
3 // Copyright (c) 2011 Blue Static
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.
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
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/>.
17 namespace hoplite\http
;
19 require_once HOPLITE_ROOT
. '/http/response_code.php';
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
29 /*! @var RootController */
32 /*! @const The key in Response#context that indicates which type of output to
33 produce, regardless of the request type.
35 const RESPONSE_TYPE
= 'response_type';
38 Constructor that takes a reference to the RootController.
40 public function __construct(RootController
$controller)
42 $this->controller
= $controller;
45 /*! Accessor for the RootController. */
46 public function controller() { return $this
->controller
; }
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.
52 public function FilterOutput(Request
$request, Response
$response)
54 // If there was an error during the processing of an action, allow hooking
56 if ($response->response_code
!= ResponseCode
::OK
)
57 if (!$this->_ContinueHandlingResponseForCode($request, $response))
60 // If there's already raw data for the body, just output that.
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);
68 // Now just output the response.
69 foreach ($response->headers
as $header => $value)
70 header("$header: $value");
71 print $response->body;
75 If the request did not generate an 200 response code, the filter gives the
76 client an opportunity to override the normal output control flow and perform
77 some other task. If you want the control flow to continue executing as
78 normal, return TRUE; otherwise, return FALSE to exit from ::FilterOutput().
81 protected function _ContinueHandlingResponseForCode(Request $request,
88 Fills out the Response#data field. This could be an evaluated HTML template,
89 a JSON payload, XML, or any other type of response for the client.
91 protected function _CreateBodyForResponse(Request $request,
96 // See if the HTTP request contains the desired output format.
97 if ($request->data['format'] == 'xml')
99 else if ($request->data['format'] == 'json')
102 // If the request didn't specify a type, try and figure it out using
105 // If this was from an XHR, assume JSON.
106 if (!$type && isset($request->data['_SERVER']['X_REQUESTED_WITH']))
109 // Check if an Action specified an overriding response type.
110 if (isset($response->context[self::RESPONSE_TYPE]))
111 $type = $response->context[self::RESPONSE_TYPE];
113 // If no type has been determined, just assume HTML.
117 if ($type == 'json') {
118 $response->headers['Content-Type'] = 'application/json';
119 $response->body = json_encode($response->data);
120 } else if ($type == 'xml') {
121 $response->headers['Content-Type'] = 'application/xml';
122 $response->body = $this->_EncodeXML($response->data);
123 } else if ($type == 'html') {
124 $response->headers['Content-Type'] = 'text/html';
129 Creates an XML tree from an array. Equivalent to json_encode.
131 protected function _EncodeXML($data)
133 $response = new \SimpleXMLElement('<response/>');
135 $writer = function($elm, $parent) use (&$writer) {
136 foreach ($elm as $key => $value) {
137 if (is_scalar($value)) {
138 $parent->AddChild($key, $value);
140 $new_parent = $parent->AddChild($key);
141 $writer($value, $new_parent);
146 $writer($data, $response);
147 return $response->AsXML();