Example #1
0
 /**
  * Runs the application
  * Should be called once in appinit.php
  *
  *
  * @return void
  * @static
  */
 public static function run()
 {
     try {
         try {
             if (substr(Request::getUri(), -1) !== '/') {
                 if (Router::getMatchedRoute(true)) {
                     self::redirect(Request::getBaseUri() . Request::getUri() . '/' . Request::getQueryString());
                 }
             }
             // log the uri
             Log::out(__METHOD__ . ' URI: ' . Request::getBaseUri() . Request::getUri(), Log::LEVEL_DEBUG);
             // only starts output buffering in development mode
             if (!self::isDevMode()) {
                 ob_start();
             }
             $all_routes_processed = false;
             $matched_route = null;
             $offset = -1;
             // start searching for matched route
             while (!$all_routes_processed) {
                 $matched_route = Router::getMatchedRoute(false, $offset + 1);
                 if (!$matched_route) {
                     App::notFound();
                 }
                 // log the pattern
                 Log::out(__METHOD__ . ' Matched route: "' . $matched_route->name . '", pattern: ' . $matched_route->getPatternRegex() . ', offset: ' . $matched_route->getOffset(), Log::LEVEL_DEBUG);
                 // log route params
                 Log::out(__METHOD__ . " Route params: \n" . print_r($matched_route->getParams(), true), Log::LEVEL_DEBUG);
                 $offset = $matched_route->getOffset();
                 try {
                     Loader::invoke($matched_route);
                 } catch (BakedCarrotPassException $e) {
                     $all_routes_processed = false;
                     continue;
                 }
                 $all_routes_processed = true;
             }
             if (!self::isDevMode()) {
                 ob_end_flush();
             }
         } catch (Exception $e) {
             if (self::isDevMode()) {
                 while (@ob_end_clean()) {
                 }
             }
             $classes_to_test = array(get_class($e), get_parent_class($e), 'Exception');
             $executed = false;
             foreach ($classes_to_test as $class) {
                 if (isset(self::$exception_handlers[$class])) {
                     Loader::invokeExceptionHandler($e, self::$exception_handlers[$class]);
                     Log::out(__METHOD__ . ' Exception handler "' . self::$exception_handlers[$class] . '" invoked for class "' . $class . '"', Log::LEVEL_INFO);
                     $executed = true;
                     break;
                 }
             }
             if (!$executed) {
                 throw $e;
             }
         }
     } catch (Exception $e) {
         self::$instance->handleDefaultException($e);
     }
 }