* Refactor functions to support loading the zen files and the tao files.

This commit is contained in:
Guan Xiying
2022-10-18 16:14:48 +08:00
parent 3e953772b9
commit ca19766cbc
3 changed files with 43 additions and 39 deletions
+18 -16
View File
@@ -270,20 +270,22 @@ class baseControl
* @access public
* @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object.
*/
public function loadModel($moduleName = '', $appName = '')
public function loadModel($moduleName = '', $appName = '', $type = 'model')
{
if(empty($moduleName)) $moduleName = $this->moduleName;
if(empty($appName)) $appName = $this->appName;
$objectName = $type == 'model' ? $moduleName : $moduleName . ucfirst($type);
global $loadedModels;
if(isset($loadedModels[$appName][$moduleName]))
if(isset($loadedModels[$type][$appName][$moduleName]))
{
$this->$moduleName = $loadedModels[$appName][$moduleName];
$this->dao = $this->$moduleName->dao;
return $this->$moduleName;
$this->$objectName = $loadedModels[$type][$appName][$moduleName];
$this->dao = $this->$objectName->dao;
return $this->$objectName;
}
$modelFile = $this->app->setModelFile($moduleName, $appName);
$modelFile = $this->app->setModelFile($moduleName, $appName, $type);
/**
* 如果没有model文件,尝试加载config配置信息。
@@ -298,24 +300,24 @@ class baseControl
}
/**
* 如果没有扩展文件,model类名是$moduleName + 'model',如果有扩展,还需要增加ext前缀。
* If no extension file, model class name is $moduleName + 'model', else with 'ext' as the prefix.
* 如果没有扩展文件,model类名是$moduleName + $type,如果有扩展,还需要增加ext前缀。
* If no extension file, model class name is $moduleName + $type, else with 'ext' as the prefix.
*/
$modelClass = class_exists('ext' . $appName . $moduleName . 'model') ? 'ext' . $appName . $moduleName . 'model' : $appName . $moduleName . 'model';
$modelClass = class_exists('ext' . $appName . $moduleName . $type) ? 'ext' . $appName . $moduleName . $type : $appName . $moduleName . $type;
if(!class_exists($modelClass))
{
$modelClass = class_exists('ext' . $moduleName . 'model') ? 'ext' . $moduleName . 'model' : $moduleName . 'model';
if(!class_exists($modelClass)) $this->app->triggerError(" The model $modelClass not found", __FILE__, __LINE__, $exit = true);
$modelClass = class_exists('ext' . $moduleName . $type) ? 'ext' . $moduleName . $type : $moduleName . $type;
if(!class_exists($modelClass)) $this->app->triggerError(" The $type $modelClass not found", __FILE__, __LINE__, $exit = true);
}
/**
* 初始化model对象,在control对象中可以通过$this->$moduleName来引用。同时将dao对象赋为control对象的成员变量,方便引用。
* Init the model object thus you can try $this->$moduleName to access it. Also assign the $dao object as a member of control object.
* 初始化model对象,在control对象中可以通过$this->$objectName来引用。同时将dao对象赋为control对象的成员变量,方便引用。
* Init the model object thus you can try $this->$objectName to access it. Also assign the $dao object as a member of control object.
*/
$loadedModels[$appName][$moduleName] = new $modelClass($appName);
$this->$moduleName = $loadedModels[$appName][$moduleName];
$this->dao = $this->$moduleName->dao;
return $this->$moduleName;
$this->$objectName = $loadedModels[$appName][$moduleName];
$this->dao = $this->$objectName->dao;
return $this->$objectName;
}
/**
+13 -11
View File
@@ -203,32 +203,34 @@ class baseModel
* @access public
* @return object|bool the model object or false if model file not exists.
*/
public function loadModel($moduleName, $appName = '')
public function loadModel($moduleName, $appName = '', $type = 'model')
{
if(empty($moduleName)) return false;
if(empty($appName)) $appName = $this->appName;
$moduleName = strtolower($moduleName);
$objectName = $type == 'model' ? $moduleName : $moduleName . ucfirst($type);
global $loadedModels;
if(isset($loadedModels[$appName][$moduleName]))
if(isset($loadedModels[$type][$appName][$moduleName]))
{
$this->$moduleName = $loadedModels[$appName][$moduleName];
return $this->$moduleName;
$this->$objectName = $loadedModels[$type][$appName][$moduleName];
return $this->$objectName;
}
$modelFile = $this->app->setModelFile($moduleName, $appName);
$modelFile = $this->app->setModelFile($moduleName, $appName, $type);
if(!helper::import($modelFile)) return false;
$modelClass = class_exists('ext' . $appName . $moduleName. 'model') ? 'ext' . $appName . $moduleName . 'model' : $appName . $moduleName . 'model';
$modelClass = class_exists('ext' . $appName . $moduleName. $type) ? 'ext' . $appName . $moduleName . $type : $appName . $moduleName . $type;
if(!class_exists($modelClass))
{
$modelClass = class_exists('ext' . $moduleName. 'model') ? 'ext' . $moduleName . 'model' : $moduleName . 'model';
if(!class_exists($modelClass)) $this->app->triggerError(" The model $modelClass not found", __FILE__, __LINE__, $exit = true);
$modelClass = class_exists('ext' . $moduleName. $type) ? 'ext' . $moduleName . $type : $moduleName . $type;
if(!class_exists($modelClass)) $this->app->triggerError(" The $type $modelClass not found", __FILE__, __LINE__, $exit = true);
}
$loadedModels[$appName][$moduleName] = new $modelClass($appName);
$this->$moduleName = $loadedModels[$appName][$moduleName];
return $this->$moduleName;
$loadedModels[$type][$appName][$moduleName] = new $modelClass($appName);
$this->$objectName = $loadedModels[$type][$appName][$moduleName];
return $this->$objectName;
}
/**
+12 -12
View File
@@ -1781,12 +1781,12 @@ class baseRouter
* @access public
* @return string the model file
*/
public function setModelFile($moduleName, $appName = '')
public function setModelFile($moduleName, $appName = '', $type = 'model')
{
if($appName == '') $appName = $this->getAppName();
/* 设置主model文件。 Set the main model file. */
$mainModelFile = $this->getModulePath($appName, $moduleName) . 'model.php';
$mainModelFile = $this->getModulePath($appName, $moduleName) . "{$type}.php";
if($this->config->framework->extensionLevel == 0) return $mainModelFile;
/* 计算扩展的文件和hook文件。Compute the extension files and hook files. */
@@ -1795,7 +1795,7 @@ class baseRouter
$apiFiles = array();
$siteExtended = false;
$modelExtPaths = $this->getModuleExtPath($appName, $moduleName, 'model');
$modelExtPaths = $this->getModuleExtPath($appName, $moduleName, $type);
foreach($modelExtPaths as $extType => $modelExtPath)
{
if(empty($modelExtPath)) continue;
@@ -1817,7 +1817,7 @@ class baseRouter
$extModelPrefix = $this->config->edition . DS . $this->config->vision . DS;
if($siteExtended and !empty($this->siteCode)) $extModelPrefix .= $this->siteCode[0] . DS . $this->siteCode;
$mergedModelDir = $this->getTmpRoot() . 'model' . DS . $extModelPrefix;
$mergedModelDir = $this->getTmpRoot() . $type . DS . $extModelPrefix;
$mergedModelFile = $mergedModelDir . $moduleName . '.php';
if(!is_dir($mergedModelDir)) mkdir($mergedModelDir, 0755, true);
@@ -1825,8 +1825,8 @@ class baseRouter
if(!$this->needModelFileUpdate($mergedModelFile, $extFiles, $hookFiles, $apiFiles, $modelExtPaths, $mainModelFile)) return $mergedModelFile;
/* 合并扩展和hook文件。Merge the extension and hook files. */
$modelLines = $this->mergeModelExtFiles($moduleName, $mainModelFile, $extFiles, $mergedModelDir);
$this->mergeModelHookFiles($moduleName, $mainModelFile, $modelLines, $hookFiles, $mergedModelDir, $mergedModelFile, $apiFiles);
$modelLines = $this->mergeModelExtFiles($moduleName, $extFiles, $mergedModelDir, $type);
$this->mergeModelHookFiles($moduleName, $mainModelFile, $modelLines, $hookFiles, $mergedModelDir, $mergedModelFile, $apiFiles, $type);
return $mergedModelFile;
}
@@ -1873,22 +1873,22 @@ class baseRouter
* 将model的扩展文件合并在一起。Merge model ext files.
*
* @param string $moduleName
* @param string $mainModelFile
* @param array $extFiles
* @param string $mergedModelDir
* @param string $type
* @access public
* @return void
*/
public function mergeModelExtFiles($moduleName, $mainModelFile, $extFiles, $mergedModelDir)
public function mergeModelExtFiles($moduleName, $extFiles, $mergedModelDir, $type = 'model')
{
/* 设置类名。Set the class names. */
$modelClass = "{$moduleName}Model";
$modelClass = $moduleName . ucfirst($type);
$tmpModelClass = "tmpExt$modelClass";
/* 开始拼装代码。Prepare the codes. */
$modelLines = "<?php\n";
$modelLines .= "global \$app;\n";
$modelLines .= "helper::import(\$app->getModulePath('', '$moduleName') . " . "'model.php');\n";
$modelLines .= "helper::import(\$app->getModulePath('', '$moduleName') . " . "'$type.php');\n";
$modelLines .= "class $tmpModelClass extends $modelClass \n{\n";
/* 将扩展文件的代码合并到代码中。Cycle all the extension files and merge them into model lines. */
@@ -1917,10 +1917,10 @@ class baseRouter
* @access public
* @return void
*/
public function mergeModelHookFiles($moduleName, $mainModelFile, $modelLines, $hookFiles, $mergedModelDir, $mergedModelFile, $apiFiles)
public function mergeModelHookFiles($moduleName, $mainModelFile, $modelLines, $hookFiles, $mergedModelDir, $mergedModelFile, $apiFiles, $type = 'model')
{
/* 定义相关变量。Init vars. */
$modelClass = $moduleName . 'Model';
$modelClass = $moduleName . ucfirst($type);
$extModelClass = 'ext' . $modelClass;
$tmpModelClass = 'tmpExt' . $modelClass;
$tmpModelFile = $mergedModelDir . "tmp$moduleName.php";