basePath = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['SCRIPT_NAME']); } // ################################################################### /** * Listens for routing requests and handles them appropriately */ public function route() { $request = str_replace($this->basePath, '', $_SERVER['REQUEST_URI']); $params = explode('/', $request); $this->request = $params[0]; 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 = ''; } } } if (isset($this->map[ $this->request ])) { call_user_func_array($this->map[ $this->request ][self::MAP_ACTION], $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(array($this, '_loadFile'), 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); } // ################################################################### /** * 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$ || ################################################################### \*=====================================================================*/ ?>