예제 #1
0
 /**
  * Execute application
  */
 protected final function _execute()
 {
     // Start output collection
     ob_start();
     // Get application execution mode from config
     $this->setMode(strtolower(strval($this->getConfig()->getValue('mode', self::MODE_NORMAL))));
     // Attach error and exception handlers if applicatio execution mode is 'debug'
     if ($this->isMode(self::MODE_DEBUG)) {
         $old_errr = error_reporting(E_ALL);
         set_error_handler(array($this, '_error_handler'), E_ALL);
         set_exception_handler(array($this, '_exception_handler'));
     }
     // Set application plugin manager listen global event dispatcher
     $this->getPluginManager()->setTarget(Zoombi::getDispatcher());
     // Notify for start execution
     $this->emit(new ZEvent($this, 'preExecute'));
     // Notify befor route start
     $this->emit(new ZEvent($this, 'preRoute'));
     if (!$this->getFlag(self::FLAG_NO_ROUTE)) {
         $request = null;
         // Process request info
         $req_qry = explode('?', $_SERVER['REQUEST_URI'], 2);
         //if( count($req_qry) > 1 )
         $request = array_shift($req_qry);
         // Get content rewrite variabe
         $rt = $this->getConfig()->getValue('routevar', 'rt');
         $rvar = null;
         if (isset($_GET[$rt])) {
             $rvar = $_GET[$rt];
             unset($_GET[$rt]);
         }
         if ($req_qry) {
             $rvar .= '?' . $req_qry[0];
         }
         // Attach routing rules to router
         $rc = new ZConfig($this->fromApplicationBaseDir($this->getConfig()->getValue('routes', null)));
         $router = new ZRouter($rvar, $rc->toArray());
         unset($rc);
         $this->setRouter($router);
         //$router->route();
         if ($this->isMode(self::MODE_DEBUG)) {
             Zoombi::log('Request route: ' . $router->getRequest(), 'FrontController');
         }
         // Get routing result
         $route = $router->getRedirect();
         if ($this->isMode(self::MODE_DEBUG)) {
             Zoombi::log('Redirect route: ' . $route, 'FrontController');
         }
         // Notify for end of routing process
         $this->emit(new ZEvent($this, 'postRoute', $route));
         // Notify before controller has created
         $this->emit(new ZEvent($this, 'preController', $route->getController()));
         if (!$this->getFlag(self::FLAG_NO_CONTROLLER)) {
             // Check if we have requested controller
             if (!$this->_processRoute($route, $_code, $_message)) {
                 if ($_code == ZLoader::EXC_NO_FILE) {
                     $this->emit(new ZEvent($this, 'onError', 404, $route, $_code, $_message));
                     $this->emit(new ZEvent($this, 'on404', $route, $_code, $_message));
                 } else {
                     $this->emit(new ZEvent($this, 'onError', 500, $route, $_code, $_message));
                     $this->emit(new ZEvent($this, 'on500', $route, $_code, $_message));
                 }
             }
             $route = $this->getRouter()->getRedirect();
             // Notify after controller be created
             $this->emit(new ZEvent($this, 'postController', $route->getController()));
             // Processing controller
             $ctl = $this->getController();
             if ($ctl) {
                 if (!$ctl->hasAction($route->getAction())) {
                     $route->action = $this->getConfig()->getValue('controller.default_action', 'index');
                 }
                 $router->setCurrent($route)->setForward($route);
                 if ($this->isMode(self::MODE_DEBUG)) {
                     Zoombi::log('Current route: ' . (string) $router->getCurrent(), 'FrontController');
                 }
                 $this->emit(new ZEvent($this, 'preAction', $route->action));
                 if (!$this->getFlag(self::FLAG_NO_ACTION)) {
                     try {
                         $this->m_return = $ctl->requestAction($route->action, $route->params);
                         $this->emit(new ZEvent($this, 'postAction'));
                     } catch (ZControllerException $e) {
                         if ($e->getCode() != ZControllerException::EXC_QUIT_OUTPUT) {
                             $this->emit(new ZEvent($this, 'onError', 500, $e->getMessage()));
                             $this->emit(new ZEvent($this, 'on500', $e->getMessage()));
                             if ($this->isMode(self::MODE_DEBUG)) {
                                 trigger_error($e->getMessage(), Zoombi::EXC_ERROR);
                             }
                         } else {
                             $this->getConfig()->setValue('output', false);
                         }
                     }
                 }
             }
         }
     }
     $this->setOutput(ob_get_contents());
     ob_end_clean();
     $flag = $this->getConfig()->getValue('output', false);
     switch (strtolower(trim(strval($flag)))) {
         case '1':
         case 'ok':
         case 'on':
         case 'yes':
         case 'true':
             $this->emit(new ZEvent($this, 'onOutput'));
             $this->outputFlush();
             if ($this->isMode(self::MODE_DEBUG)) {
                 $c = new ZDummyController($this, 'DummyController');
                 $c->renderEach(Zoombi::fromFrameworkDir('Views' . Zoombi::DS . 'view_error.php'), $this->m_errors);
                 unset($c);
             }
             break;
     }
     $this->emit(new ZEvent($this, 'postExecute'));
     if ($this->isMode(self::MODE_DEBUG)) {
         restore_exception_handler();
         restore_error_handler();
         error_reporting($old_errr);
     }
 }