From 7030672f53588aafa90c5b0018ab0a4edf2e1ff3 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Thu, 4 Aug 2011 20:07:29 -0400 Subject: [PATCH] Add http\RestAdapter --- http/rest_adapter.php | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 http/rest_adapter.php diff --git a/http/rest_adapter.php b/http/rest_adapter.php new file mode 100644 index 0000000..2522b52 --- /dev/null +++ b/http/rest_adapter.php @@ -0,0 +1,75 @@ +. + +namespace hoplite\http; + +require_once HOPLITE_ROOT . '/http/action_controller.php'; +require_once HOPLITE_ROOT . '/http/response_code.php'; +require_once HOPLITE_ROOT . '/http/rest_action.php'; + +/*! + This abstract class adapts a RESTful interface into a web frontend that only + supports GET and POST. The |action| parameter will control what is performed. +*/ +abstract class RestAdapter extends ActionController +{ + /*! @var RestAction The RESTful interface which will be adapted. */ + protected $action = NULL; + + public function FilterRequest(Request $request, Response $response) + { + $this->action = $this->_GetRestAction(); + } + + /*! Gets the RestAction that will be adapted. */ + protected abstract function _GetRestAction(); + + public function ActionFetch(Request $request, Response $response) + { + if ($request->http_method != 'GET' || $request->http_method != 'POST') { + $response->response_code = ResponseCode::METHOD_NOT_ALLOWED; + return; + } + $this->action->DoGet($request, $response); + } + + public function ActionInsert(Request $request, Response $response) + { + if ($request->http_method != 'POST') { + $response->response_code = ResponseCode::METHOD_NOT_ALLOWED; + return; + } + $this->action->DoPut($request, $response); + } + + public function ActionUpdate(Request $request, Response $response) + { + if ($request->http_method != 'POST') { + $response->response_code = ResponseCode::METHOD_NOT_ALLOWED; + return; + } + $this->action->DoPost($request, $response); + } + + public function ActionDelete(Request $request, Response $response) + { + if ($request->http_method != 'POST') { + $response->response_code = ResponseCode::METHOD_NOT_ALLOWED; + return; + } + $this->action->DoDelete($request, $response); + } +} -- 2.43.5