Exemplo n.º 1
0
 /**
  * Make route from object
  * @param ZRoute $a_route
  * @return ZRoute
  */
 public function &fromObject($a_route)
 {
     $this->clear();
     if ($a_route instanceof ZRoute) {
         $this->m_segments = $a_route->getSegments();
         $this->m_query->setData($a_route->getQuery());
     }
     return $this;
 }
Exemplo n.º 2
0
 /**
  * Make route from object
  * @param ZRoute $a_route
  * @return ZRoute
  */
 public function &fromObject($a_route)
 {
     $this->clear();
     if ($a_route instanceof ZRoute) {
         $this->m_path = clone $a_route->getPath();
         $this->m_param = clone $a_route->getParams();
         $this->m_query = clone $a_route->getQuery();
         $this->m_mode = $a_route->getMode();
     }
     return $this;
 }
Exemplo n.º 3
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.º 4
0
 /**
  * @return ZRouteMap
  */
 public function routePath($a_path, ZRoute &$a_route = null, $a_throws = false)
 {
     $path = new ZRoutePath();
     $p = new ZRoute($a_path);
     $route =& $a_route;
     if (!$route) {
         $route = new ZRoute();
     }
     $t = $p->getSegment(0);
     $t = empty($t) ? self::DEFAULT_MODULE_NAME : $t;
     if ($this->getLoader()->hasModule($t)) {
         try {
             $route->setModule($t);
             $path->module =& $this->getLoader()->module($t);
         } catch (ZException $e) {
             if ($a_throws) {
                 throw $e;
             }
             return $path;
         }
         return $path->module->routePath($p->pop_start(), $route, $a_throws);
     } else {
         $route->setModule($this->getName());
         $path->module =& $this;
     }
     if (!$path->module) {
         return $path;
     }
     $t = $p->getSegment(0);
     $t = empty($t) ? self::DEFAULT_CONTROLLER_NAME : $t;
     $p->pop_start();
     try {
         $route->setController($t);
         $path->controller =& $path->module->getLoader()->controller($t);
     } catch (ZException $e) {
         if ($a_throws) {
             throw $e;
         }
         return $path;
     }
     if (!$path->controller) {
         return $path;
     }
     $t = $p->getSegment(0);
     $t = empty($t) ? self::DEFAULT_ACTION_NAME : $t;
     $p->pop_start();
     if ($path->controller->hasAction($t)) {
         $route->setAction($t);
         $path->action = $t;
     } else {
         if ($a_throws) {
             throw new ZException('Action ' . $t . ' not found at ' . $path->controller->getName() . ' controller in ' . $path->module->getName() . ' module', ZControllerException::EXC_ACTION);
         }
         return $path;
     }
     foreach ($p->getSegments() as $_p) {
         $route->push($_p);
     }
     $route->getQuery()->setData($p->getQuery());
     unset($p);
     return $path;
 }
Exemplo n.º 5
0
 /**
  * Route path
  * @param string $a_path
  * @return ZModule
  */
 public final function &route($a_path, $a_args = array())
 {
     if (substr($a_path, 0, 1) == Zoombi::SS) {
         return Zoombi::getApplication()->route(substr($a_path, 1));
     }
     if (substr($a_path, 0, 3) == '..' . Zoombi::SS) {
         $parent = $this->getModule();
         if (!$parent) {
             $parent = Zoombi::getApplication();
         }
         return $parent->route(substr($a_path, 3));
     }
     $rewrite = $this->getRouter()->rewrite($a_path);
     $path = new ZRoute($rewrite);
     $epath = new ZRoutePath();
     if ($path->getSegment(0) == $this->getName()) {
         if ($this->getLoader()->hasController($path->getSegment(1)) or $this->getLoader()->hasModule($path->getSegment(1))) {
             $path->pop_start();
         }
     }
     $epath->parents[] = $this;
     $m = $this;
     do {
         $s = $path->getSegment(0);
         $m = $m->getLoader()->module($s, false);
         if ($m) {
             $epath->parents[] = $m;
             $path->pop_start();
         }
     } while ($m);
     $sc = $path->getSegment(0);
     $path->pop_start();
     $mod = $epath->module;
     if (!$mod) {
         $this->triggerError(new ZException('Application router can\'t find module "' . $path->getSegment(0) . '" -> ' . $rewrite, ZControllerException::EXC_LOAD));
     }
     $ctl = $epath->module->getLoader()->controller($sc, false);
     if (!$ctl) {
         return $this->triggerError(new ZException('Application router can\'t find controller "' . $sc . '" in module "' . $this->module->getName() . '" -> ' . $rewrite, ZControllerException::EXC_LOAD));
     }
     $epath->controller = $ctl;
     $sa = $path->getSegment(0);
     $path->pop_start();
     if (!$epath->controller->hasAction($sa)) {
         return $this->triggerError(new ZException('Application router can\'t find action "' . $sa . '" in controller "' . $sc . '" of module "' . $mod->getName() . '" -> ' . $rewrite, ZControllerException::EXC_ACTION));
     }
     $epath->action = $sa;
     ZRequest::getInstance()->setExecPath($epath);
     ZRequest::getInstance()->setRoutePath($epath);
     $old_args = $this->getArgs();
     $old_route = $this->getRoute();
     $old_current = $this->getRouter()->getForward();
     $nr = new ZRoute(implode(Zoombi::SS, array_merge($epath->toArray(), $path->getSegments())) . $path->queryString());
     $this->setArgs($a_args);
     $this->setRoute($nr);
     $this->getRouter()->setForward($nr);
     $epath->controller->requestAction($epath->action, $path->getSegments());
     /* $this->setReturn($epath->controller->getReturn());
     	  $this->setOutput($epath->controller->getOutput()); */
     foreach ($epath->parents as $mod) {
         if ($mod and $mod instanceof ZModule) {
             $mod->setReturn($epath->controller->getReturn());
             $mod->setOutput($epath->controller->getOutput());
         }
     }
     $this->setArgs($old_args);
     $this->setRoute($old_route);
     $this->getRouter()->setForward($old_current);
     return $this;
 }
Exemplo n.º 6
0
 private function &_route($a_path)
 {
     if (substr($a_path, 0, 1) == Zoombi::SS) {
         $rewrite = Zoombi::getApplication()->getName() . Zoombi::SS . substr($a_path, 1);
         $a_path = $rewrite;
     }
     $rewrite = $this->getRouter()->rewrite($a_path);
     if (empty($rewrite)) {
         $rewrite = implode(Zoombi::SS, array(ZModule::DEFAULT_MODULE_NAME, ZModule::DEFAULT_CONTROLLER_NAME, ZModule::DEFAULT_ACTION_NAME));
     }
     $path = new ZRoute($rewrite);
     $epath = new ZRoutePath();
     $epath->parents[] = $this;
     if ($path->getSegment(0) == $this->getName() and $this->getLoader()->hasController($path->getSegment(1))) {
         $path->pop_start();
     }
     $m = $this;
     do {
         $s = $path->getSegment(0);
         $m = $m->getLoader()->module($s);
         if ($m) {
             $epath->parents[] = $m;
             $path->pop_start();
         }
     } while ($m);
     $mod = $epath->module;
     if (!$mod) {
         throw new ZException('Application router can\'t find module start', ZControllerException::EXC_LOAD);
     }
     $sc = $path->getSegment(0);
     $path->pop_start();
     $ctl = $epath->module->getLoader()->controller($sc);
     if (!$ctl) {
         $ctl = $epath->module->getLoader()->controller(ZModule::DEFAULT_CONTROLLER_NAME, false);
     }
     if (!$ctl) {
         throw new ZException('Application router can\'t find controller "' . $sc . '" in module "' . $mod->getName() . '"', ZControllerException::EXC_LOAD);
     }
     $epath->controller = $ctl;
     $sa = $path->getSegment(0);
     $path->pop_start();
     if (empty($sa)) {
         $sa = ZModule::DEFAULT_ACTION_NAME;
     }
     if (!$ctl->hasAction($sa)) {
         throw new ZException('Application router can\'t find action "' . $sa . '" in controller "' . $sc . '" of module "' . $mod->getName() . '"', ZControllerException::EXC_ACTION);
     }
     $action = $mod->getConfig()->getValue('controller.action_prefix', ZModule::DEFAULT_CONTROLLER_METHOD_PREFIX) . $sa;
     if (substr($action, 0, 1) == '_') {
         throw new ZException('External routes to private action requies is disallowed', ZControllerException::EXC_LOAD);
     }
     $epath->action = $sa;
     $_r = implode(Zoombi::SS, array_merge($epath->toArray(), $path->getSegments())) . $path->queryString();
     return $_r;
 }
Exemplo n.º 7
0
 private function _processRoute(ZRoute &$a_route, &$a_code, &$a_message)
 {
     $ctl = null;
     $mod = null;
     if ($a_route->module) {
         $mod = new ZModule($this, $a_route->module . 'Module');
         $mod->setBaseDir($this->fromBaseDir(Zoombi::config('path.module', 'module')) . Zoombi::DS . $a_route->module);
     }
     if (!$a_route->controller) {
         $a_route->controller = $this->getConfig()->getValue('controller.default_name', 'index');
     }
     if (!$a_route->action) {
         $a_route->action = $this->getConfig()->getValue('controller.default_action', 'index');
     }
     if (!$mod) {
         $mod =& $this;
     }
     try {
         $ctl = $mod->load->controller($a_route->getController());
     } catch (ZControllerException $e) {
         $a_code = $e->getCode();
         $a_message = $e->getMessage();
         return false;
     }
     if (!$ctl) {
         return false;
     }
     $this->setController($ctl);
     return true;
 }
Exemplo n.º 8
0
 /**
  * Route path
  * @param string $a_path
  * @return ZModule
  */
 public final function &route($a_path, $a_args = array())
 {
     Zoombi::getApplication()->m_route_deep++;
     switch (gettype($a_path)) {
         case 'string':
             if (substr($a_path, 0, 1) == Zoombi::SS) {
                 return $this->_reRoute(Zoombi::getApplication(), substr($a_path, 1), $a_args);
             }
             break;
         case 'object':
             if (!$a_path instanceof ZRoute) {
                 return $this;
             }
             break;
         default:
             return $this;
             break;
     }
     $p = new ZRoute($a_path);
     $m = $p->getModule();
     if (empty($m)) {
         $m = $this->getConfig()->getValue('module.default_name', self::DEFAULT_MODULE_NAME);
     }
     $this->emit(new ZEvent($this, 'preModule', $m));
     if ($this->getLoader()->hasModule($m)) {
         $mod = $this->getLoader()->module($m);
         if (!$mod) {
             throw new ZModuleException('Bad module');
         }
         $this->emit(new ZEvent($this, 'postModule', $mod));
         return $this->_reRoute($mod, $p->pop_start(), $a_args);
     }
     $this->emit(new ZEvent($this, 'postModule', $this));
     $p->push_start($this->getName());
     $route = $p;
     $c = $route->getController();
     $a = $route->getAction();
     if (empty($c)) {
         $route->setController($this->getConfig()->getValue('controller.default_name', self::DEFAULT_CONTROLLER_NAME));
     }
     if (empty($a)) {
         $route->setAction($this->getConfig()->getValue('controller.default_action', self::DEFAULT_ACTION_NAME));
     }
     $c = $route->getController();
     $a = $route->getAction();
     $this->emit(new ZEvent($this, 'preController', $c));
     $ctl = null;
     $ctl = $this->getLoader()->controller($c);
     if (!$ctl) {
         throw new ZModuleException('Module "' . $this->getName() . '" can\'t find controller "' . $c . '".');
     }
     $this->emit(new ZEvent($this, 'postController', $c, $ctl));
     if (!$ctl->hasAction($a)) {
         throw new ZModuleException('Module "' . $this->getName() . '" can\'t find controller "' . $c . '" action "' . $a . '".');
     }
     $this->emit(new ZEvent($this, 'preAction', $a));
     $this->setRoute($route)->setArgs($a_args);
     $this->m_return = $ctl->requestAction($a, $this->getRoute()->getParamsArray(), $this->m_output);
     return $this->emit(new ZEvent($this, 'postAction', $a));
 }
Exemplo n.º 9
0
 private function _processRoute(ZRoute &$a_route, &$a_code, &$a_message)
 {
     $ctl = null;
     if (!$a_route->controller) {
         $a_route->controller = $this->m_config->getValue('controller.default_name', 'index');
     }
     if (!$a_route->action) {
         $a_route->action = $this->m_config->getValue('controller.default_action', 'index');
     }
     try {
         $ctl = ZLoader::controller($a_route->getController());
     } catch (ZControllerException $e) {
         $a_code = $e->getCode();
         $a_message = $e->getMessage();
         return false;
     }
     if (!$ctl) {
         return false;
     }
     $this->setController($ctl);
     return true;
 }