Exemplo n.º 1
0
 public function __construct(ZObject &$parent = null, $a_name = null)
 {
     parent::__construct($parent, $a_name);
     $this->m_config = new ZConfig();
     $this->m_flag = array();
     $this->setLoader(new ZLoader($this));
     $this->setLanguage(new ZLanguage($this));
     $this->setRegistry(new ZRegistry());
     $this->setMode(ZApplication::MODE_NORMAL);
     $this->setDispatcher(Zoombi::getDispatcher($this));
     $this->setPluginManager(new ZPluginManager($this));
 }
Exemplo n.º 2
0
 /**
  * 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();
 }
Exemplo n.º 3
0
 /**
  *
  * @return ZDispatcher
  */
 public function &getDispatcher()
 {
     $d = $this->m_dispatcher ? $this->m_dispatcher : Zoombi::getDispatcher();
     return $d;
 }
Exemplo n.º 4
0
 /**
  * Execute application
  */
 private 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;
             // Process request info
             $req_qry = explode('?', $_SERVER['REQUEST_URI'], 2);
             $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
             $routes = new ZConfig();
             $rs = $this->getConfig()->getValue('routes', null);
             switch (gettype($rs)) {
                 case 'array':
                 case 'object':
                     $routes->setData($rs);
                     break;
                 case 'string':
                     $routes->fromFile($this->fromBaseDir($rs));
                     break;
             }
             $this->getRouter()->setRules($routes->toArray());
             unset($routes);
             $this->emit(new ZEvent($this, 'preRoute', $rvar));
             $this->getRouter()->setRequest($rvar);
             $redir = $this->getRouter()->route($rvar);
             $p = new ZRoute($redir);
             if (!$this->getLoader()->hasModule($p->getModule()) and $p->getModule() != $this->getName()) {
                 $p->push_start($this->getName());
             }
             $fix = $this->fixroute($p);
             $this->getRouter()->setRedirect($fix)->setCurrent($fix)->setForward($fix);
             $this->emit(new ZEvent($this, 'postRoute', $fix));
             $approute = true;
             if ($p->getModule() == $this->getName()) {
                 $p->pop_start();
             }
             $this->route($p, array('approute'));
             $approute = false;
         }
     } 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;
         }
     }
     if (Zoombi::ack($this->getConfig()->getValue('output', false))) {
         ob_start();
         if (Zoombi::ack($this->getConfig()->get('showerror', 'false'))) {
             foreach ($this->m_errors as $e) {
                 $this->showError($e);
             }
         }
         $this->emit(new ZEvent($this, 'onOutput'));
         $cnt = ob_get_contents();
         ob_end_clean();
         ZResponse::getInstance()->setContent(ZResponse::getInstance()->getContent() . $cnt);
     }
     $this->emit(new ZEvent($this, 'postExecute'));
     if ($this->isMode(self::MODE_DEBUG)) {
         restore_error_handler();
         error_reporting($old_errr);
     }
     ZResponse::getInstance()->output();
 }
Exemplo n.º 5
0
 /**
  * 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'));
     register_shutdown_function(array(&$this, '_shutdown'));
     // 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 Zoombi_Event($this, 'preExecute'));
         // Notify befor route start
         if (!$this->getFlag(self::FLAG_NO_ROUTE)) {
             // Attach routing rules
             $routes = new Zoombi_Config();
             $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 Zoombi_Url($this->getConfig()->getValue('baseurl'));
             if (strstr($request, $url->path) == 0) {
                 $request = substr($request, strlen($url->path));
             }
             $router->setRequest($request);
             $this->emit(new Zoombi_Event($this, 'preRoute', $router->getRequest()));
             $r_path = (string) $router->getRequest();
             if (empty($r_path)) {
                 $r_path = '_root_';
             }
             //Do route rewriting
             $redirect = $router->rewrite($r_path);
             $router->setRedirect($redirect);
             $this->emit(new Zoombi_Event($this, 'postRoute', $router->getRedirect()));
             $path = clone $router->getRedirect();
             $s = $path->getSegment(0);
             if ($s == $this->getName() or $s == Zoombi_Module::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 Zoombi_Exception_Controller::EXC_QUIT:
                 $this->getConfig()->setValue('output', false);
                 $this->emit(new Zoombi_Event($this, 'onQuit'));
                 break;
             case Zoombi_Exception_Controller::EXC_QUIT_OUTPUT:
                 $this->getConfig()->setValue('output', true);
                 $this->emit(new Zoombi_Event($this, 'onQuit'));
                 break;
             case Zoombi_Exception_Controller::EXC_AUTH:
                 $this->emit(new Zoombi_Event($this, 'onError', 401, $e));
                 $this->emit(new Zoombi_Event($this, 'on401', $this->getRoute()));
                 $this->route('_401_');
                 break;
             case Zoombi_Exception_Controller::EXC_DENY:
                 $this->emit(new Zoombi_Event($this, 'onError', 403, $e));
                 $this->emit(new Zoombi_Event($this, 'on403', $this->getRoute()));
                 $this->route('_403_');
                 break;
             case Zoombi_Exception_Controller::EXC_LOAD:
             case Zoombi_Exception_Controller::EXC_NO_FILE:
             case Zoombi_Exception_Controller::EXC_ACTION:
                 $this->triggerError($e);
                 $this->emit(new Zoombi_Event($this, 'onError', 404, $e));
                 $this->emit(new Zoombi_Event($this, 'on404', $this->getRoute()));
                 $this->route('_404_');
                 break;
             default:
                 $this->triggerError($e);
                 $this->emit(new Zoombi_Event($this, 'onError', 500, $e));
                 $this->emit(new Zoombi_Event($this, 'on500', $e));
                 $this->route('_500_');
                 break;
         }
     }
     restore_error_handler();
     restore_exception_handler();
     if (Zoombi::ack($this->getConfig()->getValue('output', false))) {
         ob_start();
         if ($this->outputLength() > 0) {
             $this->outputFlush();
         }
         Zoombi_Response::getInstance()->appendContent(ob_get_contents());
         ob_end_clean();
         $this->emit(new Zoombi_Event($this, 'onOutput'));
         Zoombi_Response::getInstance()->output();
     }
     $this->emit(new Zoombi_Event($this, 'postExecute'));
     error_reporting($olderr);
 }
Exemplo n.º 6
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);
     }
 }