/** * concrete request & response */ public function dispatch() { $target_module = $this->_request->getContextAttr('module'); $target_ctrl = $this->_request->getContextAttr('ctrl'); $target_act = $this->_request->getContextAttr('act'); if (!$target_module || !$target_act || !$target_ctrl) { return false; } // firstly we must confirm the ctrl is reachable $EXEC = Executor::getInstance(); $moduleDir = $EXEC->getModuleDir(); $moduleDir = rtrim($moduleDir, '/\\'); $initFileNamePost = $EXEC->getModuleInitFileName(); $moduleInitFile = $moduleDir . '/Module' . $initFileNamePost; $this->importFileByAbsPath($moduleInitFile); $mvcInitFile = $moduleDir . '/' . $target_module . '/' . ucfirst($target_module) . $initFileNamePost; $this->importFileByAbsPath($mvcInitFile); $moduleBaseNamespace = ltrim(str_replace(APPLICATION_PATH, '', $moduleDir), '/\\'); $tmp = $moduleBaseNamespace ? $moduleBaseNamespace . '\\' : ''; $ctrlClassBaseName = $target_ctrl . $EXEC->getCtrlClassPostfix(); $mvcCtrlClassName = 'application\\' . $tmp . $target_module . '\\ctrl\\' . $ctrlClassBaseName; $actPostFix = $EXEC->getActMethodPostfix(); $actMethodName = $target_act . $actPostFix; $actViewModel = null; try { $actViewModel = $this->executeAct($mvcCtrlClassName, $actMethodName); } catch (DispatchException $e) { // force to ErrorCtrl -> (indexAct) beneath the same dir $actViewModel = $this->handleMvcError(str_replace($ctrlClassBaseName, $EXEC->getErrorCtrlName() . $EXEC->getCtrlClassPostfix(), $mvcCtrlClassName), $EXEC->getErrorActName() . $actPostFix, $e); } // plugin init // Dispatch // plugin terminate }
/** * @param $path * @param Request $request * @param Response $response * @return \Closure|bool */ public function apply(&$path, Request &$request, Response &$response) { list($param_names, $rule_segments) = $this->extractFromRuleStr(); if (empty($param_names)) { return false; } $path_segments = explode(self::SEGMENT_DELIMITER, $path); array_shift($rule_segments); array_shift($path_segments); $contextParams = array(); if (count($rule_segments) == count($path_segments)) { foreach ($rule_segments as $k => $v) { if (false !== strpos($v, self::RULE_PARAM_PREFIX)) { $param_name = array_shift($param_names); $contextParams[$param_name] = $path_segments[$k]; } elseif ($v === $path_segments[$k]) { continue; } else { return false; } } $request->withAttributes($contextParams); $this->performCallback($request, $response); return true; } return false; }
/** * 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; }