From 945729c52cca34a07bc8e06309421cf9345cf7ce Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 6 Aug 2011 16:31:33 -0400 Subject: [PATCH] Add RootControllerDelegate --- http/root_controller.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/http/root_controller.php b/http/root_controller.php index ca09e42..43d16da 100644 --- a/http/root_controller.php +++ b/http/root_controller.php @@ -16,6 +16,7 @@ namespace hoplite\http; +require_once HOPLITE_ROOT . '/base/weak_interface.php'; require_once HOPLITE_ROOT . '/http/request.php'; require_once HOPLITE_ROOT . '/http/response.php'; require_once HOPLITE_ROOT . '/http/response_code.php'; @@ -38,6 +39,9 @@ class RootController /*! @var OutputFilter */ private $output_filter = NULL; + /*! @var WeakInterface */ + private $delegate = NULL; + /*! Creates the controller with the request context information, typicallhy from the global scope ($GLOBALS), but can be injected for testing. @@ -55,6 +59,7 @@ class RootController '_COOKIE' => &$globals['_COOKIE'], '_SERVER' => &$globals['_SERVER'] ); + $this->delegate = new \hoplite\base\WeakInterface('hoplite\http\RootControllerDelegate'); } /*! Accessors */ @@ -70,6 +75,12 @@ class RootController $this->output_filter = $output_filter; } + /*! Sets the delegate. */ + public function set_delegate($delegate) + { + $this->delegate->Bind($delegate); + } + /*! Createst the Request and Response that are used throughout the duration of the execution. @@ -89,6 +100,8 @@ class RootController $this->request->url = $url; $this->request->http_method = $this->request->data['_SERVER']['REQUEST_METHOD']; + $this->delegate->OnInitialRequest($this->request); + // Dispatch the request to an Action. $this->RouteRequest($this->request); @@ -103,6 +116,7 @@ class RootController */ public function Stop() { + $this->delegate->WillStop($this->request, $this->response); $this->output_filter->FilterOutput($this->request, $this->response); $this->_Exit(); } @@ -122,6 +136,8 @@ class RootController */ public function RouteRequest(Request $request) { + $this->delegate->WillRouteRequest($request, $this->response); + $url_map_value = $this->url_map->Evaluate($request); $action = NULL; @@ -143,9 +159,13 @@ class RootController */ public function InvokeAction(Action $action) { + $this->delegate->WillInvokeAction($action, $this->request, $this->response); + $action->FilterRequest($this->request, $this->response); $action->Invoke($this->request, $this->response); $action->FilterResponse($this->request, $this->response); + + $this->delegate->DidInvokeAction($action, $this->request, $this->response); } /*! @@ -162,3 +182,20 @@ class RootController } } } + +/*! + Delegate for the root controller. The controller uses WeakInterface to call + these methods, so they're all optional. +*/ +interface RootControllerDelegate +{ + public function OnInitialRequest(Request $request); + + public function WillRouteRequest(Request $request, Response $response); + + public function WillInvokeAction(Action $action, Request $request, Response $response); + + public function DidInvokeAction(Action $action, Request $request, Response $response); + + public function WillStop(Request $request, Response $response); +} -- 2.43.5