* Add ActionController
[hoplite.git] / http / action_controller.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/action.php';
20 require_once HOPLITE_ROOT . '/http/response_code.php';
21
22 /*!
23 An ActionController is an Action that operates like a typical MVC Controller.
24 It will look at the Request's data for a key named 'action', which should
25 correspond to a method named 'Action$action'.
26 */
27 class ActionController extends Action
28 {
29 /*!
30 Forwards the request/response pair to an internal method based on a key in
31 the Request object.
32 */
33 public function Invoke(Request $request, Response $response)
34 {
35 $method = $this->_GetActionMethod($request);
36 if (!method_exists($this, $method)) {
37 $response->response_code = ResponseCode::NOT_FOUND;
38 $this->controller()->Stop();
39 return;
40 }
41
42 $this->$method($request, $response);
43 }
44
45 /*!
46 Returns the method name to invoke based on the Request.
47 @return string|NULL
48 */
49 protected function _GetActionMethod(Request $request)
50 {
51 if (!isset($request->data['action']))
52 return NULL;
53 return 'Action' . $request->data['action'];
54 }
55 }