]>
src.bluestatic.org Git - hoplite.git/blob - root_controller.php
ca09e42905fb94058ebb75dea4cbd7fa24733232
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/request.php';
20 require_once HOPLITE_ROOT
. '/http/response.php';
21 require_once HOPLITE_ROOT
. '/http/response_code.php';
24 The RootController is meant to be invoked from the index.php of the
30 private $request = NULL;
33 private $response = NULL;
36 private $url_map = NULL;
38 /*! @var OutputFilter */
39 private $output_filter = NULL;
42 Creates the controller with the request context information, typicallhy
43 from the global scope ($GLOBALS), but can be injected for testing.
44 @param UrlMap The routing map
45 @param OutputFilter The object responsible for decorating output.
46 @param array& PHP globals array
48 public function __construct(array $globals)
50 $this->response
= new Response();
51 $this->request
= new Request();
52 $this->request
->data
= array(
53 '_GET' => &$globals['_GET'],
54 '_POST' => &$globals['_POST'],
55 '_COOKIE' => &$globals['_COOKIE'],
56 '_SERVER' => &$globals['_SERVER']
61 public function request() { return $this
->request
; }
62 public function response() { return $this
->response
; }
64 /*! Sets the UrlMap. */
65 public function set_urL_map(UrlMap
$url_map) { $this
->url_map
= $url_map
; }
67 /*! Sest the Output Filter. */
68 public function set_output_filter(OutputFilter
$output_filter)
70 $this->output_filter
= $output_filter;
74 Createst the Request and Response that are used throughout the duration of
79 // The query rewriter module of the webserver rewrites a request from:
80 // http://example.com/webapp/user/view/42
82 // http://example.com/webapp/index.php/user/view/42
83 // ... which then becomes accessible from PATH_INFO.
84 $url = $this->request
->data
['_SERVER']['PATH_INFO'];
86 $url = substr($url, 1);
88 // Set the final pieces of the request.
89 $this->request
->url
= $url;
90 $this->request
->http_method
= $this->request
->data
['_SERVER']['REQUEST_METHOD'];
92 // Dispatch the request to an Action.
93 $this->RouteRequest($this->request
);
95 // When control returns here, all actions have been invoked and it's time
96 // to start the output filter and exit.
101 Prevents any other Actions from executing. This starts the OutputFilter and
104 public function Stop()
106 $this->output_filter
->FilterOutput($this->request
, $this->response
);
111 Wrapper around PHP exit().
113 protected function _Exit()
119 Invoked by Run() and can be invoked by others to evaluate and perform the
120 lookup in the UrlMap. This then calls InvokeAction().
121 @param Request The Request whose URL will be routed
123 public function RouteRequest(Request
$request)
125 $url_map_value = $this->url_map
->Evaluate($request);
129 $action = $this->url_map
->LookupAction($url_map_value);
132 $this->response
->response_code
= ResponseCode
::NOT_FOUND
;
137 $this->InvokeAction($action);
141 Used to run an Action and drive it through its states.
144 public function InvokeAction(Action
$action)
146 $action->FilterRequest($this->request
, $this->response
);
147 $action->Invoke($this->request
, $this->response
);
148 $action->FilterResponse($this->request
, $this->response
);
152 Performs a reverse-lookup in the UrlMap for the pattern/fragment for the
153 name of a given Action class.
154 @param string Class name.
156 public function LookupAction($class)
158 $map = $this->url_map
->map();
159 foreach ($map as $pattern => $action) {
160 if ($action == $class)