Axiom (v1.2.0)

A lightweight PHP framework
H:/Workspace/php-axiom/www/php-axiom/libraries/axiom/axRouter.class.php
Go to the documentation of this file.
00001 <?php
00018 class axRouter {
00019     
00024     protected static $_routes;
00025 
00030     protected static $_request;
00031     
00036     protected static $_response;
00037     
00068     public static function connect ($template, $params = array(), array $options = array()) {
00069         if (!is_object($template)) {
00070             if (is_string($params))
00071                 $params = self::_parseParamString($params);
00072             
00073             $params  += array('action' => 'index');
00074             $template = new axRoute($template, $params, $options);
00075         }
00076         
00077         if ($template instanceof axRoute)
00078             self::$_routes[] = $template;
00079         else
00080             throw new RuntimeException("Cannot connect route", 2049);
00081     }
00082     
00097     public static function run ($route = null, $action = null) {
00098         if (!isset(self::$_request))
00099             self::$_request = new axRequest;
00100             
00101         if (!isset(self::$_response))
00102             self::$_response = new axResponse;
00103             
00104         if (empty($route))
00105             $route = self::_getRoute(self::$_request->url);
00106         
00107         if ($route instanceof axRoute) {
00108             $params  = $route->getParams();
00109             $options = $route->getOptions();
00110             
00111             if (empty($params['controller']))
00112                 throw new RuntimeException("No controller specified");
00113 
00114             self::$_request->add($params);
00115             $controller = ucfirst($params['controller']);
00116             $action     = !empty($params['action']) ? $params['action'] : 'index';
00117             
00118             if (!empty($options['lang']))
00119                 $lang = $options['lang'];
00120                 
00121             if (!empty($params['lang']))
00122                 $lang = $params['lang'];
00123             
00124             if (!empty($lang) && Axiom::locale() && $lang != Axiom::locale()->getLang())
00125                 Axiom::locale()->setLang($lang);
00126             
00127             if (!empty($options['module'])) {
00128                 try {
00129                     Axiom::module()->load($options['module']);
00130                 }
00131                 catch (Exception $e) {
00132                     return self::run('error', 'http500');
00133                 }
00134             }
00135         }
00136         elseif (is_string($route)) {
00137             if (Axiom::module()->exists($route))
00138                 Axiom::module()->load($route);
00139             
00140             $controller = ucfirst($route);
00141             $action     = !empty($action) ? $action : 'index';
00142         }
00143         else {
00144             list($controller, $action) = array('ErrorController', 'http404');
00145         }
00146         
00147         if (strpos(strtolower($controller), 'controller') === false)
00148             $controller .= 'Controller';
00149         
00150         if (!class_exists($controller, true))
00151             list($controller, $action) = array('ErrorController', 'http404');
00152         
00153         self::_load($controller, $action);
00154     }
00155     
00166     protected static function _load ($controller, $action = null) {
00167         if (empty($action))
00168             $action = "index";
00169             
00170         try {
00171             call_user_func_array(array($controller, '_init'), array(&self::$_request, &self::$_response));
00172             if (!is_callable(array($controller, $action)))
00173                 throw new BadMethodCallException("No such action for $controller", 2003);
00174             self::$_response->add(call_user_func(array($controller, $action)));
00175         }
00176         catch (BadMethodCallException $e) {
00177             return self::run("error", "http404");
00178         }
00179         catch (axLoginException $e) {
00180             return self::run("error", "http403");
00181         }
00182         catch (axForwardException $e) {
00183             return self::load($e->getController(), $e->getAction());
00184         }
00185         catch (axRedirectException $e) {
00186             return self::_redirect($e);
00187         }
00188         catch (Exception $e) {
00189             if ($code = $e->getCode())
00190                 self::$_response->error_code = $code;
00191             return self::run("error", "http500");
00192         }
00193         
00194         if (!self::$_response->getViewSection()) {
00195             $section = strtolower($controller);
00196             $section = ($offset = strpos($section, 'controller')) !== false ? 
00197                 substr($section, 0, $offset): 
00198                 $section;
00199             self::$_response->setViewSection($section);
00200         }
00201         
00202         if (!self::$_response->getView()) {
00203             $view = strtolower($action);
00204             self::$_response->setView($view);
00205         }
00206         
00207         try {
00208             echo Axiom::view()->load(self::$_response);
00209         }
00210         catch (Exception $e) {
00211             self::$_response->reset();
00212             if ($code = $e->getCode())
00213                 self::$_response->error_code = $code;
00214             return self::run("error", "http500");
00215         }
00216     }
00217     
00227     protected static function _redirect (axRedirectException $exception) {
00228         header((string)$exception);
00229         
00230         if ($exception->getMethod() == axRedirectException::REDIRECT_REFRESH) {
00231             self::$_response->addAll(array('url' => $exception->getUrl()));
00232             self::load('ErrorController', 'redirection');
00233         }
00234     }
00235     
00249     protected static function _parseParamString ($params) {
00250         list($controller, $action) = (strpos($params, '::') !== false) ? explode('::', $params) : array($params, 'index');
00251         return array('controller' => $controller, 'action' => $action);
00252     }
00253     
00259     protected static function _getRoute ($url) {
00260         foreach (self::$_routes as $route) {
00261             if ($params = $route->match($url)) {
00262                 return $route;
00263             }
00264         }
00265         return false;
00266     }
00267 }
 All Data Structures Files Functions Variables