2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework
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.
43 * If your server cannot do this rewriting, then simply define this constant:
44 * ISSO_ROUTER_NO_REWRITE as TRUE and it will switch to standard URLs
47 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
58 * Routing map of request:(controller,:method,args...)
61 private $map = array();
70 * Map action for errors
73 private $errorAction = ':undefined:';
76 * Router controller requested
82 * Router action requsted
91 private $params = array();
93 // ###################################################################
97 public function __construct()
99 $this->basePath
= str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['SCRIPT_NAME']);
102 // ###################################################################
104 * Listens for routing requests and handles them appropriately
106 public function dispatch()
108 if ((defined('ISSO_ROUTER_NO_REWRITE') AND constant('ISSO_ROUTER_NO_REWRITE')) OR isset($_REQUEST['controller']))
110 $this->request
= (isset($_REQUEST['controller']) ? $_REQUEST['controller'] : '');
111 $this->action
= (isset($_REQUEST['action']) ? $_REQUEST['action'] : '');
112 $this->params
= $_GET;
116 $request = str_replace($this->basePath
, '', $_SERVER['REQUEST_URI']);
118 $params = explode('/', $request);
119 $request = explode('.', $params[0]);
120 $this->request
= $request[0];
121 $this->action
= (isset($request[1]) ? $request[1] : '');
124 if (sizeof($params) == 0)
128 else if (sizeof($params) == 1)
130 $this->params
[':id'] = $params[1];
135 for ($i = 1; $i <= sizeof($params); $i++
)
137 if ($i == 1 AND $params["$i"][0] != '+')
139 $this->params[':id'] = $params["$i"];
143 if ($params["$i"] == '')
148 if ($params["$i"][0] == '+')
152 $this->_error(sprintf(_('The paramter "%1$s" does not have a value'), $key));
154 $key = substr($params["$i"], 1);
160 $this->_error(sprintf(_('The value "%
1$s" does not have a paramatized key
'), $params["$i"]));
162 $this->params["$key"] = $params["$i"];
169 $this->request = (empty($this->request) ? 'Index
' : $this->request);
170 $this->action = (empty($this->action) ? 'Index
' : $this->action);
172 if (isset($this->map[ $this->request ]))
174 call_user_func_array(array($this, $this->map[ $this->request ][self::MAP_LOADER]), $this->map[ $this->request ][self::MAP_PARAMS]);
178 $this->_error(sprintf(_('Routing request
"%1$s" not found in map'), $this->request));
182 // ###################################################################
184 * Sets the error handler of the router; when an error occurs while
185 * routing, this method is called
187 * @param string Error handler name
189 public function setErrorAction($name)
191 $this->errorAction = $name;
194 // ###################################################################
196 * Adds a directory of files to the maps, with the file names (without .php)
197 * as the map request name
199 * @param string Directory path
201 public function addFileDirectory($path)
203 $path = BSFunctions::FetchSourcePath($path);
204 $controllers = BSFunctions::ScanDirectory($path);
206 foreach ($controllers AS $file)
208 $controller = str_replace('.php', '', $file);
209 $this->addFileRequest($controller, $path . $file);
213 // ###################################################################
215 * Routes a request to the loading of a file; this is the most common
216 * routing request and it performs no extra functions
218 * @param string Request name
219 * @param string File name
221 public function addFileRequest($request, $file)
223 $this->map["$request"] = array(self
::MAP_LOADER
=> '_loadFile', self
::MAP_PARAMS
=> array($file));
226 // ###################################################################
228 * Adds a list of files to the mapping; the controller will become
229 * the file name without the .php extension
231 * @param string Search path
233 public function addControllerDirectory($path)
235 $path = BSFunctions
::FetchSourcePath($path);
236 $controllers = BSFunctions
::ScanDirectory($path);
238 foreach ($controllers AS $file)
240 $controller = str_replace('.php', '', $file);
241 $this->addController($controller, $path . $controller);
245 // ###################################################################
247 * Routes a request to the controller system
249 * @param string Request name
250 * @param string Controller name; the file must be named <X>.php and define a class <X>Controller.php
252 public function addController($request, $file)
254 $this->map
["$request"] = array(self::MAP_LOADER => '_invokeController', self::MAP_PARAMS => array($file));
257 // ###################################################################
259 * Includes a given file name
261 * @param string File name
263 private function _loadFile($filename)
265 if (!file_exists($filename))
267 $this->_error(sprintf(_('Cannot find the file "%
1$s"'), $filename));
272 // ###################################################################
274 * Loads and then invokes a controller
276 * @param string File name of the controller
278 public function _invokeController($controller)
280 $this->_loadFile($controller);
282 $controllerName = basename($controller, '.php');
283 $className = "{$controllerName}Controller";
285 if (!class_exists($className))
287 trigger_error('The controller "' . $className . '" does not exist and therfore cannot have actions forwarded to it');
290 if (!is_subclass_of($className, 'BSRouterController'))
292 trigger_error('Cannot invoke controller-style actions on a non-controller: requested controller is not of the type BSRouterController');
295 // these go out the door so BSInput will process them and merge them into BSInput->in[]
296 $_GET = $this->params
;
297 $request = new $className(BSRegister
::LoadModule('Input'));
299 if (!method_exists($className, $this->action
))
301 trigger_error('Invalid action called on controller: ' . $className . ' does not respond to ' . $this->action
. '()');
304 $request->{$this
->action
}();
307 // ###################################################################
309 * Sends an error to the error action handler
311 * @param string Error message
313 private function _error($errMsg)
315 if (function_exists($this->errorAction
) OR (is_array($this->errorAction
) AND method_exists($this->errorAction
[0], $this->errorAction
[1])))
317 $this->errorAction($errMsg);
321 trigger_error($errMsg);
327 /*=====================================================================*
328 || ###################################################################
331 || ###################################################################
332 \*=====================================================================*/