From 743055890cf88495c9b3e17f148fd7839a59cbeb Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 11 Jun 2011 14:37:37 -0400 Subject: [PATCH] * Add ActionController * Split out ResponseCode into its own file --- http/action_controller.php | 55 ++++++++++++++++++++++++++++++++ http/request.php | 2 +- http/response.php | 48 +--------------------------- http/response_code.php | 64 ++++++++++++++++++++++++++++++++++++++ http/rest_action.php | 5 ++- 5 files changed, 125 insertions(+), 49 deletions(-) create mode 100644 http/action_controller.php create mode 100644 http/response_code.php diff --git a/http/action_controller.php b/http/action_controller.php new file mode 100644 index 0000000..3da7039 --- /dev/null +++ b/http/action_controller.php @@ -0,0 +1,55 @@ +. + +namespace hoplite\http; + +require_once HOPLITE_ROOT . '/http/action.php'; +require_once HOPLITE_ROOT . '/http/response_code.php'; + +/*! + An ActionController is an Action that operates like a typical MVC Controller. + It will look at the Request's data for a key named 'action', which should + correspond to a method named 'Action$action'. +*/ +class ActionController extends Action +{ + /*! + Forwards the request/response pair to an internal method based on a key in + the Request object. + */ + public function Invoke(Request $request, Response $response) + { + $method = $this->_GetActionMethod($request); + if (!method_exists($this, $method)) { + $response->response_code = ResponseCode::NOT_FOUND; + $this->controller()->Stop(); + return; + } + + $this->$method($request, $response); + } + + /*! + Returns the method name to invoke based on the Request. + @return string|NULL + */ + protected function _GetActionMethod(Request $request) + { + if (!isset($request->data['action'])) + return NULL; + return 'Action' . $request->data['action']; + } +} diff --git a/http/request.php b/http/request.php index aaa7ac5..7f0590e 100644 --- a/http/request.php +++ b/http/request.php @@ -31,5 +31,5 @@ class Request extends \hoplite\base\StrictObject public $url = ''; /*! @var array HTTP request data. */ - public $data = NULL; + public $data = array(); } diff --git a/http/response.php b/http/response.php index ebebf13..86f2d02 100644 --- a/http/response.php +++ b/http/response.php @@ -17,6 +17,7 @@ namespace hoplite\http; require_once HOPLITE_ROOT . 'base/strict_object.php'; +require_once HOPLITE_ROOT . 'http/response_code.php'; /*! A Response holds data processed by Action objects. When the RootController is @@ -43,50 +44,3 @@ class Response extends \hoplite\base\StrictObject /*! @var array Model data. */ public $data = array(); } - -/*! - An enumeration of all the HTTP status codes as constants. This is the complete - list of codes. Not all will be usable by an application. - @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html -*/ -class ResponseCode -{ - const 100 = CONTINUE; - const 101 = SWITCHING_PROTOCOLS; - const 200 = OK; - const 201 = CREATED; - const 202 = ACCEPTED; - const 204 = NO_CONTENT; - const 205 = RESET_CONTENT; - const 206 = PARTIAL_CONTENT; - const 300 = MULTIPLE_CHOICES; - const 301 = MOVED_PERMANENTLY; - const 302 = FOUND; - const 303 = SEE_OTHER; - const 304 = NOT_MODIFIED; - const 305 = USE_PROXY; - const 307 = TEMPORARY_REDIRECT; - const 400 = BAD_REQUEST; - const 401 = UNAUTHORIZED; - const 402 = PAYMENT_REQUIRED; - const 403 = FORBIDDEN; - const 404 = NOT_FOUND; - const 405 = METHOD_NOT_ALLOWED; - const 406 = NOT_ACCEPTABLE; - const 407 = PROXY_AUTHENTICATION_REQUIRED; - const 408 = REQUEST_TIMEOUT; - const 409 = CONFLICT; - const 410 = GONE; - const 411 = LENGTH_REQUIRED; - const 412 = PRECONDITION_FAILED; - const 413 = REQUEST_ENTITY_TOO_LARGE; - const 415 = UNSUPPORTED_MEDIA_TYPE; - const 416 = REQUESTED_RANGE_NOT_SATISFIABLE; - const 417 = EXPECTATION_FAILED; - const 500 = INTERNAL_SERVER_ERROR; - const 501 = NOT_IMPLEMENTED; - const 502 = BAD_GATEWAY; - const 503 = SERVICE_UNAVAILABLE; - const 504 = GATEWAY_TIMEOUT; - const 505 = HTTP_VERSION_NOT_SUPPORTED; -} diff --git a/http/response_code.php b/http/response_code.php new file mode 100644 index 0000000..ab8cd54 --- /dev/null +++ b/http/response_code.php @@ -0,0 +1,64 @@ +. + +namespace hoplite\http; + +/*! + An enumeration of all the HTTP status codes as constants. This is the complete + list of codes. Not all will be usable by an application. + @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html +*/ +class ResponseCode +{ + const CONTINUE = 100; + const SWITCHING_PROTOCOLS = 101; + const OK = 200; + const CREATED = 201; + const ACCEPTED = 202; + const NO_CONTENT = 204; + const RESET_CONTENT = 205; + const PARTIAL_CONTENT = 206; + const MULTIPLE_CHOICES = 300; + const MOVED_PERMANENTLY = 301; + const FOUND = 302; + const SEE_OTHER = 303; + const NOT_MODIFIED = 304; + const USE_PROXY = 305; + const TEMPORARY_REDIRECT = 307; + const BAD_REQUEST = 400; + const UNAUTHORIZED = 401; + const PAYMENT_REQUIRED = 402; + const FORBIDDEN = 403; + const NOT_FOUND = 404; + const METHOD_NOT_ALLOWED = 405; + const NOT_ACCEPTABLE = 406; + const PROXY_AUTHENTICATION_REQUIRED = 407; + const REQUEST_TIMEOUT = 408; + const CONFLICT = 409; + const GONE = 410; + const LENGTH_REQUIRED = 411; + const PRECONDITION_FAILED = 412; + const REQUEST_ENTITY_TOO_LARGE = 413; + const UNSUPPORTED_MEDIA_TYPE = 415; + const REQUESTED_RANGE_NOT_SATISFIABLE = 416; + const EXPECTATION_FAILED = 417; + const INTERNAL_SERVER_ERROR = 500; + const NOT_IMPLEMENTED = 501; + const BAD_GATEWAY = 502; + const SERVICE_UNAVAILABLE = 503; + const GATEWAY_TIMEOUT = 504; + const HTTP_VERSION_NOT_SUPPORTED = 505; +} diff --git a/http/rest_action.php b/http/rest_action.php index 1d6c20d..9300d4c 100644 --- a/http/rest_action.php +++ b/http/rest_action.php @@ -16,6 +16,9 @@ namespace hoplite\http; +require_once HOPLITE_ROOT . '/http/action.php'; +require_once HOPLITE_ROOT . '/http/response_code.php'; + /*! This abstract class is the base for handling all requests and filling out response objects. @@ -30,7 +33,7 @@ class RestAction extends Action $valid_methods = array('get', 'post', 'delete', 'put'); $method = strtolower($request->http_method); if (!in_array($method, $valid_methods)) { - $response->http_code = 405 /* METHOD_NOT_ALLOWED */; + $response->http_code = ResponseCode::METHOD_NOT_ALLOWED; $this->controller()->Stop(); return; } -- 2.22.5