From 96c34adbf288fb9fcee99481d75c5484feb3d592 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 19 Dec 2006 03:07:41 +0000 Subject: [PATCH] Added the controller system in BSRouter --- Router.php | 94 +++++++++++++++++++++++++++++++++++++++----- RouterController.php | 68 ++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 10 deletions(-) create mode 100644 RouterController.php diff --git a/Router.php b/Router.php index e501b12..eee38fc 100644 --- a/Router.php +++ b/Router.php @@ -26,6 +26,7 @@ */ require_once('ISSO/Functions.php'); +require_once('ISSO/RouterController.php'); /** * Router @@ -42,11 +43,11 @@ require_once('ISSO/Functions.php'); */ class BSRouter { - const MAP_ACTION = 0; - const MAP_PARAMS = 1; + const MAP_LOADER = 1; + const MAP_PARAMS = 2; /** - * Routing map of request:(method,args...) + * Routing map of request:(controller,:method,args...) * @var array */ private $map = array(); @@ -64,10 +65,16 @@ class BSRouter private $errorAction = ':undefined:'; /** - * Router action requested + * Router controller requested * @var string */ - private $request; + private $controller; + + /** + * Router action requsted + * @var string + */ + private $action; /** * Request paramaters @@ -92,8 +99,10 @@ class BSRouter { $request = str_replace($this->basePath, '', $_SERVER['REQUEST_URI']); - $params = explode('/', $request); - $this->request = $params[0]; + $params = explode('/', $request); + $request = explode('.', $params[0]); + $this->request = $request[0]; + $this->action = ((!isset($request[1]) OR empty($request[1])) ? 'Index' : $request[1]); unset($params[0]); if (sizeof($params) == 0) @@ -142,7 +151,7 @@ class BSRouter if (isset($this->map[ $this->request ])) { - call_user_func_array($this->map[ $this->request ][self::MAP_ACTION], $this->map[ $this->request ][self::MAP_PARAMS]); + call_user_func_array(array($this, $this->map[ $this->request ][self::MAP_LOADER]), $this->map[ $this->request ][self::MAP_PARAMS]); } else { @@ -191,7 +200,38 @@ class BSRouter */ public function addFileRequest($request, $file) { - $this->map["$request"] = array(array($this, '_loadFile'), array($file)); + $this->map["$request"] = array(self::MAP_LOADER => '_loadFile', self::MAP_PARAMS => array($file)); + } + + // ################################################################### + /** + * Adds a list of files to the mapping; the controller will become + * the file name without the .php extension + * + * @param string Search path + */ + public function addControllerDirectory($path) + { + $path = BSFunctions::FetchSourcePath($path); + $controllers = BSFunctions::ScanDirectory($path); + + foreach ($controllers AS $file) + { + $controller = str_replace('.php', '', $file); + $this->addController($controller, $path . $controller); + } + } + + // ################################################################### + /** + * Routes a request to the controller system + * + * @param string Request name + * @param string Controller name; the file must be named .php and define a class Controller.php + */ + public function addController($request, $file) + { + $this->map["$request"] = array(self::MAP_LOADER => '_invokeController', self::MAP_PARAMS => array($file)); } // ################################################################### @@ -206,10 +246,44 @@ class BSRouter { $this->_error(sprintf(_('Cannot find the file "%1$s"'), $filename)); } - include($filename); } + // ################################################################### + /** + * Loads and then invokes a controller + * + * @param string File name of the controller + */ + public function _invokeController($controller) + { + $this->_loadFile($controller); + + $controllerName = basename($controller, '.php'); + $className = "{$controllerName}Controller"; + + if (!class_exists($className)) + { + trigger_error('The controller "' . $className . '" does not exist and therfore cannot have actions forwarded to it'); + } + + if (!is_subclass_of($className, 'BSRouterController')) + { + trigger_error('Cannot invoke controller-style actions on a non-controller: requested controller is not of the type BSRouterController'); + } + + // these go out the door so BSInput will process them and merge them into BSInput->in[] + $_GET = $this->params; + $request = new $className(BSRegister::LoadModule('Input')); + + if (!method_exists($className, $this->action)) + { + trigger_error('Invalid action called on controller: ' . $className . ' does not respond to ' . $this->action . '()'); + } + + $request->{$this->action}(); + } + // ################################################################### /** * Sends an error to the error action handler diff --git a/RouterController.php b/RouterController.php new file mode 100644 index 0000000..8c439a0 --- /dev/null +++ b/RouterController.php @@ -0,0 +1,68 @@ +params = $params; + } + + /** + * The default action (index) + */ + public abstract function Index(); +} + +/*=====================================================================* +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file -- 2.43.5