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');
29 require_once('ISSO/RouterController.php');
34 * Run dispatch() in a file with a defined routing pattern and send all
35 * requests to that file, this will then load and appropriatley run
36 * the right source files. You can use mod_rewrite like this:
39 * RewriteCond %{REQUEST_URI} !static/(.*)$
40 * RewriteRule ^(.*)$ index.php
42 * This will prevent the rewriting on any file in the static/ directory
45 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
56 * Routing map of request:(controller,:method,args...)
59 private $map = array();
68 * Map action for errors
71 private $errorAction = ':undefined:';
74 * Router controller requested
80 * Router action requsted
89 private $params = array();
91 // ###################################################################
95 public function __construct()
97 $this->basePath
= str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['SCRIPT_NAME']);
100 // ###################################################################
102 * Listens for routing requests and handles them appropriately
104 public function dispatch()
106 $request = str_replace($this->basePath
, '', $_SERVER['REQUEST_URI']);
108 $params = explode('/', $request);
109 $request = explode('.', $params[0]);
110 $this->request
= $request[0];
111 $this->action
= ((!isset($request[1]) OR empty($request[1])) ?
'Index' : $request[1]);
114 if (sizeof($params) == 0)
118 else if (sizeof($params) == 1)
120 $this->params
[':id'] = $params[1];
125 for ($i = 1; $i <= sizeof($params); $i++
)
127 if ($i == 1 AND $params["$i"][0] != '+')
129 $this->params
[':id'] = $params["$i"];
133 if ($params["$i"] == '')
138 if ($params["$i"][0] == '+')
142 $this->_error(sprintf(_('The paramter "%1$s" does not have a value'), $key));
144 $key = substr($params["$i"], 1);
150 $this->_error(sprintf(_('The value "%1$s" does not have a paramatized key'), $params["$i"]));
152 $this->params
["$key"] = $params["$i"];
158 if (isset($this->map
[ $this->request
]))
160 call_user_func_array(array($this, $this->map
[ $this->request
][self
::MAP_LOADER
]), $this->map
[ $this->request
][self
::MAP_PARAMS
]);
164 $this->_error(sprintf(_('Routing request "%1$s" not found in map'), $this->request
));
168 // ###################################################################
170 * Sets the error handler of the router; when an error occurs while
171 * routing, this method is called
173 * @param string Error handler name
175 public function setErrorAction($name)
177 $this->errorAction
= $name;
180 // ###################################################################
182 * Adds a directory of files to the maps, with the file names (without .php)
183 * as the map request name
185 * @param string Directory path
187 public function addFileDirectory($path)
189 $path = BSFunctions
::FetchSourcePath($path);
190 $controllers = BSFunctions
::ScanDirectory($path);
192 foreach ($controllers AS $file)
194 $controller = str_replace('.php', '', $file);
195 $this->addFileRequest($controller, $path . $file);
199 // ###################################################################
201 * Routes a request to the loading of a file; this is the most common
202 * routing request and it performs no extra functions
204 * @param string Request name
205 * @param string File name
207 public function addFileRequest($request, $file)
209 $this->map
["$request"] = array(self
::MAP_LOADER
=> '_loadFile', self
::MAP_PARAMS
=> array($file));
212 // ###################################################################
214 * Adds a list of files to the mapping; the controller will become
215 * the file name without the .php extension
217 * @param string Search path
219 public function addControllerDirectory($path)
221 $path = BSFunctions
::FetchSourcePath($path);
222 $controllers = BSFunctions
::ScanDirectory($path);
224 foreach ($controllers AS $file)
226 $controller = str_replace('.php', '', $file);
227 $this->addController($controller, $path . $controller);
231 // ###################################################################
233 * Routes a request to the controller system
235 * @param string Request name
236 * @param string Controller name; the file must be named <X>.php and define a class <X>Controller.php
238 public function addController($request, $file)
240 $this->map
["$request"] = array(self
::MAP_LOADER
=> '_invokeController', self
::MAP_PARAMS
=> array($file));
243 // ###################################################################
245 * Includes a given file name
247 * @param string File name
249 private function _loadFile($filename)
251 if (!file_exists($filename))
253 $this->_error(sprintf(_('Cannot find the file "%1$s"'), $filename));
258 // ###################################################################
260 * Loads and then invokes a controller
262 * @param string File name of the controller
264 public function _invokeController($controller)
266 $this->_loadFile($controller);
268 $controllerName = basename($controller, '.php');
269 $className = "{$controllerName}Controller";
271 if (!class_exists($className))
273 trigger_error('The controller "' . $className . '" does not exist and therfore cannot have actions forwarded to it');
276 if (!is_subclass_of($className, 'BSRouterController'))
278 trigger_error('Cannot invoke controller-style actions on a non-controller: requested controller is not of the type BSRouterController');
281 // these go out the door so BSInput will process them and merge them into BSInput->in[]
282 $_GET = $this->params
;
283 $request = new $className(BSRegister
::LoadModule('Input'));
285 if (!method_exists($className, $this->action
))
287 trigger_error('Invalid action called on controller: ' . $className . ' does not respond to ' . $this->action
. '()');
290 $request->{$this->action
}();
293 // ###################################################################
295 * Sends an error to the error action handler
297 * @param string Error message
299 private function _error($errMsg)
301 if (function_exists($this->errorAction
) OR (is_array($this->errorAction
) AND method_exists($this->errorAction
[0], $this->errorAction
[1])))
303 $this->errorAction($errMsg);
307 trigger_error($errMsg);
313 /*=====================================================================*
314 || ###################################################################
317 || ###################################################################
318 \*=====================================================================*/