Пример #1
0
 public static function process($options = [])
 {
     // start buffering
     helper_ob::start(true);
     $controller_class = self::$settings['mvc']['controller_class'];
     // if we are handling error message and controller class has not been loaded
     if ($controller_class == 'controller_error' && error_base::$flag_error_already && !class_exists('controller_error')) {
         require './controller/error.php';
     }
     $controller = new $controller_class();
     // processing options
     if (!empty($options)) {
         foreach ($options as $k => $v) {
             $controller->{$k} = $v;
         }
     }
     // put action into controller
     $controller->action = ['code' => self::$settings['mvc']['controller_action_code'], 'full' => self::$settings['mvc']['controller_action']];
     // check ACL
     if ($controller_class != 'controller_error') {
         helper_acl::merge_data_with_db($controller, self::$settings['mvc']['controller_class']);
         if (helper_acl::can_be_executed($controller, true) == false) {
             throw new Exception('Permission denied!', -1);
         }
     } else {
         // important to unset controller data
         application::set('controller', null);
     }
     // auto populating input property in controller
     if (!empty(self::$settings['application']['controller']['input'])) {
         $controller->input = request::input(null, true, true);
     }
     // init method
     if (method_exists($controller, 'init')) {
         call_user_func(array($controller, 'init'));
     }
     // check if action exists
     if (!method_exists($controller, $controller->action['full'])) {
         throw new Exception('Action does not exists!');
     }
     // calling action
     echo call_user_func(array($controller, $controller->action['full']));
     // auto rendering view only if view exists, processing extension order as specified in .ini file
     $temp_reflection_obj = new ReflectionClass($controller);
     $controller_dir = pathinfo($temp_reflection_obj->getFileName(), PATHINFO_DIRNAME) . '/';
     $controller_file = end(self::$settings['mvc']['controllers']);
     $view = self::$settings['mvc']['controller_view'];
     $flag_view_found = false;
     if (!empty($view)) {
         $extensions = explode(',', isset(self::$settings['application']['view']['extension']) ? self::$settings['application']['view']['extension'] : 'html');
         foreach ($extensions as $extension) {
             $file = $controller_dir . $controller_file . '.' . $view . '.' . $extension;
             if (file_exists($file)) {
                 $controller = new view($controller, $file, $extension);
                 $flag_view_found = true;
                 break;
             }
         }
         // if views are mandatory
         if (!empty(self::$settings['application']['view']['mandatory']) && !$flag_view_found) {
             throw new Exception('View ' . $view . ' does not exists!');
         }
     }
     // autoloading media files
     layout::include_media($controller_dir, $controller_file, $view, $controller_class);
     // appending view after controllers output
     $controller->view = ($controller->view ?? '') . helper_ob::clean();
     // if we have to render debug toolbar
     if (debug::$toolbar) {
         helper_ob::start();
     }
     // call pre rendering method in bootstrap
     bootstrap::pre_render();
     // rendering layout
     $__skip_layout = application::get('flag.global.__skip_layout');
     if (!empty(self::$settings['mvc']['controller_layout']) && empty($__skip_layout)) {
         helper_ob::start();
         if (file_exists(self::$settings['mvc']['controller_layout_file'])) {
             $controller = new layout($controller, self::$settings['mvc']['controller_layout_file'], self::$settings['mvc']['controller_layout_extension']);
         }
         // session expiry dialog before replaces
         session::expiry_dialog();
         // buffer output and handling javascript files, chicken and egg problem
         $from = ['<!-- [numbers: messages] -->', '<!-- [numbers: title] -->', '<!-- [numbers: document title] -->', '<!-- [numbers: actions] -->', '<!-- [numbers: breadcrumbs] -->', '<!-- [numbers: javascript links] -->', '<!-- [numbers: javascript data] -->', '<!-- [numbers: css links] -->', '<!-- [numbers: layout onload] -->', '<!-- [numbers: layout onhtml] -->'];
         $to = [layout::render_messages(), layout::render_title(), layout::render_document_title(), layout::render_actions(), layout::render_breadcrumbs(), layout::render_js(), layout::render_js_data(), layout::render_css(), layout::render_onload(), layout::$onhtml];
         echo str_replace($from, $to, helper_ob::clean());
     } else {
         echo $controller->view;
     }
     // ajax calls that has not been processed by application
     if (application::get('flag.global.__ajax')) {
         layout::render_as(['success' => false, 'error' => [i18n(null, 'Could not process ajax call!')]], 'application/json');
     }
 }