/**
  * Execute application
  */
 public final function execute()
 {
     $_errtype = E_ALL;
     // Set application plugin manager listen global event dispatcher
     $this->getPluginManager()->setTarget(Zoombi::getDispatcher());
     $this->getDispatcher()->connect('_triggerError', array($this, '_error_trigger'));
     try {
         // Notify for start execution
         $this->emit(new ZEvent($this, 'preExecute'));
         // Attach error and exception handlers if applicatio execution mode is 'debug'
         if ($this->isMode(self::MODE_DEBUG)) {
             $old_errr = error_reporting($_errtype);
             set_error_handler(array($this, '_error_handler'), $_errtype);
             //set_exception_handler(array( $this, '_exception_handler' ));
         }
         // Notify befor route start
         //$this->emit(new ZEvent($this, 'preRoute'));
         if (!$this->getFlag(self::FLAG_NO_ROUTE)) {
             $request = null;
             $rvar = $_SERVER['REQUEST_URI'];
             // Attach routing rules to router
             $routes = new ZConfig();
             $router = $this->getRouter();
             $rs = $this->getConfig()->getValue('routes', null);
             switch (gettype($rs)) {
                 case 'array':
                 case 'object':
                     $routes->setData($rs);
                     break;
                 case 'string':
                     $cf = $rs;
                     if (!file_exists($cf)) {
                         $cf = $this->fromBaseDir($cf);
                     }
                     if (file_exists($cf)) {
                         $routes->fromFile($cf);
                     }
                     break;
             }
             $ra = $routes->toArray();
             $router->setRules($ra);
             unset($routes);
             $router->setRequest($rvar);
             $this->emit(new ZEvent($this, 'preRoute', $router->getRequest()));
             $redir = $router->rewrite($router->getRequest());
             $router->setRedirect($redir)->setCurrent($redir)->setForward($redir);
             $this->emit(new ZEvent($this, 'postRoute', $redir));
             $path = $router->getRedirect();
             if ($path->getSegment(0) == $this->getName()) {
                 $path->pop_start();
             }
             $curr = null;
             $fpath = $this->routePath($path, $curr, true);
             $router->setCurrent($curr)->setForward($curr);
             if ($fpath->isInvalid()) {
                 throw new ZException('Page not found', ZControllerException::EXC_ACTION);
             }
             if ($curr->getSegment(0) == $this->getName()) {
                 $curr->pop_start();
             }
             $this->route($curr);
         }
     } catch (ZException $e) {
         switch ($e->getCode()) {
             case ZControllerException::EXC_QUIT:
                 $this->getConfig()->setValue('output', false);
                 break;
             case ZControllerException::EXC_QUIT_OUTPUT:
                 $this->getConfig()->setValue('output', true);
                 break;
             case ZControllerException::EXC_LOAD:
             case ZControllerException::EXC_NO_FILE:
             case ZControllerException::EXC_ACTION:
                 $this->emit(new ZEvent($this, 'onError', 404, $e));
                 $this->emit(new ZEvent($this, 'on404', $this->getRoute()));
                 break;
             case ZControllerException::EXC_DENY:
                 $this->emit(new ZEvent($this, 'onError', 403, $e));
                 $this->emit(new ZEvent($this, 'on403', $this->getRoute()));
                 break;
             case ZControllerException::EXC_AUTH:
                 $this->emit(new ZEvent($this, 'onError', 401, $e));
                 $this->emit(new ZEvent($this, 'on401', $this->getRoute()));
                 break;
             default:
                 $this->emit(new ZEvent($this, 'onError', 500, $e));
                 $this->emit(new ZEvent($this, 'on500', $e));
                 break;
         }
     }
     $responce = ZResponse::getInstance();
     if (Zoombi::ack($this->getConfig()->getValue('output', false))) {
         ob_start();
         $this->emit(new ZEvent($this, 'onOutput'));
         if ($this->outputLength() > 0) {
             $this->outputFlush();
         }
         if (Zoombi::ack($this->getConfig()->get('showtrace', false))) {
             ZDebug::printTraces();
         }
         if (Zoombi::ack($this->getConfig()->get('showerror', false))) {
             $this->showErrors();
         }
         $cnt = ob_get_contents();
         ob_end_clean();
         $responce->appendContent($cnt);
     }
     $this->emit(new ZEvent($this, 'postExecute'));
     if ($this->isMode(self::MODE_DEBUG)) {
         restore_error_handler();
         error_reporting($old_errr);
     }
     $responce->output();
 }
 /**
  * Execute application
  */
 public final function execute($a_route_url = null)
 {
     // Attach error and exception handlers
     $olderr = error_reporting(E_ALL);
     set_error_handler(array($this, '_error_handler'));
     set_exception_handler(array($this, '_exception_handler'));
     // Set application plugin manager listen global event dispatcher
     $this->getPluginManager()->setTarget(Zoombi::getDispatcher());
     $this->getDispatcher()->connect('_triggerError', array($this, '_error_trigger'));
     try {
         // Notify for start execution
         $this->emit(new ZEvent($this, 'preExecute'));
         // Notify befor route start
         if (!$this->getFlag(self::FLAG_NO_ROUTE)) {
             // Attach routing rules to router
             $routes = new ZConfig();
             $router = $this->getRouter();
             $rs = $this->getConfig()->getValue('routes', null);
             switch (gettype($rs)) {
                 case 'array':
                 case 'object':
                     $routes->setData($rs);
                     break;
                 case 'string':
                     $cf = $rs;
                     if (!file_exists($cf)) {
                         $cf = $this->fromBaseDir($cf);
                     }
                     if (file_exists($cf)) {
                         $routes->fromFile($cf);
                     }
                     break;
             }
             $ra = $routes->toArray();
             $router->setRules($ra);
             unset($routes);
             $request = $a_route_url ? $a_route_url : $_SERVER['REQUEST_URI'];
             $url = new ZUrl($this->getConfig()->getValue('baseurl'));
             if (strstr($request, $url->path) == 0) {
                 $request = substr($request, strlen($url->path));
             }
             $router->setRequest($request);
             $this->emit(new ZEvent($this, 'preRoute', $router->getRequest()));
             $redirect = $router->rewrite((string) $router->getRequest());
             $router->setRedirect($redirect);
             $this->emit(new ZEvent($this, 'postRoute', $router->getRedirect()));
             $path = clone $router->getRedirect();
             $s = $path->getSegment(0);
             if ($s == $this->getName() or $s == ZModule::DEFAULT_MODULE_NAME) {
                 if (!$this->getLoader()->hasController($s)) {
                     $path->pop_start();
                 }
             }
             $this->exec_route = $this->_route((string) $path);
             if ($this->exec_route) {
                 $this->getRouter()->setCurrent($this->exec_route);
                 $this->route($this->exec_route);
             }
         }
     } catch (Exception $e) {
         switch ($e->getCode()) {
             case ZControllerException::EXC_QUIT:
                 $this->getConfig()->setValue('output', false);
                 $this->emit(new ZEvent($this, 'onQuit'));
                 break;
             case ZControllerException::EXC_QUIT_OUTPUT:
                 $this->getConfig()->setValue('output', true);
                 $this->emit(new ZEvent($this, 'onQuit'));
                 break;
             case ZControllerException::EXC_AUTH:
                 $this->emit(new ZEvent($this, 'onError', 401, $e));
                 $this->emit(new ZEvent($this, 'on401', $this->getRoute()));
                 break;
             case ZControllerException::EXC_DENY:
                 $this->emit(new ZEvent($this, 'onError', 403, $e));
                 $this->emit(new ZEvent($this, 'on403', $this->getRoute()));
                 break;
             case ZControllerException::EXC_LOAD:
             case ZControllerException::EXC_NO_FILE:
             case ZControllerException::EXC_ACTION:
                 $this->emit(new ZEvent($this, 'onError', 404, $e));
                 $this->emit(new ZEvent($this, 'on404', $this->getRoute()));
                 break;
             default:
                 $this->emit(new ZEvent($this, 'onError', 500, $e));
                 $this->emit(new ZEvent($this, 'on500', $e));
                 break;
         }
     }
     restore_error_handler();
     restore_exception_handler();
     if (Zoombi::ack($this->getConfig()->getValue('output', false))) {
         ob_start();
         if ($this->outputLength() > 0) {
             $this->outputFlush();
         }
         if (Zoombi::ack($this->getConfig()->get('showtrace', false))) {
             ZDebug::printTraces();
         }
         if (Zoombi::ack($this->getConfig()->get('showerror', false))) {
             $this->showErrors();
         }
         ZResponse::getInstance()->appendContent(ob_get_contents());
         ob_end_clean();
         $this->emit(new ZEvent($this, 'onOutput'));
         ZResponse::getInstance()->output();
     }
     $this->emit(new ZEvent($this, 'postExecute'));
     error_reporting($olderr);
 }