2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
23 * Request Router (Router.php)
28 require_once('ISSO/Functions.php');
33 * Run route() in a file with a defined routing pattern and send all
34 * requests to that file, this will then load and appropriatley run
35 * the right source files
38 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
49 * Routing map of request:(method,args...)
52 private $map = array();
61 * Map action for errors
64 private $errorAction = ':undefined:';
67 * Router action requested
76 private $params = array();
78 // ###################################################################
82 public function __construct()
84 $this->basePath
= str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['SCRIPT_NAME']);
87 // ###################################################################
89 * Listens for routing requests and handles them appropriately
91 public function route()
93 $request = str_replace($this->basePath
, '', $_SERVER['REQUEST_URI']);
95 $params = explode('/', $request);
96 $this->request
= $params[0];
99 if (sizeof($params) == 0)
103 else if (sizeof($params) == 1)
105 $this->params
[':id'] = $params[1];
110 for ($i = 1; $i <= sizeof($params); $i++
)
112 if ($i == 1 AND $params["$i"][0] != '+')
114 $this->params[':id'] = $params["$i"];
118 if ($params["$i"] != '' AND $params["$i"][0] == '+')
122 $this->_error(sprintf(_('The paramter "%1$s" does not have a value'), $key));
124 $key = substr($params["$i"], 1);
130 $this->_error(sprintf(_('The value "%
1$s" does not have a paramatized key
'), $params["$i"]));
132 $this->params["$key"] = $params["$i"];
138 if (isset($this->map[ $this->request ]))
140 call_user_func_array($this->map[ $this->request ][self::MAP_ACTION], $this->map[ $this->request ][self::MAP_PARAMS]);
144 $this->_error(sprintf(_('Routing request
"%1$s" not found in map'), $this->request));
148 // ###################################################################
150 * Sets the error handler of the router; when an error occurs while
151 * routing, this method is called
153 * @param string Error handler name
155 public function setErrorAction($name)
157 $this->errorAction = $name;
160 // ###################################################################
162 * Adds a directory of files to the maps, with the file names (without .php)
163 * as the map request name
165 * @param string Directory path
167 public function addFileDirectory($path)
169 $path = BSFunctions::FetchSourcePath($path);
170 $controllers = BSFunctions::ScanDirectory($path);
171 $controllers = $controllers[''];
173 foreach ($controllers AS $file)
175 $controller = str_replace('.php', '', $file);
176 $this->addFileRequest($controller, $path . $file);
180 // ###################################################################
182 * Routes a request to the loading of a file; this is the most common
183 * routing request and it performs no extra functions
185 * @param string Request name
186 * @param string File name
188 public function addFileRequest($request, $file)
190 $this->map["$request"] = array(array($this, '_loadFile'), array($file));
193 // ###################################################################
195 * Includes a given file name
197 * @param string File name
199 private function _loadFile($filename)
201 if (!file_exists($filename))
203 $this->_error(sprintf(_('Cannot find the file "%1$s"'), $filename));
209 // ###################################################################
211 * Sends an error to the error action handler
213 * @param string Error message
215 private function _error($errMsg)
217 if (function_exists($this->errorAction) OR (is_array($this->errorAction) AND method_exists($this->errorAction[0], $this->errorAction[1])))
219 $this->errorAction($errMsg);
223 trigger_error($errMsg);
229 /*=====================================================================*
230 || ###################################################################
233 || ###################################################################
234 \*=====================================================================*/