]>
src.bluestatic.org Git - hoplite.git/blob - root_controller.php
43d16dad24b80ade384bc9e4b291ca6efa6a886a
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
. '/base/weak_interface.php';
20 require_once HOPLITE_ROOT
. '/http/request.php';
21 require_once HOPLITE_ROOT
. '/http/response.php';
22 require_once HOPLITE_ROOT
. '/http/response_code.php';
25 The RootController is meant to be invoked from the index.php of the
31 private $request = NULL;
34 private $response = NULL;
37 private $url_map = NULL;
39 /*! @var OutputFilter */
40 private $output_filter = NULL;
42 /*! @var WeakInterface<RootControllerDelegate> */
43 private $delegate = NULL;
46 Creates the controller with the request context information, typicallhy
47 from the global scope ($GLOBALS), but can be injected for testing.
48 @param UrlMap The routing map
49 @param OutputFilter The object responsible for decorating output.
50 @param array& PHP globals array
52 public function __construct(array $globals)
54 $this->response
= new Response();
55 $this->request
= new Request();
56 $this->request
->data
= array(
57 '_GET' => &$globals['_GET'],
58 '_POST' => &$globals['_POST'],
59 '_COOKIE' => &$globals['_COOKIE'],
60 '_SERVER' => &$globals['_SERVER']
62 $this->delegate
= new \hoplite\base\
WeakInterface('hoplite\http\RootControllerDelegate');
66 public function request() { return $this
->request
; }
67 public function response() { return $this
->response
; }
69 /*! Sets the UrlMap. */
70 public function set_urL_map(UrlMap
$url_map) { $this
->url_map
= $url_map
; }
72 /*! Sest the Output Filter. */
73 public function set_output_filter(OutputFilter
$output_filter)
75 $this->output_filter
= $output_filter;
78 /*! Sets the delegate. */
79 public function set_delegate($delegate)
81 $this->delegate
->Bind($delegate);
85 Createst the Request and Response that are used throughout the duration of
90 // The query rewriter module of the webserver rewrites a request from:
91 // http://example.com/webapp/user/view/42
93 // http://example.com/webapp/index.php/user/view/42
94 // ... which then becomes accessible from PATH_INFO.
95 $url = $this->request
->data
['_SERVER']['PATH_INFO'];
97 $url = substr($url, 1);
99 // Set the final pieces of the request.
100 $this->request
->url
= $url;
101 $this->request
->http_method
= $this->request
->data
['_SERVER']['REQUEST_METHOD'];
103 $this->delegate
->OnInitialRequest($this->request
);
105 // Dispatch the request to an Action.
106 $this->RouteRequest($this->request
);
108 // When control returns here, all actions have been invoked and it's time
109 // to start the output filter and exit.
114 Prevents any other Actions from executing. This starts the OutputFilter and
117 public function Stop()
119 $this->delegate
->WillStop($this->request
, $this->response
);
120 $this->output_filter
->FilterOutput($this->request
, $this->response
);
125 Wrapper around PHP exit().
127 protected function _Exit()
133 Invoked by Run() and can be invoked by others to evaluate and perform the
134 lookup in the UrlMap. This then calls InvokeAction().
135 @param Request The Request whose URL will be routed
137 public function RouteRequest(Request
$request)
139 $this->delegate
->WillRouteRequest($request, $this->response
);
141 $url_map_value = $this->url_map
->Evaluate($request);
145 $action = $this->url_map
->LookupAction($url_map_value);
148 $this->response
->response_code
= ResponseCode
::NOT_FOUND
;
153 $this->InvokeAction($action);
157 Used to run an Action and drive it through its states.
160 public function InvokeAction(Action
$action)
162 $this->delegate
->WillInvokeAction($action, $this->request
, $this->response
);
164 $action->FilterRequest($this->request
, $this->response
);
165 $action->Invoke($this->request
, $this->response
);
166 $action->FilterResponse($this->request
, $this->response
);
168 $this->delegate
->DidInvokeAction($action, $this->request
, $this->response
);
172 Performs a reverse-lookup in the UrlMap for the pattern/fragment for the
173 name of a given Action class.
174 @param string Class name.
176 public function LookupAction($class)
178 $map = $this->url_map
->map();
179 foreach ($map as $pattern => $action) {
180 if ($action == $class)
187 Delegate for the root controller. The controller uses WeakInterface to call
188 these methods, so they're all optional.
190 interface RootControllerDelegate
192 public function OnInitialRequest(Request
$request);
194 public function WillRouteRequest(Request
$request, Response
$response);
196 public function WillInvokeAction(Action
$action, Request
$request, Response
$response);
198 public function DidInvokeAction(Action
$action, Request
$request, Response
$response);
200 public function WillStop(Request
$request, Response
$response);