Exemplo n.º 1
0
 /**
  * match and extact module, ctrl and act
  *
  * @param Request $request
  * @param Response $response
  * @return boolean
  */
 public function match(Request &$request, Response &$response)
 {
     $sanitizedPath = trim(preg_replace('/\\/{2,}/', '/', $request->getUri()->getPath()));
     $EXECUTOR = Executor::getInstance();
     $AVAILABLE_MODULES = $EXECUTOR->getAvailableModules();
     $DEFAULT_MODULE = $EXECUTOR->getDefaultModule();
     $DEFAULT_CTRL = $EXECUTOR->getDefaultCtrl();
     $DEFAULT_ACT = $EXECUTOR->getDefaultAct();
     if ($this->_rules) {
         foreach ($this->_rules as $routeRule) {
             if ($routeRule instanceof RuleInterface && false !== ($ruleCallback = $routeRule->apply($sanitizedPath, $request, $response))) {
                 if ($routeRule->isTerminable()) {
                     $this->tailing($DEFAULT_MODULE, $DEFAULT_CTRL, $DEFAULT_ACT);
                     $request->setContextAttr('module', $this->_module);
                     $request->setContextAttr('ctrl', $this->_ctrl);
                     $request->setContextAttr('act', $this->_act);
                     return true;
                 }
             }
         }
     }
     $pathArr = explode('/', $sanitizedPath);
     array_shift($pathArr);
     foreach ($pathArr as $k => $segment) {
         if (!$segment) {
             break;
         }
         switch ($k) {
             case 0:
                 if (in_array($segment, $AVAILABLE_MODULES)) {
                     $this->_module = $segment;
                 } else {
                     $this->_module = $DEFAULT_MODULE;
                     $this->_ctrl = $segment;
                 }
                 break;
             case 1:
                 if ($this->_ctrl) {
                     $this->_act = $segment;
                 } else {
                     // ctrl has not been assigned yet!
                     $this->_ctrl = $segment;
                 }
                 break;
             case 2:
                 if ($this->_ctrl) {
                     '' === $this->_act && ($this->_act = $segment);
                 } else {
                     $this->_ctrl = $segment;
                 }
                 break;
             default:
                 return false;
                 break;
         }
     }
     /*var_dump($this->_module);
       var_dump($this->_ctrl);
       var_dump($this->_act);exit;*/
     $this->tailing($DEFAULT_MODULE, $DEFAULT_CTRL, $DEFAULT_ACT);
     $request->setContextAttr('module', $this->_module);
     $request->setContextAttr('ctrl', $this->_ctrl);
     $request->setContextAttr('act', $this->_act);
     return true;
 }