RestAction: Support OPTIONS and fix old StopWithCode.
[hoplite.git] / http / rest_action.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 This abstract class is the base for handling all requests and filling out
24 response objects.
25 */
26 class RestAction extends Action
27 {
28 /*!
29 Performs the action and fills out the response's data model.
30 */
31 public function Invoke(Request $request, Response $response)
32 {
33 $valid_methods = array('options', 'get', 'post', 'delete', 'put');
34 $method = strtolower($request->http_method);
35 if (!in_array($method, $valid_methods)) {
36 $this->controller()->SendResponseCode(ResponseCode::METHOD_NOT_ALLOWED);
37 return;
38 }
39
40 $invoke = 'Do' . ucwords($method);
41 $this->$invoke($request, $response);
42 }
43
44 /*! Methods for each of the different HTTP methods. */
45
46
47 public function DoOptions(Request $request, Response $response) {}
48
49 public function DoGet(Request $request, Response $response)
50 {
51 $this->controller()->SendReseponseCode(ResponseCode::METHOD_NOT_ALLOWED);
52 }
53
54 public function DoPost(Request $request, Response $response)
55 {
56 $this->controller()->SendReseponseCode(ResponseCode::METHOD_NOT_ALLOWED);
57 }
58
59 public function DoDelete(Request $request, Response $response)
60 {
61 $this->controller()->SendReseponseCode(ResponseCode::METHOD_NOT_ALLOWED);
62 }
63
64 public function DoPut(Request $request, Response $response)
65 {
66 $this->controller()->SendReseponseCode(ResponseCode::METHOD_NOT_ALLOWED);
67 }
68 }