Adding the Router class
[isso.git] / Router.php
1 <?php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * Request Router (Router.php)
24 *
25 * @package ISSO
26 */
27
28 require_once('ISSO/Functions.php');
29
30 /**
31 * Router
32 *
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
36 *
37 * @author Blue Static
38 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
39 * @version $Revision$
40 * @package ISSO
41 *
42 */
43 class BSRouter
44 {
45 const MAP_ACTION = 0;
46 const MAP_PARAMS = 1;
47
48 /**
49 * Routing map of request:(method,args...)
50 * @var array
51 */
52 private $map = array();
53
54 /**
55 * The base URL
56 * @var string
57 */
58 private $basePath;
59
60 /**
61 * Map action for errors
62 * @var string
63 */
64 private $errorAction = ':undefined:';
65
66 /**
67 * Router action requested
68 * @var string
69 */
70 private $request;
71
72 /**
73 * Request paramaters
74 * @var array
75 */
76 private $params = array();
77
78 // ###################################################################
79 /**
80 * Constructor
81 */
82 public function __construct()
83 {
84 $this->basePath = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['SCRIPT_NAME']);
85 }
86
87 // ###################################################################
88 /**
89 * Listens for routing requests and handles them appropriately
90 */
91 public function route()
92 {
93 $request = str_replace($this->basePath, '', $_SERVER['REQUEST_URI']);
94
95 $params = explode('/', $request);
96 $this->request = $params[0];
97 unset($params[0]);
98
99 if (sizeof($params) == 0)
100 {
101 // do nothing
102 }
103 else if (sizeof($params) == 1)
104 {
105 $this->params[':id'] = $params[1];
106 }
107 else
108 {
109 $key = '';
110 for ($i = 1; $i <= sizeof($params); $i++)
111 {
112 if ($i == 1 AND $params["$i"][0] != '+')
113 {
114 $this->params[':id'] = $params["$i"];
115 continue;
116 }
117
118 if ($params["$i"] != '' AND $params["$i"][0] == '+')
119 {
120 if (!empty($key))
121 {
122 $this->_error(sprintf(_('The paramter "%1$s" does not have a value'), $key));
123 }
124 $key = substr($params["$i"], 1);
125 }
126 else
127 {
128 if (empty($key))
129 {
130 $this->_error(sprintf(_('The value "%1$s" does not have a paramatized key'), $params["$i"]));
131 }
132 $this->params["$key"] = $params["$i"];
133 $key = '';
134 }
135 }
136 }
137
138 if (isset($this->map[ $this->request ]))
139 {
140 call_user_func_array($this->map[ $this->request ][self::MAP_ACTION], $this->map[ $this->request ][self::MAP_PARAMS]);
141 }
142 else
143 {
144 $this->_error(sprintf(_('Routing request "%1$s" not found in map'), $this->request));
145 }
146 }
147
148 // ###################################################################
149 /**
150 * Sets the error handler of the router; when an error occurs while
151 * routing, this method is called
152 *
153 * @param string Error handler name
154 */
155 public function setErrorAction($name)
156 {
157 $this->errorAction = $name;
158 }
159
160 // ###################################################################
161 /**
162 * Adds a directory of files to the maps, with the file names (without .php)
163 * as the map request name
164 *
165 * @param string Directory path
166 */
167 public function addFileDirectory($path)
168 {
169 $path = BSFunctions::FetchSourcePath($path);
170 $controllers = BSFunctions::ScanDirectory($path);
171 $controllers = $controllers[''];
172
173 foreach ($controllers AS $file)
174 {
175 $controller = str_replace('.php', '', $file);
176 $this->addFileRequest($controller, $path . $file);
177 }
178 }
179
180 // ###################################################################
181 /**
182 * Routes a request to the loading of a file; this is the most common
183 * routing request and it performs no extra functions
184 *
185 * @param string Request name
186 * @param string File name
187 */
188 public function addFileRequest($request, $file)
189 {
190 $this->map["$request"] = array(array($this, '_loadFile'), array($file));
191 }
192
193 // ###################################################################
194 /**
195 * Includes a given file name
196 *
197 * @param string File name
198 */
199 private function _loadFile($filename)
200 {
201 if (!file_exists($filename))
202 {
203 $this->_error(sprintf(_('Cannot find the file "%1$s"'), $filename));
204 }
205
206 include($filename);
207 }
208
209 // ###################################################################
210 /**
211 * Sends an error to the error action handler
212 *
213 * @param string Error message
214 */
215 private function _error($errMsg)
216 {
217 if (function_exists($this->errorAction) OR (is_array($this->errorAction) AND method_exists($this->errorAction[0], $this->errorAction[1])))
218 {
219 $this->errorAction($errMsg);
220 }
221 else
222 {
223 trigger_error($errMsg);
224 }
225 exit;
226 }
227 }
228
229 /*=====================================================================*
230 || ###################################################################
231 || # $HeadURL$
232 || # $Id$
233 || ###################################################################
234 \*=====================================================================*/
235 ?>