basePath = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['SCRIPT_NAME']); } // ################################################################### /** * Listens for routing requests and handles them appropriately */ public function dispatch() { if ((defined('ISSO_ROUTER_NO_REWRITE') AND constant('ISSO_ROUTER_NO_REWRITE')) OR isset($_REQUEST['controller'])) { $this->request = (isset($_REQUEST['controller']) ? $_REQUEST['controller'] : ''); $this->action = (isset($_REQUEST['action']) ? $_REQUEST['action'] : ''); $this->params = $_GET; } else { $request = str_replace($this->basePath, '', $_SERVER['REQUEST_URI']); $params = explode('/', $request); $request = explode('.', $params[0]); $this->request = $request[0]; $this->action = (isset($request[1]) ? $request[1] : ''); unset($params[0]); if (sizeof($params) == 0) { // do nothing } else if (sizeof($params) == 1) { $this->params[':id'] = $params[1]; } else { $key = ''; for ($i = 1; $i <= sizeof($params); $i++) { if ($i == 1 AND $params["$i"][0] != '+') { $this->params[':id'] = $params["$i"]; continue; } if ($params["$i"] == '') { continue; } if ($params["$i"][0] == '+') { if (!empty($key)) { $this->_error(sprintf(_('The paramter "%1$s" does not have a value'), $key)); } $key = substr($params["$i"], 1); } else { if (empty($key)) { $this->_error(sprintf(_('The value "%1$s" does not have a paramatized key'), $params["$i"])); } $this->params["$key"] = $params["$i"]; $key = ''; } } } } $this->request = (empty($this->request) ? 'Index' : $this->request); $this->action = (empty($this->action) ? 'Index' : $this->action); if (isset($this->map[ $this->request ])) { call_user_func_array(array($this, $this->map[ $this->request ][self::MAP_LOADER]), $this->map[ $this->request ][self::MAP_PARAMS]); } else { $this->_error(sprintf(_('Routing request "%1$s" not found in map'), $this->request)); } } // ################################################################### /** * Sets the error handler of the router; when an error occurs while * routing, this method is called * * @param string Error handler name */ public function setErrorAction($name) { $this->errorAction = $name; } // ################################################################### /** * Adds a directory of files to the maps, with the file names (without .php) * as the map request name * * @param string Directory path */ public function addFileDirectory($path) { $path = BSFunctions::FetchSourcePath($path); $controllers = BSFunctions::ScanDirectory($path); foreach ($controllers AS $file) { $controller = str_replace('.php', '', $file); $this->addFileRequest($controller, $path . $file); } } // ################################################################### /** * Routes a request to the loading of a file; this is the most common * routing request and it performs no extra functions * * @param string Request name * @param string File name */ public function addFileRequest($request, $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)); } // ################################################################### /** * Includes a given file name * * @param string File name */ private function _loadFile($filename) { if (!file_exists($filename)) { $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 * * @param string Error message */ private function _error($errMsg) { if (function_exists($this->errorAction) OR (is_array($this->errorAction) AND method_exists($this->errorAction[0], $this->errorAction[1]))) { $this->errorAction($errMsg); } else { trigger_error($errMsg); } exit; } } /*=====================================================================* || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>