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 */
33 Constructor that takes a reference to the RootController.
35 public function __construct(RootController
$controller)
37 $this->controller
= $controller;
40 /*! Accessor for the RootController. */
41 public function controller() { return $this->controller
; }
43 /*! @brief Main entry point for output filtering
44 This is called from the RootController to begin processing the output and
45 generating the response.
47 public function FilterOutput(Request
$request, Response
$response)
49 // If there was an error during the processing of an action, allow hooking
51 if ($response->response_code
!= ResponseCode
::OK
)
52 if (!$this->_ContinueHandlingResponseForCode($request, $response))
55 // If there's already raw data for the body, just output that.
59 // Otherwise, construct the body based on how the Request was received and
60 // any other information in the response.
61 $this->_CreateBodyForResponse($request, $response);
65 If the request did not generate an 200 response code, the filter gives the
66 client an opportunity to override the normal output control flow and perform
67 some other task. If you want the control flow to continue executing as
68 normal, return TRUE; otherwise, return FALSE to exit from ::FilterOutput().
71 protected function _ContinueHandlingResponseForCode(Request
$request,
78 Fills out the Response#data field. This could be an evaluated HTML template,
79 a JSON payload, XML, or any other type of response for the client.
81 protected function _CreateBodyForResponse(Request
$request,
86 Creates an XML tree from an array. Equivalent to json_encode.
88 protected function _EncodeXML($data)
90 $response = new \
SimpleXMLElement('<response/>');
92 $writer = function($elm, $parent) use (&$writer) {
93 foreach ($elm as $key => $value) {
94 if (is_scalar($value)) {
95 $parent->AddChild($key, $value);
97 $new_parent = $parent->AddChild($key);
98 $writer($value, $new_parent);
103 $writer($data, $response);
104 return $response->AsXML();