Exemplo n.º 1
0
 /**
  * @param string $dispatcher_dir The directory that the application is running in.
  * @param string $base_url The base url that the application is running at.
  * @param string $requested_url The url that is being requested relative to the base url.
  * @return string Returns the response from a Controller that responded to the requested url.
  */
 public static function dispatch($dispatcher_dir, $base_url, $requested_url, $try_with_trailing_slash = true)
 {
     if ($try_with_trailing_slash) {
         self::load();
     }
     PicoraEvent::notify('PicoraDispatcher.beforeDispatch', $requested_url);
     self::$status['current_parameters'] = array_merge($_POST, $_GET);
     unset(self::$status['current_parameters'][self::DEFAULT_ROUTE_PARAMETER_NAME]);
     self::$status['dispatcher_dir'] = $dispatcher_dir . '/';
     self::$status['base_url'] = $base_url;
     self::$status['request_url'] = $requested_url;
     self::$status['flash_values'] =& $_SESSION[PicoraController::FLASH_SESSION_KEY_NAME]['values'];
     foreach (self::$routes as $route => $class_and_method) {
         if ($requested_url == $route && ($response = self::tryRoute($route, $class_and_method)) !== false) {
             return self::generateResponse($response);
         }
         if (preg_replace('{([^/]+)}', '*', $route) == preg_replace('{([^/]+)}', '*', $requested_url)) {
             preg_match_all('{([^/]+)?}', $route, $route_components);
             preg_match_all('{([^/]+)?}', $requested_url, $requested_url_components);
             $arguments = array();
             foreach ($requested_url_components[0] as $key => $requested_url_component) {
                 if ($requested_url_component == '') {
                     continue;
                 } elseif (strpos($route_components[0][$key], ':') !== false) {
                     $arguments[substr($route_components[0][$key], 1)] = $requested_url_component;
                 } elseif ($route_components[0][$key] != $requested_url_component) {
                     continue 2;
                 }
             }
             if (($response = self::tryRoute($route, $class_and_method, $arguments)) !== false) {
                 return self::generateResponse($response);
             }
         }
     }
     if ($try_with_trailing_slash && strlen($requested_url) > 1 && substr($requested_url, 0, -1) != '/' && self::dispatch($dispatcher_dir, $base_url, $requested_url . '/', false)) {
         header('HTTP/1.1 301 Moved Permanently');
         PicoraController::redirect('http' . ($_SERVER['SERVER_PORT'] == 443 ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '/');
     }
     if ($try_with_trailing_slash) {
         header('HTTP/1.1 404 Not Found');
         $response = self::call(self::$error_handler, array(), array());
         foreach (PicoraEvent::getObserverList('PicoraDispatcher.afterDispatch') as $callback) {
             call_user_func($callback, $response);
         }
         return self::generateResponse($response);
     }
 }