Ejemplo n.º 1
0
 function process_route()
 {
     if (!$this->loaded) {
         if (!$this->parse_request()) {
             $this->raise('Controller "' . $this->controller . '" not found...', '...nor were any matching routes found.', 404);
         }
     }
     // include application controller
     if (is_file($this->application_controller_file)) {
         include_once $this->application_controller_file;
     }
     // include current controller
     include_once $this->controller_file;
     if (class_exists($this->controller_class, false)) {
         // create child controller object
         $class = $this->controller_class;
         $this->controller_object = new $class();
         if (is_object($this->controller_object)) {
             // set initial properties of child controller object
             $this->controller_object->init_filters();
             $this->controller_object->controller = $this->controller;
             $this->controller_object->action = $this->action;
             $this->controller_object->id = $this->id;
             $this->controller_object->controllers_path =& $this->controllers_path;
             $this->controller_object->helpers_path =& $this->helpers_path;
             $this->controller_object->helpers_base_path =& $this->helpers_base_path;
             $this->controller_object->views_path = $this->views_path;
             $this->controller_object->layouts_path =& $this->layouts_path;
             $this->controller_object->layouts_base_path =& $this->layouts_base_path;
             $this->controller_object->route_params =& $this->route_params;
             $this->controller_object->requested_path =& $this->requested_path;
             $this->controller_object->current_route =& $this->current_route;
             // set static Znap properties
             Znap::$current_controller_object =& $this->controller_object;
             Znap::$current_controller_class_name = $this->controller_class;
             Znap::$current_controller_name = $this->controller;
             Znap::$current_controller_path = $this->controllers_path;
             Znap::$current_action_name = $this->action;
             Znap::$current_route =& $this->current_route;
             // include main application helper file
             if (is_file($this->application_helper_file)) {
                 include_once $this->application_helper_file;
             }
             // load language specific strings
             //  - defaults to english if Znap::$settings['language']
             //    is not defined by before filters
             Znap::load_strings();
             // include controller specific preferences
             if (isset($this->controller_object->has_prefs) && Znap::$prefs->read($this->controller, true)) {
                 $this->controller_object->prefs =& Znap::$prefs->{$this->controller};
             }
             // execute before_filters, display an error page if any filter method returns false
             if (($before_filters_result = $this->controller_object->execute_before_filters()) === true) {
                 // supress output for capture
                 ob_start();
                 // include controller specific helper file
                 if (is_file($this->helper_file)) {
                     include_once $this->helper_file;
                 }
                 // call default action/method if none is defined
                 if ($this->action === null) {
                     $this->action = $this->default_action;
                 }
                 // execute main method
                 if ($this->valid_action($this->action)) {
                     $action = $this->action;
                     $this->controller_object->{$action}($this->action_params);
                 } elseif (is_file($this->views_path . '/' . $this->action . '.' . Znap::$views_extension)) {
                     $action = $this->action;
                 } else {
                     $this->raise('Action "' . $this->action . '" not found.', null, 404);
                 }
                 $this->controller_object->execute_after_filters();
                 $this->controller_object->action_called = true;
                 // include any additionaly defined helpers
                 if (count($this->controller_object->helpers)) {
                     foreach ($this->controller_object->helpers as $helper) {
                         if (is_file($this->helpers_base_path . '/' . $helper . '_helper.php')) {
                             include_once $this->helpers_base_path . '/' . $helper . '_helper.php';
                         }
                     }
                 }
                 // if true Session::flash() messages will be displayed till this is set to false on a page load.
                 Znap::$keep_flash = $this->keep_flash;
                 // check if $redirect_to was set and redirect accordingly if so
                 if (isset($this->controller_object->redirect_to) && $this->controller_object->redirect_to != '') {
                     $this->redirect_to($this->controller_object->redirect_to);
                 }
                 // if render_text is defined as a string, render it instead of layout & view files
                 if (isset($this->controller_object->render_text) && $this->controller_object->render_text != '') {
                     $this->render_text($this->controller_object->render_text);
                 }
                 // if render_action is defined, use it as the render action, if it is false, don't render action view file
                 if (isset($this->controller_object->render_action)) {
                     $action = $this->controller_object->render_action;
                 }
                 // render view file
                 if ($action !== false && !$this->controller_object->render_action($action)) {
                     $this->raise('No "' . $action . '" view file found.', null, 500);
                 }
                 // grab captured output
                 $this->controller_object->content_for_layout = ob_get_contents();
                 ob_end_clean();
                 // render or not to render layout (that is the question)
                 if ($this->controller_object->render_layout !== false && ($layout_file = $this->controller_object->determine_layout())) {
                     if (Timer::$started) {
                         ob_start();
                     }
                     if (!$this->controller_object->render_file($layout_file)) {
                         echo $this->controller_object->content_for_layout;
                     }
                     if (Timer::$started) {
                         ob_end_flush();
                     }
                 } else {
                     echo $this->controller_object->content_for_layout;
                 }
             } else {
                 $this->raise('The "' . $before_filters_result . '" before filter failed.', null, 500);
             }
         } else {
             $this->raise('Failed to initiate controller object "' . $this->controller . '".', null, 500);
         }
     } else {
         $this->raise('Controller class "' . $this->controller_class . '" not found.', null, 500);
     }
 }