/** *执行相应的Action *$packageName 包名 *$actionName Action名 *$arrData 用户请求的数据 *$preAction 前一个Action */ public function doAction($packageName, $actionName, $arrData, $preAction = null) { $arrPackages = array(); $arrIntercepters = array(); $arrCustomerConfig = array(); //载入默认的mvc配置文件 if (file_exists('china_mvc.xml')) { $cofigXml = @simplexml_load_file("china_mvc.xml"); } else { throw new FileLoadException('china_mvc.xml'); } //解析xml $this->parseXML($cofigXml, true, $arrPackages, $arrIntercepters, $arrCustomerConfig); $this->parseXML($cofigXml, false, $arrPackages, $arrIntercepters, $arrCustomerConfig); $package = null; $hasFind = false; foreach ($arrPackages as $basePackage) { if ($basePackage["name"] == $packageName) { $package = $basePackage; $hasFind = true; break; } else { } } if (!$hasFind) { foreach ($arrPackages as $basePackage) { if (empty($basePackage["name"])) { $package = $basePackage; break; } else { } } } else { } if (!isset($package)) { throw new RequestPathException(); } $action = null; if (!empty($package->action)) { foreach ($package->action as $baseAction) { if ($baseAction["name"] == $actionName) { $action = $baseAction; break; } else { } } } if (!isset($action)) { throw new RequestPathException(); } $phpPath = $action["php"]; $arrPhpPath = explode("/", $phpPath); $phpName = $arrPhpPath[count($arrPhpPath) - 1]; $className = $action["class"]; if (empty($className)) { $className = $phpName; } $functionName = $action["function"]; if (empty($functionName)) { $functionName = "index"; } $className = strval($className); $languagePacksPath = DISK_ROOT . substr($phpPath, 0, Util::strLastPos($phpPath, '/')); $phpPath = DISK_ROOT . $phpPath . ".php"; if (file_exists($phpPath)) { @(require_once $phpPath); } else { throw new PhpIncludeException($phpPath); } if (class_exists($className)) { $objAction = @new $className(); } else { throw new ClassLoadException($phpPath . '------->' . $className); } //载入全局信息資源 $arrGlobalLanguagePacks = array(); foreach ($arrCustomerConfig as $customerConfig) { if (empty($customerConfig->globalLanguagePacks) || empty($customerConfig->globalLanguagePacks->globalLanguagePack)) { continue; } foreach ($customerConfig->globalLanguagePacks->globalLanguagePack as $globalLanguagePack) { if (empty($globalLanguagePack['name'])) { continue; } $arrGlobalLanguagePacks[] = $globalLanguagePack; $globalLanguagePackPath = $globalLanguagePack['path']; if (empty($globalLanguagePackPath)) { $globalLanguagePackPath = ""; } if (!file_exists(DISK_ROOT . $globalLanguagePackPath)) { throw new DirLoadException(DISK_ROOT . $globalLanguagePackPath); } $arrLanguagePacks = Util::traversalDir(DISK_ROOT . $globalLanguagePackPath); $languageFileExit = false; foreach ($arrLanguagePacks as $languagePack) { $languagePackName = $languagePack; if (Util::strStartWith($languagePack, trim($globalLanguagePack['name'])) && Util::strEndWith($languagePack, '.lgp')) { $languageFileExit = true; $languagePack = substr($languagePack, 0, -4); $languageLeval = 'global_'; $languageSort = ''; $languageCountry = ''; $arrTemp = explode('_', $languagePack); if (count($arrTemp) == 2) { $languageSort = $arrTemp[1]; } elseif (count($arrTemp) == 3) { $languageSort = $arrTemp[1]; $languageCountry = $arrTemp[2]; } else { } $languageProperties = new Properties(DISK_ROOT . $globalLanguagePackPath . '/' . $languagePackName); $arrLanguageData = $languageProperties->loadProperties(); if (!empty($languageSort)) { $langFlag = $languageLeval . $languageSort; if (!empty($languageCountry)) { $langFlag .= '_' . $languageCountry; } else { } } else { $langFlag = $languageLeval; } $objAction->addLanguagePack(array($langFlag => $arrLanguageData)); } } if (!$languageFileExit) { throw new FileLoadException(DISK_ROOT . $globalLanguagePackPath . $globalLanguagePack['name'] . '.lgp'); } } } //载入分级别的国际化信息 分为两类 :类级别,包级别 $arrLanguagePacks = Util::traversalDir($languagePacksPath); foreach ($arrLanguagePacks as $languagePack) { $languagePackName = $languagePack; if (Util::strEndWith($languagePack, '.lgp')) { $languagePack = substr($languagePack, 0, -4); $languageLeval = ''; $languageSort = ''; $languageCountry = ''; if (Util::strStartWith($languagePack, $className)) { $languageLeval = 'class_'; $arrTemp = explode('_', $languagePack); if (count($arrTemp) == 2) { $languageSort = $arrTemp[1]; } elseif (count($arrTemp) == 3) { $languageSort = $arrTemp[1]; $languageCountry = $arrTemp[2]; } else { } } elseif (Util::strStartWith($languagePack, 'package')) { $languageLeval = 'package_'; $arrTemp = explode('_', $languagePack); if (count($arrTemp) == 2) { $languageSort = $arrTemp[1]; } else { if (count($arrTemp) == 3) { $languageSort = $arrTemp[1]; $languageCountry = $arrTemp[2]; } else { } } } if (!empty($languageLeval)) { $languageProperties = new Properties($languagePacksPath . '/' . $languagePackName); $arrLanguageData = $languageProperties->loadProperties(); if (!empty($languageSort)) { $langFlag = $languageLeval . $languageSort; if (!empty($languageCountry)) { $langFlag .= '_' . $languageCountry; } else { } } else { $langFlag = $languageLeval; } $objAction->addLanguagePack(array($langFlag => $arrLanguageData)); } else { } } } //加入按照一般请求方式提交来的数据 if ($_SERVER['REQUEST_METHOD'] == GET) { $arrData += $_GET; } else { $arrData += $_POST; } //将请求数据自动赋值到相应的action属性中 Action需提供setXXX()方法 foreach ($arrData as $key => $value) { $attrFunctionName = strval('set' . ucfirst($key)); $objAction->{$attrFunctionName}($value); } if ($preAction != null) { //加入上一个action传递的数据 $objAction->addChainData($preAction->getChainData()); } else { } $functionName = strval($functionName); //执行相应的拦截器 $actionIntercepters = $action->intercepterRef; $intercepterExit = false; if (!empty($actionIntercepters)) { $arrObjIntercepters = array(); $intercepterFlag = false; foreach ($actionIntercepters as $actionIntercepter) { $intercepterName = $actionIntercepter["name"]; if (empty($intercepterName)) { continue; } foreach ($arrIntercepters as $intercepter) { if (trim($intercepterName) == trim($intercepter["name"])) { $intercepterExit = true; $intercepterPhpPath = $intercepter["php"]; $arrPhpPath = explode("/", $intercepterPhpPath); $intercepterPhpName = $arrPhpPath[count($arrPhpPath) - 1]; $intercepterClassName = $intercepter["class"]; if (empty($intercepterClassName)) { $intercepterClassName = $intercepterPhpName; } $intercepterPhpPath = DISK_ROOT . $intercepterPhpPath . ".php"; if (file_exists($intercepterPhpPath)) { @(require_once $intercepterPhpPath); } else { throw new PhpIncludeException($intercepterPhpPath); } $intercepterClassName = strval($intercepterClassName); if (class_exists($intercepterClassName)) { $objIntercepter = @new $intercepterClassName(); } else { throw new ClassLoadException($intercepterPhpPath . '------->' . $intercepterClassName); } $arrObjIntercepters[] = $objIntercepter; $invokeResult = $objIntercepter->beforeDoAction(&$objAction, $arrData); if (empty($invokeResult) || $invokeResult === false) { continue; } else { $intercepterFlag = true; $resultName = $invokeResult; break; } } } if (!$intercepterExit) { throw new IntercepterPathException($intercepterName); } } if ($intercepterExit && !$intercepterFlag) { if (!empty($action->validater)) { foreach ($action->validater as $validateXML) { $this->validateByXML($validateXML, $arrCustomerConfig, $objAction, $arrData); } } if ($functionName == 'index') { $validateFunctionName = 'validate'; } else { $validateFunctionName = 'validate' . ucfirst($functionName); } $objAction->{$validateFunctionName}(); if ($objAction->hasErrMsg()) { $resultName = 'validateError'; } else { $resultName = $objAction->{$functionName}(); } foreach ($arrObjIntercepters as $objIntercepters) { $invokeResult = $objIntercepter->afterDoAction(&$objAction, $arrData, $invokeResult); if (empty($invokeResult) || $invokeResult === false) { continue; } else { $resultName = $invokeResult; break; } } } } else { if (!empty($action->validater)) { foreach ($action->validater as $validateXML) { $this->validateByXML($validateXML, $arrCustomerConfig, $objAction, $arrData); } } if ($functionName == 'index') { $validateFunctionName = 'validate'; } else { $validateFunctionName = 'validate' . ucfirst($functionName); } $objAction->{$validateFunctionName}(); if ($objAction->hasErrMsg()) { $resultName = 'validateError'; } else { $resultName = $objAction->{$functionName}(); } } $result = NULL; foreach ($action->result as $baseResult) { $resultName = trim($resultName); if (trim($baseResult["name"], "\n\t") == $resultName) { $result = $baseResult; break; } else { } } if ($result == null) { throw new ResultException($resultName); } $arrResultParams = array(); $arrResultParams['value'] = trim(strval($result), "\n\t"); $arrResultParams['currentPackage'] = strval($packageName); foreach ($result->attributes() as $key => $value) { $arrResultParams[$key] = strval($value); } $resultType = $result["type"]; $resultValue = $result; if (empty($resultType)) { $resultType = "dispatcher"; } else { } $arrResultTypes = array(); foreach ($arrCustomerConfig as $customerConfig) { if (!empty($customerConfig->resultTypes->registResultType)) { foreach ($customerConfig->resultTypes->registResultType as $registResultType) { $arrResultTypes[] = $registResultType; } } } foreach ($arrResultTypes as $registResultType) { if (trim($registResultType['name']) == trim($resultType)) { $resultPhpPath = $registResultType['php']; $arrResultPhpPath = explode("/", $resultPhpPath); $resultPhpName = $arrResultPhpPath[count($arrResultPhpPath) - 1]; $className = $registResultType["class"]; if (empty($className)) { $className = $resultPhpName; } $className = strval($className); $resultPhpPath = DISK_ROOT . $resultPhpPath . ".php"; if (file_exists($resultPhpPath)) { @(require_once $resultPhpPath); } else { throw new PhpIncludeException($resultPhpPath); } $resultTypeClassName = strval($registResultType['class']); if (class_exists($resultTypeClassName)) { $result = @new $resultTypeClassName(); } else { throw new ClassLoadException($resultPhpPath . '------->' . $resultTypeClassName); } $result->doResult($objAction, $arrResultParams, $arrData, $this); break; } } }
<?php require_once 'Util.php'; require_once 'Properties.php'; $configProperties = new Properties(realpath('.') . '/config/project.cfg'); $arrConfigDatas = $configProperties->loadProperties(); foreach ($arrConfigDatas as $paramName => $configData) { $paramName = strtoupper(trim($paramName)); $configData = trim($configData); define($paramName, $configData); }