| src/Pluf/Dispatcher.php |
| 206 | 206 | * Load the controllers. |
| 207 | 207 | * |
| 208 | 208 | * @param string File including the views. |
| 209 | | * @param string Possible prefix to add to the views. |
| 210 | 209 | * @return bool Success. |
| 211 | 210 | */ |
| 212 | | public static function loadControllers($file, $prefix='') |
| 211 | public static function loadControllers($file) |
| 213 | 212 | { |
| 214 | 213 | if (file_exists($file)) { |
| 215 | | if ($prefix == '') { |
| 216 | | $GLOBALS['_PX_views'] = include $file; |
| 217 | | } else { |
| 218 | | $GLOBALS['_PX_views'] = Pluf_Dispatcher::addPrefixToViewFile($prefix, $file); |
| 219 | | } |
| 214 | $GLOBALS['_PX_views'] = include $file; |
| 220 | 215 | return true; |
| 221 | 216 | } |
| 222 | 217 | return false; |
| 223 | 218 | } |
| 224 | | |
| 225 | | |
| 226 | | /** |
| 227 | | * Register an action controller. |
| 228 | | * |
| 229 | | * - The class must provide a "standalone" action method |
| 230 | | * class::actionmethod($request, $match) |
| 231 | | * - The priority is to order the controller matches. |
| 232 | | * 5: Default, if the controller provides some content |
| 233 | | * 1: If the controller provides a control before, without providing |
| 234 | | * content, note that in this case the return code must be a redirection. |
| 235 | | * 8: If the controller is providing a catch all case to replace the |
| 236 | | * default 404 error page. |
| 237 | | * |
| 238 | | * @param string Class name providing the action controller |
| 239 | | * @param string The method of the plugin to be called |
| 240 | | * @param string Regex to match on the query string |
| 241 | | * @param int Priority (5) |
| 242 | | * @return void |
| 243 | | */ |
| 244 | | public static function registerController($model, $method, $regex, $priority=5) |
| 245 | | { |
| 246 | | if (!isset($GLOBALS['_PX_views'])) { |
| 247 | | $GLOBALS['_PX_views'] = array(); |
| 248 | | } |
| 249 | | $GLOBALS['_PX_views'][] = array('model' => $model, |
| 250 | | 'regex' => $regex, |
| 251 | | 'priority' => $priority, |
| 252 | | 'method' => $method); |
| 253 | | } |
| 254 | | |
| 255 | | /** |
| 256 | | * Add the controllers of an application with a given prefix. |
| 257 | | * |
| 258 | | * Suppose you have a new app you want to use within another |
| 259 | | * existing application, you may need to change the base URL not |
| 260 | | * to conflict with the existing one. For example you want to have |
| 261 | | * domain.com/forum-a/ and domain.com/forum-b/ to use 2 forums at |
| 262 | | * the same time. |
| 263 | | * |
| 264 | | * This method do that, it takes a typical "view" file and rewrite |
| 265 | | * the regex to append the prefix. Note that you should use the |
| 266 | | * 'url' tag in the template and use Pluf_HTTP_URL_reverse in the |
| 267 | | * views to not hardcode the urls or this will not work. |
| 268 | | * |
| 269 | | * @param string Prefix, for example '/alternate'. |
| 270 | | * @param string File with the views. |
| 271 | | * @return array Prefixed views. |
| 272 | | */ |
| 273 | | static public function addPrefixToViewFile($prefix, $file) |
| 274 | | { |
| 275 | | if (file_exists($file)) { |
| 276 | | $views = include $file; |
| 277 | | } else { |
| 278 | | throw new Exception('View file not found: '.$file); |
| 279 | | } |
| 280 | | return Pluf_Dispatcher::addPrefixToViews($prefix, $views); |
| 281 | | } |
| 282 | | |
| 283 | | /** |
| 284 | | * Add a prefix to an array of views. |
| 285 | | * |
| 286 | | * You can use it for example to not hardcode that in your CMS the |
| 287 | | * blog is located as /blog but is configured in the configuration |
| 288 | | * of the CMS, that way in French this could be /carnet. |
| 289 | | * |
| 290 | | * @param string Prefix, for example '/alternate'. |
| 291 | | * @param array Array of the views. |
| 292 | | * @return array Prefixed views. |
| 293 | | */ |
| 294 | | static public function addPrefixToViews($prefix, $views) |
| 295 | | { |
| 296 | | $res = array(); |
| 297 | | foreach ($views as $view) { |
| 298 | | $view['regex'] = '#^'.$prefix.substr($view['regex'], 2); |
| 299 | | $res[] = $view; |
| 300 | | } |
| 301 | | return $res; |
| 302 | | } |
| 303 | 219 | } |
| 304 | 220 | |