Add http\RestAdapter
[hoplite.git] / http / rest_adapter.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_controller.php';
20 require_once HOPLITE_ROOT . '/http/response_code.php';
21 require_once HOPLITE_ROOT . '/http/rest_action.php';
22
23 /*!
24 This abstract class adapts a RESTful interface into a web frontend that only
25 supports GET and POST. The |action| parameter will control what is performed.
26 */
27 abstract class RestAdapter extends ActionController
28 {
29 /*! @var RestAction The RESTful interface which will be adapted. */
30 protected $action = NULL;
31
32 public function FilterRequest(Request $request, Response $response)
33 {
34 $this->action = $this->_GetRestAction();
35 }
36
37 /*! Gets the RestAction that will be adapted. */
38 protected abstract function _GetRestAction();
39
40 public function ActionFetch(Request $request, Response $response)
41 {
42 if ($request->http_method != 'GET' || $request->http_method != 'POST') {
43 $response->response_code = ResponseCode::METHOD_NOT_ALLOWED;
44 return;
45 }
46 $this->action->DoGet($request, $response);
47 }
48
49 public function ActionInsert(Request $request, Response $response)
50 {
51 if ($request->http_method != 'POST') {
52 $response->response_code = ResponseCode::METHOD_NOT_ALLOWED;
53 return;
54 }
55 $this->action->DoPut($request, $response);
56 }
57
58 public function ActionUpdate(Request $request, Response $response)
59 {
60 if ($request->http_method != 'POST') {
61 $response->response_code = ResponseCode::METHOD_NOT_ALLOWED;
62 return;
63 }
64 $this->action->DoPost($request, $response);
65 }
66
67 public function ActionDelete(Request $request, Response $response)
68 {
69 if ($request->http_method != 'POST') {
70 $response->response_code = ResponseCode::METHOD_NOT_ALLOWED;
71 return;
72 }
73 $this->action->DoDelete($request, $response);
74 }
75 }