Esempio n. 1
0
 public static function dispatch()
 {
     // Se tiver filtros, carrega todos aqui.
     autoload_filters();
     // Se tiver, rotas carrega todos aqui.
     autoload_routes();
     // Verifica e prepara a URL
     self::uri_prepare();
     self::$method = $_SERVER['REQUEST_METHOD'];
     $searches = array_keys(static::$patterns);
     $replaces = array_values(static::$patterns);
     self::$routes = str_replace('//', '', self::$routes);
     $found_route = false;
     $query = '';
     $q_arr = array();
     if (strpos(self::$uri, '&') > 0) {
         $query = substr(self::$uri, strpos(self::$uri, '&') + 1);
         self::$uri = substr(self::$uri, 0, strpos(self::$uri, '&'));
         $q_arr = explode('&', $query);
         foreach ($q_arr as $q) {
             $qobj = explode('=', $q);
             $q_arr[] = array($qobj[0] => $qobj[1]);
             if (!isset($_GET[$qobj[0]])) {
                 $_GET[$qobj[0]] = $qobj[1];
             }
         }
     }
     if (in_array(self::$uri, self::$routes)) {
         $route_pos = array_keys(self::$routes, self::$uri);
         foreach ($route_pos as $route) {
             if (self::$methods[$route] == self::$method || self::$methods[$route] == 'ANY') {
                 $found_route = true;
                 if (!is_object(self::$callbacks[$route])) {
                     self::invokeObject(self::$callbacks[$route]);
                     if (self::$halts) {
                         return;
                     }
                 } else {
                     call_user_func(self::$callbacks[$route]);
                     if (self::$halts) {
                         return;
                     }
                 }
             }
         }
     } else {
         $pos = 0;
         foreach (self::$routes as $route) {
             $route = str_replace('//', '', $route);
             if (strpos($route, ':') !== false) {
                 $route = str_replace($searches, $replaces, $route);
             }
             if (preg_match('#^' . $route . '$#', self::$uri, $matched)) {
                 if (self::$methods[$pos] == self::$method || self::$methods[$pos] == 'ANY') {
                     $found_route = true;
                     array_shift($matched);
                     if (!is_object(self::$callbacks[$pos])) {
                         self::invokeObject(self::$callbacks[$pos], $matched);
                         if (self::$halts) {
                             return;
                         }
                     } else {
                         call_user_func_array(self::$callbacks[$pos], $matched);
                         if (self::$halts) {
                             return;
                         }
                     }
                 }
             }
             ++$pos;
         }
     }
     if (self::$fallback) {
         $found_route = self::autoDispatch();
     }
     if (!$found_route) {
         if (!self::$errorCallback) {
             self::$errorCallback = function () {
                 $method = explodeCamelCase(self::$method);
                 if ($method[0] != 'xhr') {
                     if (Session::get('s_loggedIn')) {
                         header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found");
                         Response::error('404');
                     } else {
                         Redirect::to("/");
                     }
                 }
             };
         }
         if (!is_object(self::$errorCallback)) {
             self::invokeObject(self::$errorCallback, null, 'No routes found.');
             if (self::$halts) {
                 return;
             }
         } else {
             call_user_func(self::$errorCallback);
             if (self::$halts) {
                 return;
             }
         }
     }
     // verifica os filtros
     if (self::$filter) {
         foreach (self::$filters as $action => $name) {
             foreach ($name as $callback) {
                 if (self::AFTER === $action) {
                     $callback();
                 }
             }
         }
     }
 }
Esempio n. 2
0
 public static function logout($redirect = '/')
 {
     Session::destroy();
     Redirect::to($redirect);
 }