The default behavior for RestAction should be to error METHOD_NOT_ALLOWED.
[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('get', 'post', 'delete', 'put');
34 $method = strtolower($request->http_method);
35 if (!in_array($method, $valid_methods)) {
36 $response->response_code = ResponseCode::METHOD_NOT_ALLOWED;
37 $this->controller()->Stop();
38 return;
39 }
40
41 $invoke = 'Do' . ucwords($method);
42 $this->$invoke($request, $response);
43 }
44
45 /*! Methods for each of the different HTTP methods. */
46 public function DoGet(Request $request, Response $response)
47 {
48 $this->controller()->StopWithCode(ResponseCode::METHOD_NOT_ALLOWED);
49 }
50
51 public function DoPost(Request $request, Response $response)
52 {
53 $this->controller()->StopWithCode(ResponseCode::METHOD_NOT_ALLOWED);
54 }
55
56 public function DoDelete(Request $request, Response $response)
57 {
58 $this->controller()->StopWithCode(ResponseCode::METHOD_NOT_ALLOWED);
59 }
60
61 public function DoPut(Request $request, Response $response)
62 {
63 $this->controller()->StopWithCode(ResponseCode::METHOD_NOT_ALLOWED);
64 }
65 }