From df0cc9ef54e6480bb4c3f52f06eda12d586c5692 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 11 Jun 2011 02:31:01 -0400 Subject: [PATCH] Start stubbing out the design doc interfaces --- http/action.php | 54 +++++++++++++++++++++++++++++++ http/rest_action.php | 47 +++++++++++++++++++++++++++ http/root_controller.php | 69 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 http/action.php create mode 100644 http/rest_action.php create mode 100644 http/root_controller.php diff --git a/http/action.php b/http/action.php new file mode 100644 index 0000000..1e73a2e --- /dev/null +++ b/http/action.php @@ -0,0 +1,54 @@ +. + +namespace hoplite\http; + +/*! + This abstract class is the base for handling all requests and filling out + response objects. +*/ +abstract class Action +{ + /*! \var RootController */ + private $controller; + + /*! + Creates a new action with a reference to the RootController. + @param RootController + */ + public function __construct($controller) + { + $this->controller = $controller; + } + + /*! Accesses the RootController */ + public function controller() { return $this->controller; } + + /*! + Called before the Action is Invoked(). + */ + public function FilterRequest(Request $request, Response $response) {} + + /*! + Performs the action and fills out the response's data model. + */ + public function Invoke(Request $request, Response $response); + + /*! + Called after this has been Invoked(). + */ + public function FilterResponse(Request $request, Response $response) {} +} diff --git a/http/rest_action.php b/http/rest_action.php new file mode 100644 index 0000000..1d6c20d --- /dev/null +++ b/http/rest_action.php @@ -0,0 +1,47 @@ +. + +namespace hoplite\http; + +/*! + This abstract class is the base for handling all requests and filling out + response objects. +*/ +class RestAction extends Action +{ + /*! + Performs the action and fills out the response's data model. + */ + public function Invoke(Request $request, Response $response) + { + $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 */; + $this->controller()->Stop(); + return; + } + + $invoke = 'Do' . ucwords($method); + $this->$invoke(); + } + + /*! Methods for each of the different HTTP methods. */ + protected function _DoGet(Request $request, Response $response) {} + protected function _DoPost(Request $request, Response $response) {} + protected function _DoDelete(Request $request, Response $response) {} + protected function _DoPut(Request $request, Response $response) {} +} diff --git a/http/root_controller.php b/http/root_controller.php new file mode 100644 index 0000000..f63cda9 --- /dev/null +++ b/http/root_controller.php @@ -0,0 +1,69 @@ +. + +namespace hoplite\http; + +/*! + The RootController is meant to be invoked from the index.php of the + application. +*/ +class RootController +{ + /*! + Creates the controller with the request context information, typicallhy + from the global scope ($GLOBALS), but can be injected for testing. + @param globals + */ + public function __construct($globals) + {} + + /*! + Createst the Request and Response that are used throughout the duration of + the execution. + */ + public function Run() + {} + + /*! + Prevents any other Actions from executing. This starts the OutputFilter and + then exits. + */ + public function Stop() + {} + + /*! + Invoked by Run() and can be invoked by others to evaluate and perform the + lookup in the UrlMap. This then calls InvokeAction(). + @param string The URL fragment to look up in the + */ + public function RouteRequest($url_fragment) + {} + + /*! + Used to run an Action and drive it through its states. + @param Action + */ + public function InvokeAction(Action $action) + {} + + /*! + Performs a reverse-lookup in the UrlMap for the pattern/fragment for the + name of a given Action class. + @param string Class name. + */ + public function LookupAction($class) + {} +} -- 2.22.5