/** * Set the model file of one module. If there's an extension file, merge it with the main model file. * * @param string $moduleName the module name * @static * @access public * @return string the model file */ public static function setModelFile($moduleName) { global $app; /* Set the main model file and extension and hook pathes and files. */ $mainModelFile = $app->getModulePath($moduleName) . 'model.php'; $modelExtPath = $app->getModuleExtPath($moduleName, 'model'); $modelHookPath = $modelExtPath . 'hook/'; $extFiles = helper::ls($modelExtPath, '.php'); $hookFiles = helper::ls($modelHookPath, '.php'); /* If no extension files and no hook files, return the main file directly. */ if (empty($extFiles) and empty($hookFiles)) { return $mainModelFile; } /* Else, judge whether needed update or not .*/ $needUpdate = false; $mergedModelFile = $app->getTmpRoot() . 'model' . $app->getPathFix() . $moduleName . '.php'; $lastTime = file_exists($mergedModelFile) ? filemtime($mergedModelFile) : 0; while (!$needUpdate) { foreach ($extFiles as $extFile) { if (filemtime($extFile) > $lastTime) { break 2; } } foreach ($hookFiles as $hookFile) { if (filemtime($hookFile) > $lastTime) { break 2; } } if (is_dir($modelExtPath) and filemtime($modelExtPath) > $lastTime) { break; } if (is_dir($modelHookPath) and filemtime($modelHookPath) > $lastTime) { break; } if (filemtime($mainModelFile) > $lastTime) { break; } return $mergedModelFile; } /* If loaded zend opcache module, turn off cache when create tmp model file to avoid the conflics. */ if (extension_loaded('Zend OPcache')) { ini_set('opcache.enable', 0); } /* Update the cache file. */ $modelClass = $moduleName . 'Model'; $extModelClass = 'ext' . $modelClass; $extTmpModelClass = 'tmpExt' . $modelClass; $modelLines = "<?php\n"; $modelLines .= "helper::import('{$mainModelFile}');\n"; $modelLines .= "class {$extTmpModelClass} extends {$modelClass} \n{\n"; /* Cycle all the extension files. */ foreach ($extFiles as $extFile) { $extLines = self::removeTagsOfPHP($extFile); $modelLines .= $extLines . "\n"; } /* Create the merged model file and import it. */ $replaceMark = '//**//'; // This mark is for replacing code using. $modelLines .= "{$replaceMark}\n}"; /* Unset conflic function for model. */ preg_match_all('/.* function\\s+(\\w+)\\s*\\(.*\\)[^\\{]*\\{/Ui', $modelLines, $functions); $functions = $functions[1]; $conflics = array_count_values($functions); foreach ($conflics as $functionName => $count) { if ($count <= 1) { unset($conflics[$functionName]); } } if ($conflics) { $modelLines = explode("\n", $modelLines); $startDel = false; foreach ($modelLines as $line => $code) { if ($startDel and preg_match('/.* function\\s+(\\w+)\\s*\\(.*\\)/Ui', $code)) { $startDel = false; } if ($startDel) { unset($modelLines[$line]); } else { foreach ($conflics as $functionName => $count) { if ($count <= 1) { continue; } if (preg_match('/.* function\\s+' . $functionName . '\\s*\\(.*\\)/Ui', $code)) { $conflics[$functionName] = $count - 1; $startDel = true; unset($modelLines[$line]); } } } } $modelLines = join("\n", $modelLines); } $tmpMergedModelFile = $app->getTmpRoot() . 'model' . $app->getPathFix() . 'tmp.' . $moduleName . '.php'; if (!@file_put_contents($tmpMergedModelFile, $modelLines)) { die("ERROR: {$tmpMergedModelFile} not writable, please make sure the " . dirname($tmpMergedModelFile) . ' directory exists and writable'); } if (!class_exists($extTmpModelClass)) { include $tmpMergedModelFile; } /* Get hook codes need to merge. */ $hookCodes = array(); foreach ($hookFiles as $hookFile) { $fileName = baseName($hookFile); list($method) = explode('.', $fileName); $hookCodes[$method][] = self::removeTagsOfPHP($hookFile); } /* Cycle the hook methods and merge hook codes. */ $hookedMethods = array_keys($hookCodes); $mainModelCodes = file($mainModelFile); $mergedModelCodes = file($tmpMergedModelFile); foreach ($hookedMethods as $method) { /* Reflection the hooked method to get it's defined position. */ $methodRelfection = new reflectionMethod($extTmpModelClass, $method); $definedFile = $methodRelfection->getFileName(); $startLine = $methodRelfection->getStartLine() . ' '; $endLine = $methodRelfection->getEndLine() . ' '; /* Merge hook codes. */ $oldCodes = $definedFile == $tmpMergedModelFile ? $mergedModelCodes : $mainModelCodes; $oldCodes = join("", array_slice($oldCodes, $startLine - 1, $endLine - $startLine + 1)); $openBrace = strpos($oldCodes, '{'); $newCodes = substr($oldCodes, 0, $openBrace + 1) . "\n" . join("\n", $hookCodes[$method]) . substr($oldCodes, $openBrace + 1); /* Replace it. */ if ($definedFile == $tmpMergedModelFile) { $modelLines = str_replace($oldCodes, $newCodes, $modelLines); } else { $modelLines = str_replace($replaceMark, $newCodes . "\n{$replaceMark}", $modelLines); } } unlink($tmpMergedModelFile); /* Save it. */ $modelLines = str_replace($extTmpModelClass, $extModelClass, $modelLines); file_put_contents($mergedModelFile, $modelLines); return $mergedModelFile; }
/** * Load lang and return it as the global lang object. * * @param string $moduleName the module name * @param string $appName the app name * @access public * @return bool|ojbect the lang object or false. */ public function loadLang($moduleName, $appName = '') { if ($moduleName == 'common' and $appName == '') { $this->loadLang('common', 'sys'); } $modulePath = $this->getModulePath($appName, $moduleName); $mainLangFile = $modulePath . 'lang' . DS . $this->clientLang . '.php'; $extLangPath = $this->getModuleExtPath($appName, $moduleName, 'lang'); $extLangFiles = helper::ls($extLangPath . $this->clientLang, '.php'); /* Set the files to includ. */ if (!is_file($mainLangFile)) { if (empty($extLangFiles)) { return false; } // also no extension file. $langFiles = $extLangFiles; } else { $langFiles = array_merge(array($mainLangFile), $extLangFiles); } global $lang; if (!is_object($lang)) { $lang = new language(); } static $loadedLangs = array(); foreach ($langFiles as $langFile) { if (in_array($langFile, $loadedLangs)) { continue; } include $langFile; $loadedLangs[] = $langFile; } /* Merge from the db lang. */ if (empty($appName)) { $appName = $this->appName; } if (isset($lang->db->custom[$appName][$moduleName])) { foreach ($lang->db->custom[$appName][$moduleName] as $section => $fields) { foreach ($fields as $key => $value) { if ($moduleName == 'common') { unset($lang->{$section}[$key]); $lang->{$section}[$key] = $value; } else { unset($lang->{$moduleName}->{$section}[$key]); $lang->{$moduleName}->{$section}[$key] = $value; } } } } $this->lang = $lang; return $lang; }
/** * Set the model file of one module. If there's an extension file, merge it with the main model file. * * @param string $moduleName the module name * @static * @access public * @return string the model file */ public static function setModelFile($moduleName) { global $app; /* Set the main model file and extension path and files. */ $mainModelFile = $app->getModulePath($moduleName) . 'model.php'; $modelExtPaths = $app->getModuleExtPath($moduleName, 'model'); $extFiles = array(); foreach ($modelExtPaths as $modelExtPath) { $extFiles = array_merge($extFiles, helper::ls($modelExtPath, '.php')); } /* If no extension file, return the main file directly. */ if (empty($extFiles)) { return $mainModelFile; } /* Else, judge whether needed update or not .*/ $mergedModelDir = $app->getTmpRoot() . 'model' . DS . $app->siteCode[0] . DS . $app->siteCode . DS; $mergedModelFile = $mergedModelDir . $app->siteCode . '.' . $moduleName . '.php'; $needUpdate = false; $lastTime = file_exists($mergedModelFile) ? filemtime($mergedModelFile) : 0; if (!is_dir($mergedModelDir)) { mkdir($mergedModelDir, 0755, true); } foreach ($extFiles as $extFile) { if (filemtime($extFile) > $lastTime) { $needUpdate = true; break; } } if (filemtime($mainModelFile) > $lastTime) { $needUpdate = true; } /* If need'nt update, return the cache file. */ if (!$needUpdate) { return $mergedModelFile; } /* Update the cache file. */ if ($needUpdate) { $modelClass = $moduleName . 'Model'; $extModelClass = 'ext' . $modelClass; $modelLines = "<?php\n"; $modelLines .= "helper::import('{$mainModelFile}');\n"; $modelLines .= "class {$extModelClass} extends {$modelClass} \n{\n"; /* Cycle all the extension files. */ foreach ($extFiles as $extFile) { $extLines = trim(file_get_contents($extFile)); if (strpos($extLines, '<?php') !== false) { $extLines = ltrim($extLines, "<?php"); } if (strpos($extLines, "?>") !== false) { $extLines = rtrim($extLines, '?>'); } $modelLines .= $extLines . "\n"; } /* Create the merged model file. */ $modelLines .= "}"; /* Unset conflic function for model. */ preg_match_all('/.* function\\s+(\\w+)\\s*\\(.*\\)[^\\{]*\\{/Ui', $modelLines, $functions); $functions = $functions[1]; $conflics = array_count_values($functions); foreach ($conflics as $functionName => $count) { if ($count <= 1) { unset($conflics[$functionName]); } } if ($conflics) { $modelLines = explode("\n", $modelLines); $startDel = false; foreach ($modelLines as $line => $code) { if ($startDel and preg_match('/.* function\\s+(\\w+)\\s*\\(.*\\)/Ui', $code)) { $startDel = false; } if ($startDel) { unset($modelLines[$line]); } else { foreach ($conflics as $functionName => $count) { if ($count <= 1) { continue; } if (preg_match('/.* function\\s+' . $functionName . '\\s*\\(.*\\)/Ui', $code)) { $conflics[$functionName] = $count - 1; $startDel = true; unset($modelLines[$line]); } } } } $modelLines = join("\n", $modelLines); } file_put_contents($mergedModelFile, $modelLines); return $mergedModelFile; } }
/** * Load lang and return it as the global lang object. * * @param string $moduleName the module name * @access public * @return bool|ojbect the lang object or false. */ public function loadLang($moduleName) { $modulePath = $this->getModulePath($moduleName); $mainLangFile = $modulePath . 'lang' . $this->pathFix . $this->clientLang . '.php'; $extLangPath = $this->getModuleExtPath($moduleName, 'lang'); $extLangFiles = helper::ls($extLangPath . $this->clientLang, '.php'); /* Set the files to includ. */ if (!is_file($mainLangFile)) { if (empty($extLangFiles)) { return false; } // also no extension file. $langFiles = $extLangFiles; } else { $langFiles = array_merge(array($mainLangFile), $extLangFiles); } global $lang; if (!is_object($lang)) { $lang = new language(); } /* Set productcommon and projectcommon for flow. */ if ($moduleName == 'common') { $productproject = false; if ($this->dbh) { $productproject = $this->dbh->query('SELECT value FROM' . TABLE_CONFIG . "WHERE `owner`='system' AND `module`='custom' AND `key`='productproject'")->fetch(); } $productcommon = $projectcommon = 0; if ($productproject) { $productproject = $productproject->value; list($productcommon, $projectcommon) = explode('_', $productproject); } $lang->productcommon = isset($this->config->productcommonList[$this->clientLang][(int) $productcommon]) ? $this->config->productcommonList[$this->clientLang][(int) $productcommon] : $this->config->productcommonList['zh-cn'][0]; $lang->projectcommon = isset($this->config->projectcommonList[$this->clientLang][(int) $projectcommon]) ? $this->config->projectcommonList[$this->clientLang][(int) $projectcommon] : $this->config->projectcommonList['zh-cn'][0]; } static $loadedLangs = array(); foreach ($langFiles as $langFile) { if (in_array($langFile, $loadedLangs)) { continue; } include $langFile; $loadedLangs[] = $langFile; } /* Merge from the db lang. */ if ($moduleName != 'common' and isset($lang->db->custom[$moduleName])) { foreach ($lang->db->custom[$moduleName] as $section => $fields) { foreach ($fields as $key => $value) { unset($lang->{$moduleName}->{$section}[$key]); $lang->{$moduleName}->{$section}[$key] = $value; } } } $this->lang = $lang; return $lang; }
/** * Load lang and return it as the global lang object. * * @param string $moduleName the module name * @access public * @return bool|ojbect the lang object or false. */ public function loadLang($moduleName) { $modulePath = $this->getModulePath($moduleName); $mainLangFile = $modulePath . 'lang' . $this->pathFix . $this->clientLang . '.php'; $extLangPath = $this->getModuleExtPath($moduleName, 'lang'); $extLangFiles = helper::ls($extLangPath . $this->clientLang, '.php'); /* Set the files to includ. */ if (!is_file($mainLangFile)) { if (empty($extLangFiles)) { return false; } // also no extension file. $langFiles = $extLangFiles; } else { $langFiles = array_merge(array($mainLangFile), $extLangFiles); } global $lang; if (!is_object($lang)) { $lang = new language(); } static $loadedLangs = array(); foreach ($langFiles as $langFile) { if (in_array($langFile, $loadedLangs)) { continue; } include $langFile; $loadedLangs[] = $langFile; } $this->lang = $lang; return $lang; }
/** * Set the model file of one module. If there's an extension file, merge it with the main model file. * * @param string $moduleName the module name * @static * @access public * @return string the model file */ public static function setModelFile($moduleName) { global $app; /* Set the main model file and extension and hook pathes and files. */ $mainModelFile = $app->getModulePath($moduleName) . 'model.php'; $modelExtPath = $app->getModuleExtPath($moduleName, 'model'); $modelHookPath = $modelExtPath . 'hook/'; $extFiles = helper::ls($modelExtPath, '.php'); $hookFiles = helper::ls($modelHookPath, '.php'); /* If no extension files and no hook files, return the main file directly. */ if (empty($extFiles) and empty($hookFiles)) { return $mainModelFile; } /* Else, judge whether needed update or not .*/ $needUpdate = false; $mergedModelFile = $app->getTmpRoot() . 'model' . $app->getPathFix() . $moduleName . '.php'; $lastTime = file_exists($mergedModelFile) ? filemtime($mergedModelFile) : 0; while (!$needUpdate) { foreach ($extFiles as $extFile) { if (filemtime($extFile) > $lastTime) { break 2; } } foreach ($hookFiles as $hookFile) { if (filemtime($hookFile) > $lastTime) { break 2; } } if (is_dir($modelExtPath) and filemtime($modelExtPath) > $lastTime) { break; } if (is_dir($modelHookPath) and filemtime($modelHookPath) > $lastTime) { break; } if (filemtime($mainModelFile) > $lastTime) { break; } return $mergedModelFile; } /* Update the cache file. */ $modelClass = $moduleName . 'Model'; $extModelClass = 'ext' . $modelClass; $extTmpModelClass = 'tmpExt' . $modelClass; $modelLines = "<?php\n"; $modelLines .= "helper::import('{$mainModelFile}');\n"; $modelLines .= "class {$extTmpModelClass} extends {$modelClass} \n{\n"; /* Cycle all the extension files. */ foreach ($extFiles as $extFile) { $extLines = self::removeTagsOfPHP($extFile); $modelLines .= $extLines . "\n"; } /* Create the merged model file and import it. */ $replaceMark = '//**//'; // This mark is for replacing code using. $modelLines .= "{$replaceMark}\n}"; if (!@file_put_contents($mergedModelFile, $modelLines)) { die("ERROR: {$mergedModelFile} not writable, please make sur the " . dirname($mergedModelFile) . ' directory exists and writable'); } if (!class_exists($extTmpModelClass)) { include $mergedModelFile; } /* Get hook codes need to merge. */ $hookCodes = array(); foreach ($hookFiles as $hookFile) { $fileName = baseName($hookFile); list($method) = explode('.', $fileName); $hookCodes[$method][] = self::removeTagsOfPHP($hookFile); } /* Cycle the hook methods and merge hook codes. */ $hookedMethods = array_keys($hookCodes); $mainModelCodes = file($mainModelFile); $mergedModelCodes = file($mergedModelFile); foreach ($hookedMethods as $method) { /* Reflection the hooked method to get it's defined position. */ $methodRelfection = new reflectionMethod($extTmpModelClass, $method); $definedFile = $methodRelfection->getFileName(); $startLine = $methodRelfection->getStartLine() . ' '; $endLine = $methodRelfection->getEndLine() . ' '; /* Merge hook codes. */ $oldCodes = $definedFile == $mergedModelFile ? $mergedModelCodes : $mainModelCodes; $oldCodes = join("", array_slice($oldCodes, $startLine - 1, $endLine - $startLine + 1)); $openBrace = strpos($oldCodes, '{'); $newCodes = substr($oldCodes, 0, $openBrace + 1) . "\n" . join("\n", $hookCodes[$method]) . substr($oldCodes, $openBrace + 1); /* Replace it. */ if ($definedFile == $mergedModelFile) { $modelLines = str_replace($oldCodes, $newCodes, $modelLines); } else { $modelLines = str_replace($replaceMark, $newCodes . "\n{$replaceMark}", $modelLines); } } /* Save it. */ $modelLines = str_replace($extTmpModelClass, $extModelClass, $modelLines); file_put_contents($mergedModelFile, $modelLines); return $mergedModelFile; }
/** * Set the model file of one module. If there's an extension file, merge it with the main model file. * * @param string $moduleName the module name * @static * @access public * @return string the model file */ public static function setModelFile($moduleName) { global $app; /* Set the main model file and extension path and files. */ $mainModelFile = $app->getModulePath($moduleName) . 'model.php'; $modelExtPath = $app->getModuleExtPath($moduleName, 'model'); $extFiles = helper::ls($modelExtPath, '.php'); /* If no extension file, return the main file directly. */ if (empty($extFiles)) { return $mainModelFile; } /* Else, judge whether needed update or not .*/ $mergedModelFile = $app->getTmpRoot() . 'model' . $app->getPathFix() . $moduleName . '.php'; $needUpdate = false; $lastTime = file_exists($mergedModelFile) ? filemtime($mergedModelFile) : 0; foreach ($extFiles as $extFile) { if (filemtime($extFile) > $lastTime) { $needUpdate = true; break; } } if (filemtime($mainModelFile) > $lastTime) { $needUpdate = true; } /* If need'nt update, return the cache file. */ if (!$needUpdate) { return $mergedModelFile; } /* Update the cache file. */ if ($needUpdate) { $modelClass = $moduleName . 'Model'; $extModelClass = 'ext' . $modelClass; $modelLines = trim(file_get_contents($mainModelFile)); $modelLines = rtrim($modelLines, '?>'); // To make sure the last end tag is removed. $modelLines .= "class {$extModelClass} extends {$modelClass} {\n"; /* Cycle all the extension files. */ foreach ($extFiles as $extFile) { $extLines = trim(file_get_contents($extFile)); if (strpos($extLines, '<?php') !== false) { $extLines = ltrim($extLines, '<?php'); } if (strpos($extLines, '?>') !== false) { $extLines = rtrim($extLines, '?>'); } $modelLines .= $extLines . "\n"; } /* Create the merged model file. */ $modelLines .= "}"; file_put_contents($mergedModelFile, $modelLines); return $mergedModelFile; } }
/** * Set the model file of one module. If there's an extension file, merge it with the main model file. * * @param string $moduleName the module name * @static * @access public * @return string the model file */ public static function setModelFile($moduleName) { global $app; /* Set the main model file and extension path and files. */ $mainModelFile = $app->getModulePath($moduleName) . 'model.php'; $modelExtPaths = $app->getModuleExtPath($moduleName, 'model'); $extFiles = array(); foreach ($modelExtPaths as $modelExtPath) { $extFiles = array_merge($extFiles, helper::ls($modelExtPath, '.php')); } /* If no extension file, return the main file directly. */ if (empty($extFiles)) { return $mainModelFile; } /* Else, judge whether needed update or not .*/ $mergedModelDir = $app->getTmpRoot() . 'model' . DS . $app->siteCode[0] . DS . $app->siteCode . DS; $mergedModelFile = $mergedModelDir . $app->siteCode . '.' . $moduleName . '.php'; $needUpdate = false; $lastTime = file_exists($mergedModelFile) ? filemtime($mergedModelFile) : 0; if (!is_dir($mergedModelDir)) { mkdir($mergedModelDir, 0755, true); } foreach ($extFiles as $extFile) { if (filemtime($extFile) > $lastTime) { $needUpdate = true; break; } } if (filemtime($mainModelFile) > $lastTime) { $needUpdate = true; } /* If need'nt update, return the cache file. */ if (!$needUpdate) { return $mergedModelFile; } /* Update the cache file. */ if ($needUpdate) { $modelClass = $moduleName . 'Model'; $extModelClass = 'ext' . $modelClass; $modelLines = "<?php\n"; $modelLines .= "helper::import('{$mainModelFile}');\n"; $modelLines .= "class {$extModelClass} extends {$modelClass} \n{\n"; /* Cycle all the extension files. */ foreach ($extFiles as $extFile) { $extLines = trim(file_get_contents($extFile)); if (strpos($extLines, '<?php') !== false) { $extLines = ltrim($extLines, '<?php'); } if (strpos($extLines, '?>') !== false) { $extLines = rtrim($extLines, '?>'); } $modelLines .= $extLines . "\n"; } /* Create the merged model file. */ $modelLines .= "}"; file_put_contents($mergedModelFile, $modelLines); return $mergedModelFile; } }
/** * Load lang and return it as the global lang object. * * @param string $moduleName the module name * @access public * @return bool|ojbect the lang object or false. */ public function loadLang($moduleName) { $modulePath = $this->getModulePath($moduleName); $mainLangFile = $modulePath . 'lang' . DS . $this->clientLang . '.php'; /* get ext lang files. */ $extLangPath = $this->getModuleExtPath($moduleName, 'lang'); $commonExtLangFiles = helper::ls($extLangPath['common'] . $this->clientLang, '.php'); $siteExtLangFiles = helper::ls($extLangPath['site'] . $this->clientLang, '.php'); $extLangFiles = array_merge($commonExtLangFiles, $siteExtLangFiles); /* Set the files to includ. */ if (!is_file($mainLangFile)) { if (empty($extLangFiles)) { return false; } // also no extension file. $langFiles = $extLangFiles; } else { $langFiles = array_merge(array($mainLangFile), $extLangFiles); } $langPath = $this->getTplRoot() . $this->config->template->name . DS . 'lang' . DS . $moduleName . DS; $templateLangFile = $langPath . $this->clientLang . '.php'; if (file_exists($templateLangFile)) { $langFiles[] = $templateLangFile; } global $lang; if (!is_object($lang)) { $lang = new language(); } if (!isset($lang->{$moduleName})) { $lang->{$moduleName} = new stdclass(); } static $loadedLangs = array(); foreach ($langFiles as $langFile) { if (in_array($langFile, $loadedLangs)) { continue; } include $langFile; $loadedLangs[] = $langFile; } $this->lang = $lang; return $lang; }
/** * 设置视图文件,可以获取其他模块的视图文件。 * Set the view file, thus can use fetch other module's page. * * @param string $moduleName module name * @param string $methodName method name * @access private * @return string the view file */ private function setViewFile($moduleName, $methodName) { $moduleName = strtolower(trim($moduleName)); $methodName = strtolower(trim($methodName)); $modulePath = $this->app->getModulePath($moduleName); $viewExtPath = $this->app->getModuleExtPath($moduleName, 'view'); /* * 主视图文件,扩展视图文件和钩子文件。 * The main view file, extension view file and hook file. **/ $mainViewFile = $modulePath . 'view' . $this->pathFix . $methodName . '.' . $this->viewType . '.php'; $extViewFile = $viewExtPath . $methodName . ".{$this->viewType}.php"; $extHookFiles = helper::ls($viewExtPath, '.hook.php'); $viewFile = file_exists($extViewFile) ? $extViewFile : $mainViewFile; if (!is_file($viewFile)) { $this->app->triggerError("the view file {$viewFile} not found", __FILE__, __LINE__, $exit = true); } if (!empty($extHookFiles)) { return array('viewFile' => $viewFile, 'hookFiles' => $extHookFiles); } return $viewFile; }