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 route() 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
39 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
50 * Routing map of request:(controller,:method,args...)
53 private $map = array();
62 * Map action for errors
65 private $errorAction = ':undefined:';
68 * Router controller requested
74 * Router action requsted
83 private $params = array();
85 // ###################################################################
89 public function __construct()
91 $this->basePath
= str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['SCRIPT_NAME']);
94 // ###################################################################
96 * Listens for routing requests and handles them appropriately
98 public function route()
100 $request = str_replace($this->basePath
, '', $_SERVER['REQUEST_URI']);
102 $params = explode('/', $request);
103 $request = explode('.', $params[0]);
104 $this->request
= $request[0];
105 $this->action
= ((!isset($request[1]) OR empty($request[1])) ?
'Index' : $request[1]);
108 if (sizeof($params) == 0)
112 else if (sizeof($params) == 1)
114 $this->params
[':id'] = $params[1];
119 for ($i = 1; $i <= sizeof($params); $i++
)
121 if ($i == 1 AND $params["$i"][0] != '+')
123 $this->params
[':id'] = $params["$i"];
127 if ($params["$i"] == '')
132 if ($params["$i"][0] == '+')
136 $this->_error(sprintf(_('The paramter "%1$s" does not have a value'), $key));
138 $key = substr($params["$i"], 1);
144 $this->_error(sprintf(_('The value "%1$s" does not have a paramatized key'), $params["$i"]));
146 $this->params
["$key"] = $params["$i"];
152 if (isset($this->map
[ $this->request
]))
154 call_user_func_array(array($this, $this->map
[ $this->request
][self
::MAP_LOADER
]), $this->map
[ $this->request
][self
::MAP_PARAMS
]);
158 $this->_error(sprintf(_('Routing request "%1$s" not found in map'), $this->request
));
162 // ###################################################################
164 * Sets the error handler of the router; when an error occurs while
165 * routing, this method is called
167 * @param string Error handler name
169 public function setErrorAction($name)
171 $this->errorAction
= $name;
174 // ###################################################################
176 * Adds a directory of files to the maps, with the file names (without .php)
177 * as the map request name
179 * @param string Directory path
181 public function addFileDirectory($path)
183 $path = BSFunctions
::FetchSourcePath($path);
184 $controllers = BSFunctions
::ScanDirectory($path);
186 foreach ($controllers AS $file)
188 $controller = str_replace('.php', '', $file);
189 $this->addFileRequest($controller, $path . $file);
193 // ###################################################################
195 * Routes a request to the loading of a file; this is the most common
196 * routing request and it performs no extra functions
198 * @param string Request name
199 * @param string File name
201 public function addFileRequest($request, $file)
203 $this->map
["$request"] = array(self
::MAP_LOADER
=> '_loadFile', self
::MAP_PARAMS
=> array($file));
206 // ###################################################################
208 * Adds a list of files to the mapping; the controller will become
209 * the file name without the .php extension
211 * @param string Search path
213 public function addControllerDirectory($path)
215 $path = BSFunctions
::FetchSourcePath($path);
216 $controllers = BSFunctions
::ScanDirectory($path);
218 foreach ($controllers AS $file)
220 $controller = str_replace('.php', '', $file);
221 $this->addController($controller, $path . $controller);
225 // ###################################################################
227 * Routes a request to the controller system
229 * @param string Request name
230 * @param string Controller name; the file must be named <X>.php and define a class <X>Controller.php
232 public function addController($request, $file)
234 $this->map
["$request"] = array(self
::MAP_LOADER
=> '_invokeController', self
::MAP_PARAMS
=> array($file));
237 // ###################################################################
239 * Includes a given file name
241 * @param string File name
243 private function _loadFile($filename)
245 if (!file_exists($filename))
247 $this->_error(sprintf(_('Cannot find the file "%1$s"'), $filename));
252 // ###################################################################
254 * Loads and then invokes a controller
256 * @param string File name of the controller
258 public function _invokeController($controller)
260 $this->_loadFile($controller);
262 $controllerName = basename($controller, '.php');
263 $className = "{$controllerName}Controller";
265 if (!class_exists($className))
267 trigger_error('The controller "' . $className . '" does not exist and therfore cannot have actions forwarded to it');
270 if (!is_subclass_of($className, 'BSRouterController'))
272 trigger_error('Cannot invoke controller-style actions on a non-controller: requested controller is not of the type BSRouterController');
275 // these go out the door so BSInput will process them and merge them into BSInput->in[]
276 $_GET = $this->params
;
277 $request = new $className(BSRegister
::LoadModule('Input'));
279 if (!method_exists($className, $this->action
))
281 trigger_error('Invalid action called on controller: ' . $className . ' does not respond to ' . $this->action
. '()');
284 $request->{$this->action
}();
287 // ###################################################################
289 * Sends an error to the error action handler
291 * @param string Error message
293 private function _error($errMsg)
295 if (function_exists($this->errorAction
) OR (is_array($this->errorAction
) AND method_exists($this->errorAction
[0], $this->errorAction
[1])))
297 $this->errorAction($errMsg);
301 trigger_error($errMsg);
307 /*=====================================================================*
308 || ###################################################################
311 || ###################################################################
312 \*=====================================================================*/