/** * 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 }
<?php \Hydrogen\Load\Loader::getInstance()->import('application/module/ModuleInit.php'); $EXE = \Hydrogen\Application\Execute\Executor::getInstance(); $baseModuleNS = 'application\\module\\front'; foreach (array('ctrl') as $registry) { $EXE->setNamespaceDir($baseModuleNS . '\\' . $registry, APPLICATION_PATH . '/module/front/' . $registry, true); }
/** * 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; }