From 3e953772b9700f25d3c0b23f0fa9d2130c44424d Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Tue, 18 Oct 2022 16:01:56 +0800 Subject: [PATCH 01/39] * Refactor codes to extend the export fields and the editor fields by workflow. --- framework/control.class.php | 95 +++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 36 deletions(-) diff --git a/framework/control.class.php b/framework/control.class.php index 209cf5fb76..e2c6d700d1 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -42,48 +42,71 @@ class control extends baseControl /* Code for task #9224. Set requiredFields for workflow. */ if($this->dbh and (defined('IN_USE') or (defined('RUN_MODE') and RUN_MODE == 'api'))) { - if(isset($this->config->{$this->moduleName}) and strpos($this->methodName, 'export') !== false) - { - if(isset($this->config->{$this->moduleName}->exportFields) or isset($this->config->{$this->moduleName}->list->exportFields)) - { - $exportFields = $this->dao->select('*')->from(TABLE_WORKFLOWFIELD)->where('module')->eq($this->moduleName)->andWhere('canExport')->eq('1')->andWhere('buildin')->eq('0')->fetchAll('field'); - - if(isset($this->config->{$this->moduleName}->exportFields)) - { - foreach($exportFields as $field) $this->config->{$this->moduleName}->exportFields .= ",{$field->field}"; - } - - if(isset($this->config->{$this->moduleName}->list->exportFields)) - { - foreach($exportFields as $field) $this->config->{$this->moduleName}->list->exportFields .= ",{$field->field}"; - } - - foreach($exportFields as $flowField => $exportField) - { - if(!isset($this->lang->{$this->moduleName}->$flowField)) $this->lang->{$this->moduleName}->$flowField = $exportField->name; - } - } - } - - /* Append editor field to this module config from workflow. */ - $textareaFields = $this->dao->select('*')->from(TABLE_WORKFLOWFIELD)->where('module')->eq($this->moduleName)->andWhere('control')->eq('richtext')->andWhere('buildin')->eq('0')->fetchAll('field'); - if($textareaFields) - { - $editorIdList = array(); - foreach($textareaFields as $textareaField) $editorIdList[] = $textareaField->field; - - if(!isset($this->config->{$this->moduleName})) $this->config->{$this->moduleName} = new stdclass(); - if(!isset($this->config->{$this->moduleName}->editor)) $this->config->{$this->moduleName}->editor = new stdclass(); - if(!isset($this->config->{$this->moduleName}->editor->{$this->methodName})) $this->config->{$this->moduleName}->editor->{$this->methodName} = array('id' => '', 'tools' => 'simpleTools'); - $this->config->{$this->moduleName}->editor->{$this->methodName}['id'] .= ',' . join(',', $editorIdList); - trim($this->config->{$this->moduleName}->editor->{$this->methodName}['id'], ','); - } + $this->extendExportFields(); + $this->extendEditorFields(); /* If workflow is created by a normal user, set priv. */ if(isset($this->app->user) and !$this->app->user->admin) $this->setDefaultPrivByWorkflow(); } } + /** + * Append export fields to the config of this module from workflow. + * + * @access public + * @return void + */ + public function extendExportFields() + { + if(isset($this->config->{$this->moduleName}) and strpos($this->methodName, 'export') !== false) + { + if(isset($this->config->{$this->moduleName}->exportFields) or isset($this->config->{$this->moduleName}->list->exportFields)) + { + $exportFields = $this->dao->select('*')->from(TABLE_WORKFLOWFIELD)->where('module')->eq($this->moduleName)->andWhere('canExport')->eq('1')->andWhere('buildin')->eq('0')->fetchAll('field'); + + if(isset($this->config->{$this->moduleName}->exportFields)) + { + foreach($exportFields as $field) $this->config->{$this->moduleName}->exportFields .= ",{$field->field}"; + } + + if(isset($this->config->{$this->moduleName}->list->exportFields)) + { + foreach($exportFields as $field) $this->config->{$this->moduleName}->list->exportFields .= ",{$field->field}"; + } + + foreach($exportFields as $flowField => $exportField) + { + if(!isset($this->lang->{$this->moduleName}->$flowField)) $this->lang->{$this->moduleName}->$flowField = $exportField->name; + } + } + } + } + + /** + * Append editor fields to the config of this module from workflow. + * + * @access public + * @return void + */ + public function extendEditorFields() + { + $moduleName = $this->moduleName; + $methodName = $this->methodName; + + $textareaFields = $this->dao->select('*')->from(TABLE_WORKFLOWFIELD)->where('module')->eq($this->moduleName)->andWhere('control')->eq('richtext')->andWhere('buildin')->eq('0')->fetchAll('field'); + if($textareaFields) + { + $editorIdList = array(); + foreach($textareaFields as $textareaField) $editorIdList[] = $textareaField->field; + + if(!isset($this->config->{$moduleName})) $this->config->{$moduleName} = new stdclass(); + if(!isset($this->config->{$moduleName}->editor)) $this->config->{$moduleName}->editor = new stdclass(); + if(!isset($this->config->{$moduleName}->editor->{$methodName})) $this->config->{$moduleName}->editor->{$methodName} = array('id' => '', 'tools' => 'simpleTools'); + $this->config->{$moduleName}->editor->{$methodName}['id'] .= ',' . join(',', $editorIdList); + trim($this->config->{$moduleName}->editor->{$methodName}['id'], ','); + } + } + /** * Det default priv by workflow. * From ca19766cbc815ae4ff76c4a86991d2f7bfaff6bc Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Tue, 18 Oct 2022 16:14:48 +0800 Subject: [PATCH 02/39] * Refactor functions to support loading the zen files and the tao files. --- framework/base/control.class.php | 34 +++++++++++++++++--------------- framework/base/model.class.php | 24 +++++++++++----------- framework/base/router.class.php | 24 +++++++++++----------- 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index 1576843b29..7ea4882f38 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -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; } /** diff --git a/framework/base/model.class.php b/framework/base/model.class.php index 37f53b06e5..116b9e15b3 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -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; } /** diff --git a/framework/base/router.class.php b/framework/base/router.class.php index 84df7b4d85..23525391b7 100644 --- a/framework/base/router.class.php +++ b/framework/base/router.class.php @@ -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 = "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"; From 8dce21799fa32ac1b6921ead5e75e90f698eefe4 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Tue, 18 Oct 2022 16:28:10 +0800 Subject: [PATCH 03/39] * Optimize the codes of loadModel function. --- framework/base/control.class.php | 8 ++++---- framework/base/model.class.php | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index 7ea4882f38..0a74fdc745 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -281,7 +281,7 @@ class baseControl if(isset($loadedModels[$type][$appName][$moduleName])) { $this->$objectName = $loadedModels[$type][$appName][$moduleName]; - $this->dao = $this->$objectName->dao; + if($type == 'model') $this->dao = $this->$objectName->dao; return $this->$objectName; } @@ -314,9 +314,9 @@ class baseControl * 初始化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->$objectName = $loadedModels[$appName][$moduleName]; - $this->dao = $this->$objectName->dao; + $this->$objectName = new $modelClass($appName); + if($type == 'model') $this->dao = $this->$objectName->dao; + $loadedModels[$type][$appName][$moduleName] = $this->$objectName; return $this->$objectName; } diff --git a/framework/base/model.class.php b/framework/base/model.class.php index 116b9e15b3..6529dcdf0a 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -221,6 +221,7 @@ class baseModel $modelFile = $this->app->setModelFile($moduleName, $appName, $type); if(!helper::import($modelFile)) return false; + $modelClass = class_exists('ext' . $appName . $moduleName. $type) ? 'ext' . $appName . $moduleName . $type : $appName . $moduleName . $type; if(!class_exists($modelClass)) { @@ -228,8 +229,8 @@ class baseModel if(!class_exists($modelClass)) $this->app->triggerError(" The $type $modelClass not found", __FILE__, __LINE__, $exit = true); } - $loadedModels[$type][$appName][$moduleName] = new $modelClass($appName); - $this->$objectName = $loadedModels[$type][$appName][$moduleName]; + $this->$objectName = new $modelClass($appName); + $loadedModels[$type][$appName][$moduleName] = $this->$objectName; return $this->$objectName; } From 30080999766866e38d483366c426e9f44f80ba7e Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Tue, 18 Oct 2022 16:31:24 +0800 Subject: [PATCH 04/39] * Load the zen file and the tao file. --- framework/control.class.php | 8 +++++--- framework/model.class.php | 23 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/framework/control.class.php b/framework/control.class.php index e2c6d700d1..4f4d20df4a 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -33,6 +33,9 @@ class control extends baseControl { parent::__construct($moduleName, $methodName, $appName); + $className = get_class($this); + if(strtolower(substr($className, -6)) == 'control') $this->loadModel($moduleName, $appName, 'zen'); + $this->app->setOpenApp(); if(defined('IN_USE') or (defined('RUN_MODE') and RUN_MODE != 'api')) $this->setPreference(); @@ -179,9 +182,9 @@ class control extends 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') { - return parent::loadModel($moduleName); + return parent::loadModel($moduleName, '', $type); } /** @@ -575,5 +578,4 @@ class control extends baseControl } if($message) $this->send(array('result' => 'fail', 'message' => $message)); } - } diff --git a/framework/model.class.php b/framework/model.class.php index 45c14dba1e..c9f7604d77 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -20,6 +20,25 @@ include dirname(__FILE__) . '/base/model.class.php'; class model extends baseModel { + /** + * Load the tao file. + * + * @param string $appName + * @access public + * @return void + */ + public function __construct($appName = '') + { + parent::__construct($appName); + + $className = get_class($this); + if(strtolower(substr($className, -5)) == 'model') + { + $moduleName = $this->app->getModuleName(); + $this->loadModel($moduleName, $appName, 'tao'); + } + } + /** * 企业版部分功能是从然之合并过来的。然之代码中调用loadModel方法时传递了一个非空的appName,在禅道中会导致错误。 * 调用父类的loadModel方法来避免这个错误。 @@ -30,9 +49,9 @@ class model extends 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') { - return parent::loadModel($moduleName); + return parent::loadModel($moduleName, '', $type); } /** From b9b8b6e584c53354bccd6e0d263a961d45e347a0 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Tue, 18 Oct 2022 16:32:26 +0800 Subject: [PATCH 05/39] * Add functions to call the methods declared in the zen files and the tao files. --- framework/control.class.php | 37 +++++++++++++++++++++++++++++++++++++ framework/model.class.php | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/framework/control.class.php b/framework/control.class.php index 4f4d20df4a..c97478b09d 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -578,4 +578,41 @@ class control extends baseControl } if($message) $this->send(array('result' => 'fail', 'message' => $message)); } + + /** + * Call the functions declared in the tao files. + * + * @param string $method + * @param array $arguments + * @access public + * @return mixed + */ + public function __call($method, $arguments) + { + $moduleName = $this->app->getModuleName(); + $taoClass = $moduleName . 'Tao'; + + if(!is_callable(array($this->{$taoClass}, $method))) $this->app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); + + return call_user_func_array(array($this->{$taoClass}, $method), $arguments); + } + + /** + * Call the static functions declared in the tao files. + * + * @param string $method + * @param array $arguments + * @access public + * @return mixed + */ + public static function __callStatic($method, $arguments) + { + global $app; + $moduleName = $app->getModuleName(); + $taoClass = $moduleName . 'Tao'; + + if(!is_callable("{$taoClass}::{$method}")) $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); + + return call_user_func_array("{$taoClass}::{$method}", $arguments); + } } diff --git a/framework/model.class.php b/framework/model.class.php index c9f7604d77..c4d606f08a 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -339,4 +339,41 @@ class model extends baseModel $flow = $this->loadModel('workflow')->getByModule($moduleName); if($flow && $action) return $this->loadModel('workflowhook')->execute($flow, $action, $objectID); } + + /** + * Call the functions declared in the tao files. + * + * @param string $method + * @param array $arguments + * @access public + * @return mixed + */ + public function __call($method, $arguments) + { + $moduleName = $this->app->getModuleName(); + $taoClass = $moduleName . 'Tao'; + + if(!is_callable(array($this->{$taoClass}, $method))) $this->app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); + + return call_user_func_array(array($this->{$taoClass}, $method), $arguments); + } + + /** + * Call the static functions declared in the tao files. + * + * @param string $method + * @param array $arguments + * @access public + * @return mixed + */ + public static function __callStatic($method, $arguments) + { + global $app; + $moduleName = $app->getModuleName(); + $taoClass = $moduleName . 'Tao'; + + if(!is_callable("{$taoClass}::{$method}")) $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); + + return call_user_func_array("{$taoClass}::{$method}", $arguments); + } } From 8d961926a4798cc7699583ec2249887eebb1b852 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Tue, 18 Oct 2022 16:45:52 +0800 Subject: [PATCH 06/39] * Adjust the comments. --- framework/base/control.class.php | 7 ++++--- framework/base/model.class.php | 8 +++++--- framework/base/router.class.php | 12 ++++++------ framework/control.class.php | 13 +++++++------ framework/model.class.php | 12 +++++++----- 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index 0a74fdc745..af56e1572c 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -266,9 +266,10 @@ class baseControl * Load the model file of one module. * * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. - * @param string $appName The app name, if empty, use current app's name. - * @access public - * @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object. + * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. + * @param string $type 对象的类型,可选值 model、zen、tao,默认为 model。The type of the object, optional values model, zen, tao, the default is model. + * @access public + * @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object. */ public function loadModel($moduleName = '', $appName = '', $type = 'model') { diff --git a/framework/base/model.class.php b/framework/base/model.class.php index 6529dcdf0a..716da9c3dd 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -199,9 +199,11 @@ class baseModel * * Load the model of one module. After loaded, can use $this->$moduleName to visit the model object. * - * @param string $moduleName - * @access public - * @return object|bool the model object or false if model file not exists. + * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. + * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. + * @param string $type 对象的类型,可选值 model、zen、tao,默认为 model。The type of the object, optional values model, zen, tao, the default is model. + * @access public + * @return object|bool the model object or false if model file not exists. */ public function loadModel($moduleName, $appName = '', $type = 'model') { diff --git a/framework/base/router.class.php b/framework/base/router.class.php index 23525391b7..d991bdc01f 100644 --- a/framework/base/router.class.php +++ b/framework/base/router.class.php @@ -1623,7 +1623,7 @@ class baseRouter * * @param string $appName the app name * @param string $moduleName the module name - * @param string $ext the extension type, can be control|model|view|lang|config + * @param string $ext the extension type, can be control|model|view|lang|config|zen|tao * @access public * @return string the extension path. */ @@ -1775,11 +1775,11 @@ class baseRouter * 设置一个模块的model文件,如果存在model扩展,一起合并。 * 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 - * @param string $appName the app name - * @static - * @access public - * @return string the model file + * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. + * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. + * @param string $type 对象的类型,可选值 model、zen、tao,默认为 model。The type of the object, optional values model, zen, tao, the default is model. + * @access public + * @return string the model file */ public function setModelFile($moduleName, $appName = '', $type = 'model') { diff --git a/framework/control.class.php b/framework/control.class.php index c97478b09d..d10c16c270 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -172,15 +172,16 @@ class control extends baseControl } /** - * 企业版部分功能是从然之合并过来的。然之代码中调用loadModel方法时传递了一个非空的appName,在禅道中会导致错误。 + * 企业版部分功能是从然之合并过来的。ZDOO代码中调用loadModel方法时传递了一个非空的appName,在禅道中会导致错误。 * 调用父类的loadModel方法来避免这个错误。 - * Some codes merged from ranzhi called the function loadModel with a non-empty appName which causes an error in zentao. + * Some codes merged from ZDOO called the function loadModel with a non-empty appName which causes an error in zentao. * Call the parent function with empty appName to avoid this error. * - * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. - * @param string $appName The app name, if empty, use current app's name. - * @access public - * @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object. + * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. + * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. + * @param string $type 对象的类型,可选值 model、zen、tao,默认为 model。The type of the object, optional values model, zen, tao, the default is model. + * @access public + * @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object. */ public function loadModel($moduleName = '', $appName = '', $type = 'model') { diff --git a/framework/model.class.php b/framework/model.class.php index c4d606f08a..80a7c5a450 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -40,14 +40,16 @@ class model extends baseModel } /** - * 企业版部分功能是从然之合并过来的。然之代码中调用loadModel方法时传递了一个非空的appName,在禅道中会导致错误。 + * 企业版部分功能是从然之合并过来的。ZDOO代码中调用loadModel方法时传递了一个非空的appName,在禅道中会导致错误。 * 调用父类的loadModel方法来避免这个错误。 - * Some codes merged from ranzhi called the function loadModel with a non-empty appName which causes an error in zentao. + * Some codes merged from ZDOO called the function loadModel with a non-empty appName which causes an error in zentao. * Call the parent function with empty appName to avoid this error. * - * @param string $moduleName - * @access public - * @return object|bool the model object or false if model file not exists. + * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. + * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. + * @param string $type 对象的类型,可选值 model、zen、tao,默认为 model。The type of the object, optional values model, zen, tao, the default is model. + * @access public + * @return object|bool the model object or false if model file not exists. */ public function loadModel($moduleName, $appName = '', $type = 'model') { From 01ac19f6e50910e1ad2d78e908f602114347219f Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Tue, 18 Oct 2022 17:19:18 +0800 Subject: [PATCH 07/39] * Fix bug of load zen and tao. --- framework/control.class.php | 10 ++++++---- framework/model.class.php | 9 +++------ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/framework/control.class.php b/framework/control.class.php index d10c16c270..a766bb21b7 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -33,8 +33,10 @@ class control extends baseControl { parent::__construct($moduleName, $methodName, $appName); - $className = get_class($this); - if(strtolower(substr($className, -6)) == 'control') $this->loadModel($moduleName, $appName, 'zen'); + if(!$moduleName) $moduleName = $this->moduleName; + + $className = strtolower(get_class($this)); + if($className == $moduleName || $className == 'my' . $moduleName) $this->loadModel($moduleName, $appName, 'zen'); $this->app->setOpenApp(); @@ -591,7 +593,7 @@ class control extends baseControl public function __call($method, $arguments) { $moduleName = $this->app->getModuleName(); - $taoClass = $moduleName . 'Tao'; + $taoClass = $moduleName . 'Zen'; if(!is_callable(array($this->{$taoClass}, $method))) $this->app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); @@ -610,7 +612,7 @@ class control extends baseControl { global $app; $moduleName = $app->getModuleName(); - $taoClass = $moduleName . 'Tao'; + $taoClass = $moduleName . 'Zen'; if(!is_callable("{$taoClass}::{$method}")) $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); diff --git a/framework/model.class.php b/framework/model.class.php index 80a7c5a450..8401f940f7 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -31,12 +31,9 @@ class model extends baseModel { parent::__construct($appName); - $className = get_class($this); - if(strtolower(substr($className, -5)) == 'model') - { - $moduleName = $this->app->getModuleName(); - $this->loadModel($moduleName, $appName, 'tao'); - } + $className = strtolower(get_class($this)); + $moduleName = $this->app->getModuleName(); + if($className == $moduleName . 'model' || $className == 'ext' . $moduleName . 'model') $this->loadModel($moduleName, $appName, 'tao'); } /** From b0be2cd295c536b285ac9e678da41eda3a50f21f Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 19 Oct 2022 10:10:15 +0800 Subject: [PATCH 08/39] * Remove useless variable. --- framework/base/control.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index af56e1572c..34a6592f74 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -753,7 +753,6 @@ class baseControl $currentModuleName = $this->moduleName; $currentMethodName = $this->methodName; - $currentAppName = $this->appName; $currentParams = $this->app->getParams(); /** From 66d76e5d65d0c3ad2a30cee38c12a760db7a2649 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 19 Oct 2022 10:15:59 +0800 Subject: [PATCH 09/39] * Fix error if the zen file doesn't exist. --- framework/control.class.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/framework/control.class.php b/framework/control.class.php index a766bb21b7..9f66e76a7c 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -36,7 +36,12 @@ class control extends baseControl if(!$moduleName) $moduleName = $this->moduleName; $className = strtolower(get_class($this)); - if($className == $moduleName || $className == 'my' . $moduleName) $this->loadModel($moduleName, $appName, 'zen'); + if($className == $moduleName || $className == 'my' . $moduleName) + { + $this->loadModel($moduleName, $appName, 'zen'); + $zenClass = $moduleName . 'Zen'; + if(isset($this->{$zenClass})) $this->{$zenClass}->view = $this->view; + } $this->app->setOpenApp(); From 50c2fd653cb9444df4dd67b9d06641a8c0a63bea Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 19 Oct 2022 16:36:22 +0800 Subject: [PATCH 10/39] * Simplify the codes to load the tao file by the loadModel function. --- framework/model.class.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/framework/model.class.php b/framework/model.class.php index 8401f940f7..b0f1205b23 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -31,9 +31,8 @@ class model extends baseModel { parent::__construct($appName); - $className = strtolower(get_class($this)); $moduleName = $this->app->getModuleName(); - if($className == $moduleName . 'model' || $className == 'ext' . $moduleName . 'model') $this->loadModel($moduleName, $appName, 'tao'); + $this->loadModel($moduleName, $appName, 'tao'); } /** From f84694aa7c2f624e3766499a5b5fe8382449380d Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 19 Oct 2022 16:39:54 +0800 Subject: [PATCH 11/39] Simplify the codes to load the zen file by the loadModel function. --- framework/control.class.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/framework/control.class.php b/framework/control.class.php index 9f66e76a7c..1ce9f030ac 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -33,15 +33,7 @@ class control extends baseControl { parent::__construct($moduleName, $methodName, $appName); - if(!$moduleName) $moduleName = $this->moduleName; - - $className = strtolower(get_class($this)); - if($className == $moduleName || $className == 'my' . $moduleName) - { - $this->loadModel($moduleName, $appName, 'zen'); - $zenClass = $moduleName . 'Zen'; - if(isset($this->{$zenClass})) $this->{$zenClass}->view = $this->view; - } + $this->loadModel($moduleName, $appName, 'zen'); $this->app->setOpenApp(); From 12ad262e1de8595581959b2cd98a2917a24f25c2 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 19 Oct 2022 16:40:22 +0800 Subject: [PATCH 12/39] * Rename the $taoClass to the $zenClass. --- framework/control.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/framework/control.class.php b/framework/control.class.php index 1ce9f030ac..e939a56bb9 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -590,11 +590,11 @@ class control extends baseControl public function __call($method, $arguments) { $moduleName = $this->app->getModuleName(); - $taoClass = $moduleName . 'Zen'; + $zenClass = $moduleName . 'Zen'; - if(!is_callable(array($this->{$taoClass}, $method))) $this->app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); + if(!is_callable(array($this->{$zenClass}, $method))) $this->app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); - return call_user_func_array(array($this->{$taoClass}, $method), $arguments); + return call_user_func_array(array($this->{$zenClass}, $method), $arguments); } /** @@ -609,10 +609,10 @@ class control extends baseControl { global $app; $moduleName = $app->getModuleName(); - $taoClass = $moduleName . 'Zen'; + $zenClass = $moduleName . 'Zen'; - if(!is_callable("{$taoClass}::{$method}")) $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); + if(!is_callable("{$zenClass}::{$method}")) $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); - return call_user_func_array("{$taoClass}::{$method}", $arguments); + return call_user_func_array("{$zenClass}::{$method}", $arguments); } } From 0ec0e38a297b3fbe73e6e6bc3e26855ad9edbd2b Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 19 Oct 2022 16:40:58 +0800 Subject: [PATCH 13/39] * Refactor the loadModel function to support loading zen files and return zen object. --- framework/base/control.class.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index 34a6592f74..149e9ed108 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -281,9 +281,9 @@ class baseControl global $loadedModels; if(isset($loadedModels[$type][$appName][$moduleName])) { - $this->$objectName = $loadedModels[$type][$appName][$moduleName]; - if($type == 'model') $this->dao = $this->$objectName->dao; - return $this->$objectName; + $this->{$objectName} = $loadedModels[$type][$appName][$moduleName]; + if($type == 'model') $this->dao = $this->{$objectName}->dao; + return $this->{$objectName}; } $modelFile = $this->app->setModelFile($moduleName, $appName, $type); @@ -312,12 +312,18 @@ class baseControl } /** - * 初始化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. + * 因为zen继承control,构造函数里会调用loadModel方法,赋默认值值防止递归调用。 */ - $this->$objectName = new $modelClass($appName); - if($type == 'model') $this->dao = $this->$objectName->dao; - $loadedModels[$type][$appName][$moduleName] = $this->$objectName; + if($type == 'zen') $loadedModels[$type][$appName][$moduleName] = false; + + /** + * 初始化model对象,在control对象中可以通过$this->$objectName来引用。同时将dao对象赋为control对象的成员变量,将view对象赋为zen对象的成员变量,方便引用。 + * Init the model object thus you can try $this->$objectName to access it. Also assign the $dao object as a member of control object, assign the $view object as a member of zen object. + */ + $this->{$objectName} = new $modelClass($appName); + if($type == 'model') $this->dao = $this->{$objectName}->dao; + if($type == 'zen') $this->{$objectName}->view = $this->view; + $loadedModels[$type][$appName][$moduleName] = $this->{$objectName}; return $this->$objectName; } From 7d305bd7f70770bcfe7893892e5086b11a478b56 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 19 Oct 2022 16:43:49 +0800 Subject: [PATCH 14/39] * Refactor the loadModel function to support loading tao files and return tao object. --- framework/base/model.class.php | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/framework/base/model.class.php b/framework/base/model.class.php index 716da9c3dd..b4e3a199de 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -207,23 +207,30 @@ class baseModel */ public function loadModel($moduleName, $appName = '', $type = 'model') { - if(empty($moduleName)) return false; + if(empty($moduleName)) $moduleName = $this->moduleName; if(empty($appName)) $appName = $this->appName; - $moduleName = strtolower($moduleName); $objectName = $type == 'model' ? $moduleName : $moduleName . ucfirst($type); global $loadedModels; if(isset($loadedModels[$type][$appName][$moduleName])) { - $this->$objectName = $loadedModels[$type][$appName][$moduleName]; - return $this->$objectName; + $this->{$objectName} = $loadedModels[$type][$appName][$moduleName]; + return $this->{$objectName}; } $modelFile = $this->app->setModelFile($moduleName, $appName, $type); + /** + * 如果没有model文件,返回。 + * If no model file, return. + */ if(!helper::import($modelFile)) return false; + /** + * 如果没有扩展文件,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. $type) ? 'ext' . $appName . $moduleName . $type : $appName . $moduleName . $type; if(!class_exists($modelClass)) { @@ -231,9 +238,18 @@ class baseModel if(!class_exists($modelClass)) $this->app->triggerError(" The $type $modelClass not found", __FILE__, __LINE__, $exit = true); } - $this->$objectName = new $modelClass($appName); - $loadedModels[$type][$appName][$moduleName] = $this->$objectName; - return $this->$objectName; + /** + * 因为tao继承model,构造函数里会调用loadModel方法,赋默认值值防止递归调用。 + */ + if($type == 'tao') $loadedModels[$type][$appName][$moduleName] = false; + + /** + * 初始化model对象,在model对象中可以通过$this->$objectName来引用。 + * Init the model object thus you can try $this->$objectName to access it. + */ + $this->{$objectName} = new $modelClass($appName); + $loadedModels[$type][$appName][$moduleName] = $this->{$objectName}; + return $this->{$objectName}; } /** From d9883bd382cf87bbf19e091f58dc84472b80ea07 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 19 Oct 2022 16:45:18 +0800 Subject: [PATCH 15/39] * Refactor to avoid errors when class names contain ext or model. --- framework/base/model.class.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/framework/base/model.class.php b/framework/base/model.class.php index b4e3a199de..0f30083ed2 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -170,11 +170,11 @@ class baseModel */ public function getModuleName() { - $parentClass = get_parent_class($this); - $selfClass = get_class($this); - $className = $parentClass == 'model' ? $selfClass : $parentClass; - if($className == 'extensionModel') return 'extension'; - return strtolower(str_ireplace(array('ext', 'Model'), '', $className)); + $className = get_class($this); + $parentClasses = class_parents($this); + if(count($parentClasses) > 2) $className = current(array_slice($parentClasses, -3, 1)); + if(strtolower(substr($className, -5)) == 'model') $className = strtolower(substr($className, 0, strlen($className) - 5)); + return $className; } /** From c43f14c7b00fa67c73cc3cada968c58d8b8089e1 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 19 Oct 2022 16:48:04 +0800 Subject: [PATCH 16/39] * Refactor to support load extensions of tao. --- framework/base/model.class.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/framework/base/model.class.php b/framework/base/model.class.php index 0f30083ed2..43c04a5b3d 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -273,17 +273,22 @@ class baseModel public function loadExtension($extensionName, $moduleName = '') { if(empty($extensionName)) return false; + if(empty($moduleName)) $moduleName = $this->getModuleName(); - $moduleName = $moduleName ? $moduleName : $this->getModuleName(); $moduleName = strtolower($moduleName); $extensionName = strtolower($extensionName); + $type = 'model'; + $className = strtolower(get_class($this)); + if($className == $moduleName . 'tao' || $className == 'ext' . $moduleName . 'tao') $type = 'tao'; + /* 设置扩展类的名字。Set the extension class name. */ $extensionClass = $extensionName . ucfirst($moduleName); + if($type != 'model') $extensionClass .= ucfirst($type); if(isset($this->$extensionClass)) return $this->$extensionClass; /* 设置扩展的名字和相应的文件。Set extenson name and extension file. */ - $moduleExtPath = $this->app->getModuleExtPath($this->appName, $moduleName, 'model'); + $moduleExtPath = $this->app->getModuleExtPath($this->appName, $moduleName, $type); if(!empty($moduleExtPath['site'])) $extensionFile = $moduleExtPath['site'] . 'class/' . $extensionName . '.class.php'; if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['custom'] . 'class/' . $extensionName . '.class.php'; if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['saas'] . 'class/' . $extensionName . '.class.php'; @@ -292,13 +297,13 @@ class baseModel if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['common'] . 'class/' . $extensionName . '.class.php'; /* 载入父类。Try to import parent model file auto and then import the extension file. */ - if(!class_exists($moduleName . 'Model')) helper::import($this->app->getModulePath($this->appName, $moduleName) . 'model.php'); + if(!class_exists($moduleName . ucfirst($type))) helper::import($this->app->getModulePath($this->appName, $moduleName) . $type . '.php'); if(!helper::import($extensionFile)) return false; if(!class_exists($extensionClass)) return false; /* 实例化扩展类。Create an instance of the extension class and return it. */ $extensionObject = new $extensionClass; - $extensionClass = str_replace('Model', '', $extensionClass); + if($type == 'model') $extensionClass = str_replace(ucfirst($type), '', $extensionClass); $this->$extensionClass = $extensionObject; return $extensionObject; } From dc874101dab67cef6427501c84c142e29d49078d Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 19 Oct 2022 17:16:51 +0800 Subject: [PATCH 17/39] * Remove blank. --- framework/base/model.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/base/model.class.php b/framework/base/model.class.php index 43c04a5b3d..f370fd5248 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -303,7 +303,7 @@ class baseModel /* 实例化扩展类。Create an instance of the extension class and return it. */ $extensionObject = new $extensionClass; - if($type == 'model') $extensionClass = str_replace(ucfirst($type), '', $extensionClass); + if($type == 'model') $extensionClass = str_replace(ucfirst($type), '', $extensionClass); $this->$extensionClass = $extensionObject; return $extensionObject; } From eb43521900daac3aed06f0f035e31661c298acf2 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Thu, 20 Oct 2022 10:18:48 +0800 Subject: [PATCH 18/39] * Add the loadExtension function to load the extension of zen. --- framework/control.class.php | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/framework/control.class.php b/framework/control.class.php index e939a56bb9..fc50a4cd27 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -187,6 +187,63 @@ class control extends baseControl return parent::loadModel($moduleName, '', $type); } + /** + * 加载model的class扩展,主要是为了开发加密代码使用。 + * 可以将主要的逻辑存放到$moduleName/ext/model/class/$extensionName.class.php中。 + * 然后在ext/model/$extension.php的扩展里面使用$this->loadExtension()来调用相应的方法。 + * ext/model/class/*.class.php代码可以加密。而ext/model/*.php可以不用加密。 + * 因为框架对model的扩展是采取合并文件的方式,ext/model/*.php文件不能加密。 + * + * Load extension class of a model thus user can encrypt the code. + * You can put the main extension logic codes in $moduleName/ext/model/class/$extensionName.class.php. + * And call them by the ext/model/$extension.php like this: $this->loadExtension('myextension')->method(). + * You can encrypt the code in ext/model/class/*.class.php. + * Because the framework will merge the extension files in ext/model/*.php to the module/model.php. + * + * @param string $extensionName + * @param string $moduleName + * @access public + * @return void + */ + public function loadExtension($extensionName, $moduleName = '') + { + if(empty($extensionName)) return false; + if(empty($moduleName)) $moduleName = $this->moduleName; + + $moduleName = strtolower($moduleName); + $extensionName = strtolower($extensionName); + + $type = 'model'; + $className = strtolower(get_class($this)); + if($className == $moduleName . 'zen' || $className == 'ext' . $moduleName . 'zen') $type = 'zen'; + + /* 设置扩展类的名字。Set the extension class name. */ + $extensionClass = $extensionName . ucfirst($moduleName); + if($type != 'model') $extensionClass .= ucfirst($type); + if(isset($this->$extensionClass)) return $this->$extensionClass; + + /* 设置扩展的名字和相应的文件。Set extenson name and extension file. */ + $moduleExtPath = $this->app->getModuleExtPath($this->appName, $moduleName, $type); + if(!empty($moduleExtPath['site'])) $extensionFile = $moduleExtPath['site'] . 'class/' . $extensionName . '.class.php'; + if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['custom'] . 'class/' . $extensionName . '.class.php'; + if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['saas'] . 'class/' . $extensionName . '.class.php'; + if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['vision'] . 'class/' . $extensionName . '.class.php'; + if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['xuan'] . 'class/' . $extensionName . '.class.php'; + if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['common'] . 'class/' . $extensionName . '.class.php'; + + /* 载入父类。Try to import parent model file auto and then import the extension file. */ + if(!class_exists($moduleName . ucfirst($type))) helper::import($this->app->getModulePath($this->appName, $moduleName) . $type . '.php'); + if(!helper::import($extensionFile)) return false; + if(!class_exists($extensionClass)) return false; + + /* 实例化扩展类。Create an instance of the extension class and return it. */ + $extensionObject = new $extensionClass; + if($type == 'model') $extensionClass = str_replace(ucfirst($type), '', $extensionClass); + $this->$extensionClass = $extensionObject; + $this->$extensionClass->view = $this->view; + return $extensionObject; + } + /** * 设置视图文件:主视图文件,扩展视图文件, 站点扩展视图文件,以及钩子脚本。 * Set view files: the main file, extension view file, site extension view file and hook files. From 83cf9f8aa5dd25944ce77283e4fb0880e8d36547 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Thu, 20 Oct 2022 10:21:13 +0800 Subject: [PATCH 19/39] * Fix bug called a object by the loadModel function in another module. --- framework/model.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/model.class.php b/framework/model.class.php index b0f1205b23..8b967734f5 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -31,7 +31,7 @@ class model extends baseModel { parent::__construct($appName); - $moduleName = $this->app->getModuleName(); + $moduleName = $this->getModuleName(); $this->loadModel($moduleName, $appName, 'tao'); } @@ -348,7 +348,7 @@ class model extends baseModel */ public function __call($method, $arguments) { - $moduleName = $this->app->getModuleName(); + $moduleName = $this->getModuleName(); $taoClass = $moduleName . 'Tao'; if(!is_callable(array($this->{$taoClass}, $method))) $this->app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); From 1cbb4546fda807357d085bfdc3a0be30b32b362b Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Thu, 20 Oct 2022 10:25:46 +0800 Subject: [PATCH 20/39] * If the method is callable return its result else trigger error. --- framework/control.class.php | 10 +++++----- framework/model.class.php | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/framework/control.class.php b/framework/control.class.php index fc50a4cd27..747a1e12f6 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -646,12 +646,12 @@ class control extends baseControl */ public function __call($method, $arguments) { - $moduleName = $this->app->getModuleName(); + $moduleName = $this->moduleName(); $zenClass = $moduleName . 'Zen'; - if(!is_callable(array($this->{$zenClass}, $method))) $this->app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); + if(is_callable(array($this->{$zenClass}, $method))) return call_user_func_array(array($this->{$zenClass}, $method), $arguments); - return call_user_func_array(array($this->{$zenClass}, $method), $arguments); + $this->app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); } /** @@ -668,8 +668,8 @@ class control extends baseControl $moduleName = $app->getModuleName(); $zenClass = $moduleName . 'Zen'; - if(!is_callable("{$zenClass}::{$method}")) $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); + if(is_callable("{$zenClass}::{$method}")) return call_user_func_array("{$zenClass}::{$method}", $arguments); - return call_user_func_array("{$zenClass}::{$method}", $arguments); + $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); } } diff --git a/framework/model.class.php b/framework/model.class.php index 8b967734f5..d120e4b7c1 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -351,9 +351,9 @@ class model extends baseModel $moduleName = $this->getModuleName(); $taoClass = $moduleName . 'Tao'; - if(!is_callable(array($this->{$taoClass}, $method))) $this->app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); + if(is_callable(array($this->{$taoClass}, $method))) return call_user_func_array(array($this->{$taoClass}, $method), $arguments); - return call_user_func_array(array($this->{$taoClass}, $method), $arguments); + $this->app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); } /** @@ -370,8 +370,8 @@ class model extends baseModel $moduleName = $app->getModuleName(); $taoClass = $moduleName . 'Tao'; - if(!is_callable("{$taoClass}::{$method}")) $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); + if(is_callable("{$taoClass}::{$method}")) return call_user_func_array("{$taoClass}::{$method}", $arguments); - return call_user_func_array("{$taoClass}::{$method}", $arguments); + $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); } } From 86df3868fb32711dc30cbfc9d46a82873d2054ad Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Thu, 20 Oct 2022 10:28:35 +0800 Subject: [PATCH 21/39] * Refactor the magic method to support call static method. Only static methods of classes declared in a zen file or an ext zen file or a tao file or an ext tao file can be called. --- framework/control.class.php | 4 +++- framework/model.class.php | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/framework/control.class.php b/framework/control.class.php index 747a1e12f6..ae729ac382 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -666,8 +666,10 @@ class control extends baseControl { global $app; $moduleName = $app->getModuleName(); - $zenClass = $moduleName . 'Zen'; + $zenClass = 'ext' . $moduleName . 'Zen'; + if(is_callable("{$zenClass}::{$method}")) return call_user_func_array("{$zenClass}::{$method}", $arguments); + $zenClass = $moduleName . 'Zen'; if(is_callable("{$zenClass}::{$method}")) return call_user_func_array("{$zenClass}::{$method}", $arguments); $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); diff --git a/framework/model.class.php b/framework/model.class.php index d120e4b7c1..062cf998c9 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -367,9 +367,11 @@ class model extends baseModel public static function __callStatic($method, $arguments) { global $app; - $moduleName = $app->getModuleName(); - $taoClass = $moduleName . 'Tao'; + $moduleName = get_called_class(); + $taoClass = 'ext' . $moduleName . 'Tao'; + if(is_callable("{$taoClass}::{$method}")) return call_user_func_array("{$taoClass}::{$method}", $arguments); + $taoClass = $moduleName . 'Tao'; if(is_callable("{$taoClass}::{$method}")) return call_user_func_array("{$taoClass}::{$method}", $arguments); $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); From 5498c0872b16e538b005d04504bbf8f322c7f3b8 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Thu, 20 Oct 2022 11:00:11 +0800 Subject: [PATCH 22/39] * Cann't call the member variables of a class in the magic method. --- framework/control.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/control.class.php b/framework/control.class.php index ae729ac382..428a8ff520 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -646,7 +646,7 @@ class control extends baseControl */ public function __call($method, $arguments) { - $moduleName = $this->moduleName(); + $moduleName = $this->app->getModuleName(); $zenClass = $moduleName . 'Zen'; if(is_callable(array($this->{$zenClass}, $method))) return call_user_func_array(array($this->{$zenClass}, $method), $arguments); From a769caa8c57b7e5c4b4b246736f4089552116876 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Thu, 20 Oct 2022 14:24:30 +0800 Subject: [PATCH 23/39] * Get module name by preg_match. --- framework/model.class.php | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/framework/model.class.php b/framework/model.class.php index 062cf998c9..977e69c42b 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -367,12 +367,28 @@ class model extends baseModel public static function __callStatic($method, $arguments) { global $app; - $moduleName = get_called_class(); - $taoClass = 'ext' . $moduleName . 'Tao'; - if(is_callable("{$taoClass}::{$method}")) return call_user_func_array("{$taoClass}::{$method}", $arguments); - $taoClass = $moduleName . 'Tao'; - if(is_callable("{$taoClass}::{$method}")) return call_user_func_array("{$taoClass}::{$method}", $arguments); + $moduleName = strtolower(get_called_class()); + + preg_match_all('/^(ext)?(\w+)model/', $moduleName, $matches); + if(isset($matches[2][0])) + { + $moduleName = $matches[2][0]; + } + else + { + preg_match_all('/^(ext)?(\w+)tao/', $moduleName, $matches); + if(isset($matches[2][0])) $moduleName = $matches[2][0]; + } + + $modelClass = 'ext' . $moduleName . 'Model'; + if(method_exists($modelClass, $method)) return call_user_func_array("{$modelClass}::{$method}", $arguments); + + $taoClass = 'ext' . $moduleName . 'Tao'; + if(method_exists($taoClass, $method)) return call_user_func_array("{$taoClass}::{$method}", $arguments); + + $taoClass = $moduleName . 'Tao'; + if(method_exists($taoClass, $method)) return call_user_func_array("{$taoClass}::{$method}", $arguments); $app->triggerError("the module {$moduleName} has no {$method} method", __FILE__, __LINE__, $exit = true); } From 23f17a3812aa5039d294c69915b582eb0f5ea273 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 10:57:27 +0800 Subject: [PATCH 24/39] * Add function to load the object of a class. --- framework/base/router.class.php | 51 +++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/framework/base/router.class.php b/framework/base/router.class.php index d991bdc01f..13bc9579ac 100644 --- a/framework/base/router.class.php +++ b/framework/base/router.class.php @@ -2181,6 +2181,57 @@ class baseRouter return isset($module) ? $module : false; } + /** + * 加载指定模块下的某种对象。 + * Load the target object of one module. + * + * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. + * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. + * @param string $class 对象的类型,可选值 model、zen、tao,默认为 model。The type of the object, optional values model, zen, tao, the default is model. + * @access public + * @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object. + */ + public function loadTarget($moduleName = '', $appName = '', $class = 'model') + { + if(empty($moduleName)) $moduleName = $this->moduleName; + if(empty($appName)) $appName = $this->appName; + + global $loadedTargets; + if(isset($loadedTargets[$class][$appName][$moduleName])) return $loadedTargets[$class][$appName][$moduleName]; + + $targetFile = $this->setModelFile($moduleName, $appName, $class); + + /** + * 如果没有target文件,返回false。 + * If no target file, return false. + */ + if(!helper::import($targetFile)) return false; + + /** + * 如果没有扩展文件,target类名是$moduleName + $class,如果有扩展,还需要增加ext前缀。 + * If no extension file, target class name is $moduleName + $class, else with 'ext' as the prefix. + */ + $targetClass = class_exists('ext' . $appName . $moduleName. $class) ? 'ext' . $appName . $moduleName . $class : $appName . $moduleName . $class; + if(!class_exists($targetClass)) + { + $targetClass = class_exists('ext' . $moduleName. $class) ? 'ext' . $moduleName . $class : $moduleName . $class; + if(!class_exists($targetClass)) $this->triggerError(" The $class $targetClass not found", __FILE__, __LINE__, $exit = true); + } + + /** + * 因为zen继承自control,tao继承自model,构造函数里会调用loadTarget方法,赋默认值值防止递归调用。 + */ + if($class == 'zen' || $class == 'tao') $loadedTargets[$class][$appName][$moduleName] = false; + + /** + * 初始化target 对象并返回。 + * Init the target object and return it. + */ + $target = new $targetClass($appName); + $loadedTargets[$class][$appName][$moduleName] = $target; + return $target; + } + /** * 设置请求的参数(PATH_INFO 方式)。 * Set the params by PATH_INFO. From b47999a32e637b232fbb69731a7581d296b1888e Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 10:59:44 +0800 Subject: [PATCH 25/39] * Call the loadTarget function to load the model object of a module. --- framework/base/control.class.php | 56 ++++++-------------------------- 1 file changed, 10 insertions(+), 46 deletions(-) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index 149e9ed108..c26bf22c56 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -262,37 +262,23 @@ class baseControl } /** - * 加载指定模块的model文件。 - * Load the model file of one module. + * 加载指定模块的model对象。 + * Load the model object of one module. * * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. - * @param string $type 对象的类型,可选值 model、zen、tao,默认为 model。The type of the object, optional values model, zen, tao, the default is model. * @access public * @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object. */ - public function loadModel($moduleName = '', $appName = '', $type = 'model') + public function loadModel($moduleName = '', $appName = '') { - if(empty($moduleName)) $moduleName = $this->moduleName; - if(empty($appName)) $appName = $this->appName; - - $objectName = $type == 'model' ? $moduleName : $moduleName . ucfirst($type); - - global $loadedModels; - if(isset($loadedModels[$type][$appName][$moduleName])) - { - $this->{$objectName} = $loadedModels[$type][$appName][$moduleName]; - if($type == 'model') $this->dao = $this->{$objectName}->dao; - return $this->{$objectName}; - } - - $modelFile = $this->app->setModelFile($moduleName, $appName, $type); + $model = $this->app->loadTarget($moduleName, $appName); /** - * 如果没有model文件,尝试加载config配置信息。 - * If no model file, try load config. + * 如果加载model失败,尝试加载config, lang配置信息。 + * If model is not loaded, try load config and lang. */ - if(!helper::import($modelFile)) + if(!$model) { $this->app->loadModuleConfig($moduleName, $appName); $this->app->loadLang($moduleName, $appName); @@ -300,31 +286,9 @@ class baseControl return false; } - /** - * 如果没有扩展文件,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 . $type) ? 'ext' . $appName . $moduleName . $type : $appName . $moduleName . $type; - if(!class_exists($modelClass)) - { - $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); - } - - /** - * 因为zen继承control,构造函数里会调用loadModel方法,赋默认值值防止递归调用。 - */ - if($type == 'zen') $loadedModels[$type][$appName][$moduleName] = false; - - /** - * 初始化model对象,在control对象中可以通过$this->$objectName来引用。同时将dao对象赋为control对象的成员变量,将view对象赋为zen对象的成员变量,方便引用。 - * Init the model object thus you can try $this->$objectName to access it. Also assign the $dao object as a member of control object, assign the $view object as a member of zen object. - */ - $this->{$objectName} = new $modelClass($appName); - if($type == 'model') $this->dao = $this->{$objectName}->dao; - if($type == 'zen') $this->{$objectName}->view = $this->view; - $loadedModels[$type][$appName][$moduleName] = $this->{$objectName}; - return $this->$objectName; + $this->{$moduleName} = $model; + $this->dao = $model->dao; + return $model; } /** From 8c185e925c4b09f5e3f76cb7ffb7bffd7529cbe1 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 11:00:27 +0800 Subject: [PATCH 26/39] * Add function to load the zen object of a module. --- framework/base/control.class.php | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index c26bf22c56..f902c426e1 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -231,6 +231,8 @@ class baseControl * Set super vars. */ $this->setSuperVars(); + + $this->loadZen($moduleName, $appName); } //-------------------- Model相关方法(Model related methods) --------------------// @@ -291,6 +293,38 @@ class baseControl return $model; } + /** + * 加载指定模块的zen对象。 + * Load the zen object of one module. + * + * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. + * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. + * @access public + * @return object|bool 如果没有zen文件,返回false,否则返回zen对象。If no zen file, return false, else return the zen object. + */ + public function loadZen($moduleName = '', $appName = '') + { + $zen = $this->app->loadTarget($moduleName, $appName, 'zen'); + + /** + * 如果加载zen失败,尝试加载config, lang配置信息。 + * If zen is not loaded, try load config and lang. + */ + if(!$zen) + { + $this->app->loadModuleConfig($moduleName, $appName); + $this->app->loadLang($moduleName, $appName); + $this->dao = new dao(); + return false; + } + + $zen->view = $this->view; + + $zenObjectName = $moduleName . 'Zen'; + $this->{$zenObjectName} = $zen; + return $zen; + } + /** * 设置超级全局变量,方便直接引用。 * Set the super vars. From 08ce854f4185ce34cdd88959e6ea8ed13ffe8f8d Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 11:00:49 +0800 Subject: [PATCH 27/39] * Call the loadTarget function to load the model object of a module. --- framework/base/model.class.php | 47 ++-------------------------------- 1 file changed, 2 insertions(+), 45 deletions(-) diff --git a/framework/base/model.class.php b/framework/base/model.class.php index f370fd5248..d03f32e385 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -201,55 +201,12 @@ class baseModel * * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. - * @param string $type 对象的类型,可选值 model、zen、tao,默认为 model。The type of the object, optional values model, zen, tao, the default is model. * @access public * @return object|bool the model object or false if model file not exists. */ - public function loadModel($moduleName, $appName = '', $type = 'model') + public function loadModel($moduleName, $appName = '') { - if(empty($moduleName)) $moduleName = $this->moduleName; - if(empty($appName)) $appName = $this->appName; - - $objectName = $type == 'model' ? $moduleName : $moduleName . ucfirst($type); - - global $loadedModels; - if(isset($loadedModels[$type][$appName][$moduleName])) - { - $this->{$objectName} = $loadedModels[$type][$appName][$moduleName]; - return $this->{$objectName}; - } - - $modelFile = $this->app->setModelFile($moduleName, $appName, $type); - - /** - * 如果没有model文件,返回。 - * If no model file, return. - */ - if(!helper::import($modelFile)) return false; - - /** - * 如果没有扩展文件,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. $type) ? 'ext' . $appName . $moduleName . $type : $appName . $moduleName . $type; - if(!class_exists($modelClass)) - { - $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); - } - - /** - * 因为tao继承model,构造函数里会调用loadModel方法,赋默认值值防止递归调用。 - */ - if($type == 'tao') $loadedModels[$type][$appName][$moduleName] = false; - - /** - * 初始化model对象,在model对象中可以通过$this->$objectName来引用。 - * Init the model object thus you can try $this->$objectName to access it. - */ - $this->{$objectName} = new $modelClass($appName); - $loadedModels[$type][$appName][$moduleName] = $this->{$objectName}; - return $this->{$objectName}; + return $this->app->loadTarget($moduleName, $appName); } /** From 59ff8af5e62838650d2ab6141184c0cc6a87a022 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 11:05:37 +0800 Subject: [PATCH 28/39] * Add the loadTao function to load the tao object of a module. --- framework/base/model.class.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/framework/base/model.class.php b/framework/base/model.class.php index d03f32e385..3b4eba3e61 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -150,6 +150,8 @@ class baseModel $this->loadDAO(); $this->setSuperVars(); + + $this->loadTao($moduleName, $this->appName); } /** @@ -194,10 +196,10 @@ class baseModel } /** - * 加载一个模块的model。加载完成后,使用$this->$moduleName来访问这个model对象。 + * 加载一个模块的model对象。加载完成后,使用$this->$moduleName来访问这个model对象。 * 比如:loadModel('user')引入user模块的model实例对象,可以通过$this->user来访问它。 * - * Load the model of one module. After loaded, can use $this->$moduleName to visit the model object. + * Load the model object of one module. After loaded, can use $this->$moduleName to visit the model object. * * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. @@ -209,6 +211,22 @@ class baseModel return $this->app->loadTarget($moduleName, $appName); } + /** + * 加载一个模块的tao对象。加载完成后,使用$this->$moduleName来访问这个tao对象。 + * 比如:loadModel('user')引入user模块的tao实例对象,可以通过$this->user来访问它。 + * + * Load the tao object of one module. After loaded, can use $this->$moduleName to visit the tao object. + * + * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. + * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. + * @access public + * @return object|bool the tao object or false if tao file not exists. + */ + public function loadTao($moduleName, $appName = '') + { + return $this->app->loadTarget($moduleName, $appName, 'tao'); + } + /** * 加载model的class扩展,主要是为了开发加密代码使用。 * 可以将主要的逻辑存放到$moduleName/ext/model/class/$extensionName.class.php中。 From 646e61e3db077edc62f55e7d3260bdb5352c1965 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 11:07:19 +0800 Subject: [PATCH 29/39] * Revert the loadModel function. --- framework/control.class.php | 7 ++----- framework/model.class.php | 20 ++------------------ 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/framework/control.class.php b/framework/control.class.php index 428a8ff520..55edf6a042 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -33,8 +33,6 @@ class control extends baseControl { parent::__construct($moduleName, $methodName, $appName); - $this->loadModel($moduleName, $appName, 'zen'); - $this->app->setOpenApp(); if(defined('IN_USE') or (defined('RUN_MODE') and RUN_MODE != 'api')) $this->setPreference(); @@ -178,13 +176,12 @@ class control extends baseControl * * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. - * @param string $type 对象的类型,可选值 model、zen、tao,默认为 model。The type of the object, optional values model, zen, tao, the default is model. * @access public * @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object. */ - public function loadModel($moduleName = '', $appName = '', $type = 'model') + public function loadModel($moduleName = '', $appName = '') { - return parent::loadModel($moduleName, '', $type); + return parent::loadModel($moduleName); } /** diff --git a/framework/model.class.php b/framework/model.class.php index 977e69c42b..0c83223166 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -20,21 +20,6 @@ include dirname(__FILE__) . '/base/model.class.php'; class model extends baseModel { - /** - * Load the tao file. - * - * @param string $appName - * @access public - * @return void - */ - public function __construct($appName = '') - { - parent::__construct($appName); - - $moduleName = $this->getModuleName(); - $this->loadModel($moduleName, $appName, 'tao'); - } - /** * 企业版部分功能是从然之合并过来的。ZDOO代码中调用loadModel方法时传递了一个非空的appName,在禅道中会导致错误。 * 调用父类的loadModel方法来避免这个错误。 @@ -43,13 +28,12 @@ class model extends baseModel * * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. - * @param string $type 对象的类型,可选值 model、zen、tao,默认为 model。The type of the object, optional values model, zen, tao, the default is model. * @access public * @return object|bool the model object or false if model file not exists. */ - public function loadModel($moduleName, $appName = '', $type = 'model') + public function loadModel($moduleName, $appName = '') { - return parent::loadModel($moduleName, '', $type); + return parent::loadModel($moduleName); } /** From ec23b692dfb3a12aca84eae745465a1eec99543f Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 11:19:50 +0800 Subject: [PATCH 30/39] * Extend the loadZen function to be compatible with the codes merged from ZDOO. --- framework/control.class.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/framework/control.class.php b/framework/control.class.php index 55edf6a042..765fa342ba 100644 --- a/framework/control.class.php +++ b/framework/control.class.php @@ -184,6 +184,22 @@ class control extends baseControl return parent::loadModel($moduleName); } + /** + * 企业版部分功能是从然之合并过来的。ZDOO代码中调用loadZen方法时传递了一个非空的appName,在禅道中会导致错误。 + * 调用父类的loadZen方法来避免这个错误。 + * Some codes merged from ZDOO called the function loadZen with a non-empty appName which causes an error in zentao. + * Call the parent function with empty appName to avoid this error. + * + * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. + * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. + * @access public + * @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object. + */ + public function loadZen($moduleName = '', $appName = '') + { + return parent::loadZen($moduleName); + } + /** * 加载model的class扩展,主要是为了开发加密代码使用。 * 可以将主要的逻辑存放到$moduleName/ext/model/class/$extensionName.class.php中。 From c220eda19e70aa1fe492484d6d55aec0e769b620 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 11:21:06 +0800 Subject: [PATCH 31/39] * Extend the loadTao function to be compatible with the codes merged from ZDOO. --- framework/model.class.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/framework/model.class.php b/framework/model.class.php index 0c83223166..2287fb1103 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -36,6 +36,22 @@ class model extends baseModel return parent::loadModel($moduleName); } + /** + * 企业版部分功能是从然之合并过来的。ZDOO代码中调用loadTao方法时传递了一个非空的appName,在禅道中会导致错误。 + * 调用父类的loadTao方法来避免这个错误。 + * Some codes merged from ZDOO called the function loadTao with a non-empty appName which causes an error in zentao. + * Call the parent function with empty appName to avoid this error. + * + * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. + * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. + * @access public + * @return object|bool the model object or false if model file not exists. + */ + public function loadTao($moduleName, $appName = '') + { + return parent::loadTao($moduleName); + } + /** * 删除记录 * Delete one record. From 9b7ccef03476cd7f8bd1347a438e9a0aca72863c Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 13:56:11 +0800 Subject: [PATCH 32/39] * Optimize the comment. --- framework/base/control.class.php | 12 ++++++++---- framework/base/model.class.php | 10 +++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index f902c426e1..a1a4c28c07 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -264,8 +264,10 @@ class baseControl } /** - * 加载指定模块的model对象。 - * Load the model object of one module. + * 加载一个模块的model对象。加载完成后,使用$this->$moduleName来访问这个model对象。 + * 比如:loadModel('user')引入user模块的model实例对象,可以通过$this->user来访问它。 + * + * Load the model object of one module. After loaded, can use $this->$moduleName to visit the model object. * * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. @@ -294,8 +296,10 @@ class baseControl } /** - * 加载指定模块的zen对象。 - * Load the zen object of one module. + * 加载一个模块的zen对象。加载完成后,使用$this->{$moduleName}Zen来访问这个zen对象。 + * 比如:loadZen('user')引入user模块的zen实例对象,可以通过$this->userZen来访问它。 + * + * Load the zen object of one module. After loaded, can use $this->{$moduleName}Zen to visit the zen object. * * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. diff --git a/framework/base/model.class.php b/framework/base/model.class.php index 3b4eba3e61..84437a42f8 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -204,7 +204,7 @@ class baseModel * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. * @access public - * @return object|bool the model object or false if model file not exists. + * @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object. */ public function loadModel($moduleName, $appName = '') { @@ -212,15 +212,15 @@ class baseModel } /** - * 加载一个模块的tao对象。加载完成后,使用$this->$moduleName来访问这个tao对象。 - * 比如:loadModel('user')引入user模块的tao实例对象,可以通过$this->user来访问它。 + * 加载一个模块的tao对象。加载完成后,使用$this->{$moduleName}Tao来访问这个tao对象。 + * 比如:loadTao('user')引入user模块的tao实例对象,可以通过$this->userTao来访问它。 * - * Load the tao object of one module. After loaded, can use $this->$moduleName to visit the tao object. + * Load the tao object of one module. After loaded, can use $this->{$moduleName}Tao to visit the tao object. * * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. * @access public - * @return object|bool the tao object or false if tao file not exists. + * @return object|bool 如果没有tao文件,返回false,否则返回tao对象。If no tao file, return false, else return the tao object. */ public function loadTao($moduleName, $appName = '') { From a3da6393be56d0e4f7f157e882a342bc470098d8 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 13:57:38 +0800 Subject: [PATCH 33/39] * Don't need load config and lang when load the zen object. --- framework/base/control.class.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index a1a4c28c07..824ad69458 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -309,18 +309,7 @@ class baseControl public function loadZen($moduleName = '', $appName = '') { $zen = $this->app->loadTarget($moduleName, $appName, 'zen'); - - /** - * 如果加载zen失败,尝试加载config, lang配置信息。 - * If zen is not loaded, try load config and lang. - */ - if(!$zen) - { - $this->app->loadModuleConfig($moduleName, $appName); - $this->app->loadLang($moduleName, $appName); - $this->dao = new dao(); - return false; - } + if(!$zen) return false; $zen->view = $this->view; From 3c46850e0ed65081b97957bbfbd7d99821c38554 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 13:58:18 +0800 Subject: [PATCH 34/39] * Return false when the target object is empty to avoid assigning property to a null object. --- framework/base/model.class.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/framework/base/model.class.php b/framework/base/model.class.php index 84437a42f8..21f00298e8 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -208,7 +208,11 @@ class baseModel */ public function loadModel($moduleName, $appName = '') { - return $this->app->loadTarget($moduleName, $appName); + $model = $this->app->loadTarget($moduleName, $appName); + if(!$model) return false; + + $this->{$moduleName} = $model; + return $model; } /** @@ -224,7 +228,12 @@ class baseModel */ public function loadTao($moduleName, $appName = '') { - return $this->app->loadTarget($moduleName, $appName, 'tao'); + $tao = $this->app->loadTarget($moduleName, $appName, 'tao'); + if(!$tao) return false; + + $taoObjectName = $moduleName . 'Tao'; + $this->{$taoObjectName} = $tao; + return $tao; } /** From 3848e7312d2547f023027d055b6ac0b61a0f0121 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Wed, 26 Oct 2022 14:00:12 +0800 Subject: [PATCH 35/39] * Use $this->moduleName instead of $moduleName for it might be empty sometimes. --- framework/base/control.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index 824ad69458..6883700bbb 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -232,7 +232,7 @@ class baseControl */ $this->setSuperVars(); - $this->loadZen($moduleName, $appName); + $this->loadZen($this->moduleName, $appName); } //-------------------- Model相关方法(Model related methods) --------------------// From c2e9f3dab2f4c6184210ba6a3691bb132ed987fa Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Thu, 27 Oct 2022 15:48:48 +0800 Subject: [PATCH 36/39] * Add function to load target file. --- framework/base/router.class.php | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/framework/base/router.class.php b/framework/base/router.class.php index 13bc9579ac..2426dc0ca9 100644 --- a/framework/base/router.class.php +++ b/framework/base/router.class.php @@ -1777,16 +1777,30 @@ class baseRouter * * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. - * @param string $type 对象的类型,可选值 model、zen、tao,默认为 model。The type of the object, optional values model, zen, tao, the default is model. * @access public * @return string the model file */ - public function setModelFile($moduleName, $appName = '', $type = 'model') + public function setModelFile($moduleName, $appName = '') + { + return $this->setTargetFile($moduleName, $appName); + } + + /** + * 设置一个模块的target文件,如果存在target扩展,一起合并。 + * Set the target file of one module. If there's an extension file, merge it with the main target file. + * + * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name. + * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name. + * @param string $class 对象的类型,可选值 target、zen、tao,默认为 target。The type of the object, optional values target, zen, tao, the default is target. + * @access public + * @return string the target file + */ + public function setTargetFile($moduleName, $appName = '', $class = 'model') { if($appName == '') $appName = $this->getAppName(); /* 设置主model文件。 Set the main model file. */ - $mainModelFile = $this->getModulePath($appName, $moduleName) . "{$type}.php"; + $mainModelFile = $this->getModulePath($appName, $moduleName) . "{$class}.php"; if($this->config->framework->extensionLevel == 0) return $mainModelFile; /* 计算扩展的文件和hook文件。Compute the extension files and hook files. */ @@ -1795,7 +1809,7 @@ class baseRouter $apiFiles = array(); $siteExtended = false; - $modelExtPaths = $this->getModuleExtPath($appName, $moduleName, $type); + $modelExtPaths = $this->getModuleExtPath($appName, $moduleName, $class); foreach($modelExtPaths as $extType => $modelExtPath) { if(empty($modelExtPath)) continue; @@ -1817,7 +1831,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() . $type . DS . $extModelPrefix; + $mergedModelDir = $this->getTmpRoot() . $class . DS . $extModelPrefix; $mergedModelFile = $mergedModelDir . $moduleName . '.php'; if(!is_dir($mergedModelDir)) mkdir($mergedModelDir, 0755, true); @@ -1825,8 +1839,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, $extFiles, $mergedModelDir, $type); - $this->mergeModelHookFiles($moduleName, $mainModelFile, $modelLines, $hookFiles, $mergedModelDir, $mergedModelFile, $apiFiles, $type); + $modelLines = $this->mergeModelExtFiles($moduleName, $extFiles, $mergedModelDir, $class); + $this->mergeModelHookFiles($moduleName, $mainModelFile, $modelLines, $hookFiles, $mergedModelDir, $mergedModelFile, $apiFiles, $class); return $mergedModelFile; } @@ -2199,7 +2213,7 @@ class baseRouter global $loadedTargets; if(isset($loadedTargets[$class][$appName][$moduleName])) return $loadedTargets[$class][$appName][$moduleName]; - $targetFile = $this->setModelFile($moduleName, $appName, $class); + $targetFile = $this->setTargetFile($moduleName, $appName, $class); /** * 如果没有target文件,返回false。 From 9b6e282725325f39e99cf03234482a41356dd6f2 Mon Sep 17 00:00:00 2001 From: liugang Date: Tue, 1 Nov 2022 09:45:25 +0800 Subject: [PATCH 37/39] * Load zen class in the control class and its subclasses. --- framework/base/control.class.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/framework/base/control.class.php b/framework/base/control.class.php index 6883700bbb..0005a24389 100644 --- a/framework/base/control.class.php +++ b/framework/base/control.class.php @@ -232,7 +232,14 @@ class baseControl */ $this->setSuperVars(); - $this->loadZen($this->moduleName, $appName); + /** + * 读取当前模块的zen类。 + * Load the zen file auto. + */ + $zenClass = $this->moduleName . 'Zen'; + $selfClass = get_class($this); + $parentClasses = class_parents($this); + if($selfClass != $zenClass && !isset($parentClasses[$zenClass])) $this->loadZen($this->moduleName, $appName); } //-------------------- Model相关方法(Model related methods) --------------------// From 8220ce7025d90bd630e9dbec3731caba8a0725cf Mon Sep 17 00:00:00 2001 From: liugang Date: Tue, 1 Nov 2022 09:47:09 +0800 Subject: [PATCH 38/39] * Load tao class in the model class and its subclasses. --- framework/base/model.class.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/framework/base/model.class.php b/framework/base/model.class.php index 21f00298e8..bf4a0136ab 100644 --- a/framework/base/model.class.php +++ b/framework/base/model.class.php @@ -151,7 +151,14 @@ class baseModel $this->loadDAO(); $this->setSuperVars(); - $this->loadTao($moduleName, $this->appName); + /** + * 读取当前模块的tao类。 + * Load the tao file auto. + */ + $taoClass = $moduleName . 'Tao'; + $selfClass = get_class($this); + $parentClasses = class_parents($this); + if($selfClass != $taoClass && !isset($parentClasses[$taoClass])) $this->loadTao($moduleName, $this->appName); } /** From 540cd0e3c585ca4990174dee147d7b5a5bf00403 Mon Sep 17 00:00:00 2001 From: liugang Date: Tue, 1 Nov 2022 09:59:32 +0800 Subject: [PATCH 39/39] * Rename model to target. --- framework/base/router.class.php | 158 +++++++++++++++++--------------- 1 file changed, 83 insertions(+), 75 deletions(-) diff --git a/framework/base/router.class.php b/framework/base/router.class.php index 2426dc0ca9..c1e65e842a 100644 --- a/framework/base/router.class.php +++ b/framework/base/router.class.php @@ -1799,9 +1799,9 @@ class baseRouter { if($appName == '') $appName = $this->getAppName(); - /* 设置主model文件。 Set the main model file. */ - $mainModelFile = $this->getModulePath($appName, $moduleName) . "{$class}.php"; - if($this->config->framework->extensionLevel == 0) return $mainModelFile; + /* 设置主target文件。 Set the main target file. */ + $mainTargetFile = $this->getModulePath($appName, $moduleName) . "{$class}.php"; + if($this->config->framework->extensionLevel == 0) return $mainTargetFile; /* 计算扩展的文件和hook文件。Compute the extension files and hook files. */ $hookFiles = array(); @@ -1809,14 +1809,14 @@ class baseRouter $apiFiles = array(); $siteExtended = false; - $modelExtPaths = $this->getModuleExtPath($appName, $moduleName, $class); - foreach($modelExtPaths as $extType => $modelExtPath) + $targetExtPaths = $this->getModuleExtPath($appName, $moduleName, $class); + foreach($targetExtPaths as $extType => $targetExtPath) { - if(empty($modelExtPath)) continue; + if(empty($targetExtPath)) continue; - $tmpHookFiles = helper::ls($modelExtPath . 'hook/', '.php'); - $tmpExtFiles = helper::ls($modelExtPath, '.php'); - $tmpAPIFiles = helper::ls($modelExtPath, '.api'); + $tmpHookFiles = helper::ls($targetExtPath . 'hook/', '.php'); + $tmpExtFiles = helper::ls($targetExtPath, '.php'); + $tmpAPIFiles = helper::ls($targetExtPath, '.api'); $hookFiles = array_merge($hookFiles, $tmpHookFiles); $extFiles = array_merge($extFiles, $tmpExtFiles); $apiFiles = array_merge($apiFiles, $tmpAPIFiles); @@ -1825,119 +1825,127 @@ class baseRouter } /* 如果没有扩展文件,返回主文件。 If no extension or hook files, return the main file directly. */ - if(empty($extFiles) and empty($hookFiles) and empty($apiFiles)) return $mainModelFile; + if(empty($extFiles) and empty($hookFiles) and empty($apiFiles)) return $mainTargetFile; - /* 计算合并之后的modelFile路径。Compute the merged model file path. */ - $extModelPrefix = $this->config->edition . DS . $this->config->vision . DS; - if($siteExtended and !empty($this->siteCode)) $extModelPrefix .= $this->siteCode[0] . DS . $this->siteCode; + /* 计算合并之后的targetFile路径。Compute the merged target file path. */ + $extTargetPrefix = $this->config->edition . DS . $this->config->vision . DS; + if($siteExtended and !empty($this->siteCode)) $extTargetPrefix .= $this->siteCode[0] . DS . $this->siteCode; - $mergedModelDir = $this->getTmpRoot() . $class . DS . $extModelPrefix; - $mergedModelFile = $mergedModelDir . $moduleName . '.php'; - if(!is_dir($mergedModelDir)) mkdir($mergedModelDir, 0755, true); + $mergedTargetDir = $this->getTmpRoot() . $class . DS . $extTargetPrefix; + $mergedTargetFile = $mergedTargetDir . $moduleName . '.php'; + if(!is_dir($mergedTargetDir)) mkdir($mergedTargetDir, 0755, true); - /* 判断生成的缓存文件是否需要更新。 Judge whether the merged model file needed update or not. */ - if(!$this->needModelFileUpdate($mergedModelFile, $extFiles, $hookFiles, $apiFiles, $modelExtPaths, $mainModelFile)) return $mergedModelFile; + /* 判断生成的缓存文件是否需要更新。 Judge whether the merged target file needed update or not. */ + if(!$this->needTargetFileUpdate($mergedTargetFile, $extFiles, $hookFiles, $apiFiles, $targetExtPaths, $mainTargetFile)) return $mergedTargetFile; /* 合并扩展和hook文件。Merge the extension and hook files. */ - $modelLines = $this->mergeModelExtFiles($moduleName, $extFiles, $mergedModelDir, $class); - $this->mergeModelHookFiles($moduleName, $mainModelFile, $modelLines, $hookFiles, $mergedModelDir, $mergedModelFile, $apiFiles, $class); + $targetLines = $this->mergeTargetExtFiles($moduleName, $extFiles, $mergedTargetDir, $class); + $this->mergeTargetHookFiles($moduleName, $mainTargetFile, $targetLines, $hookFiles, $mergedTargetDir, $mergedTargetFile, $apiFiles, $class); - return $mergedModelFile; + return $mergedTargetFile; } /** - * 检查合并之后的model文件是否需要更新。Check whether the merged model file need update or not. + * 检查合并之后的target文件是否需要更新。Check whether the merged target file need update or not. * - * @param string $mergedModelFile + * @param string $mergedTargetFile * @param array $extFiles * @param array $hookFiles * @param array $apiFiles - * @param string $modelExtPaths - * @param string $mainModelFile + * @param string $targetExtPaths + * @param string $mainTargetFile * @access public * @return bool */ - public function needModelFileUpdate($mergedModelFile, $extFiles, $hookFiles, $apiFiles, $modelExtPaths, $mainModelFile) + public function needTargetFileUpdate($mergedTargetFile, $extFiles, $hookFiles, $apiFiles, $targetExtPaths, $mainTargetFile) { - $lastTime = file_exists($mergedModelFile) ? filemtime($mergedModelFile) : 0; + $lastTime = file_exists($mergedTargetFile) ? filemtime($mergedTargetFile) : 0; foreach($extFiles as $extFile) if(filemtime($extFile) > $lastTime) return true; foreach($hookFiles as $hookFile) if(filemtime($hookFile) > $lastTime) return true; foreach($apiFiles as $apiFile) if(filemtime($apiFile) > $lastTime) return true; - $modelExtPath = $modelExtPaths['common']; - $modelHookPath = $modelExtPaths['common'] . 'hook/'; - if(is_dir($modelExtPath ) and filemtime($modelExtPath) > $lastTime) return true; - if(is_dir($modelHookPath) and filemtime($modelHookPath) > $lastTime) return true; + $targetExtPath = $targetExtPaths['common']; + $targetHookPath = $targetExtPaths['common'] . 'hook/'; + if(is_dir($targetExtPath ) and filemtime($targetExtPath) > $lastTime) return true; + if(is_dir($targetHookPath) and filemtime($targetHookPath) > $lastTime) return true; - if(!empty($modelExtPaths['site'])) + if(!empty($targetExtPaths['site'])) { - $modelExtPath = $modelExtPaths['site']; - $modelHookPath = $modelExtPaths['site'] . 'hook/'; - if(is_dir($modelExtPath ) and filemtime($modelExtPath) > $lastTime) return true; - if(is_dir($modelHookPath) and filemtime($modelHookPath) > $lastTime) return true; + $targetExtPath = $targetExtPaths['site']; + $targetHookPath = $targetExtPaths['site'] . 'hook/'; + if(is_dir($targetExtPath ) and filemtime($targetExtPath) > $lastTime) return true; + if(is_dir($targetHookPath) and filemtime($targetHookPath) > $lastTime) return true; } - if(filemtime($mainModelFile) > $lastTime) return true; + if(filemtime($mainTargetFile) > $lastTime) return true; return false; } /** - * 将model的扩展文件合并在一起。Merge model ext files. + * 将target的扩展文件合并在一起。Merge target ext files. * * @param string $moduleName * @param array $extFiles - * @param string $mergedModelDir - * @param string $type + * @param string $mergedTargetDir + * @param string $class model | zen | tao * @access public * @return void */ - public function mergeModelExtFiles($moduleName, $extFiles, $mergedModelDir, $type = 'model') + public function mergeTargetExtFiles($moduleName, $extFiles, $mergedTargetDir, $class = 'model') { /* 设置类名。Set the class names. */ - $modelClass = $moduleName . ucfirst($type); - $tmpModelClass = "tmpExt$modelClass"; + $targetClass = $moduleName . ucfirst($class); + $tmpTargetClass = "tmpExt$targetClass"; /* 开始拼装代码。Prepare the codes. */ - $modelLines = "getModulePath('', '$moduleName') . " . "'$type.php');\n"; - $modelLines .= "class $tmpModelClass extends $modelClass \n{\n"; + $targetLines = "getModulePath('', '$moduleName') . " . "'$class.php');\n"; + $targetLines .= "class $tmpTargetClass extends $targetClass \n{\n"; - /* 将扩展文件的代码合并到代码中。Cycle all the extension files and merge them into model lines. */ - $extModels = array(); - foreach($extFiles as $extFile) $extModels[basename($extFile)] = $extFile; - foreach($extModels as $extModel) $modelLines .= self::removePHPTAG($extModel); + /* 将扩展文件的代码合并到代码中。Cycle all the extension files and merge them into target lines. */ + $extTargets = array(); + foreach($extFiles as $extFile) $extTargets[basename($extFile)] = $extFile; + foreach($extTargets as $extTarget) $targetLines .= self::removePHPTAG($extTarget); /* 做个标记,方便后面替换代码使用。Make a mark for replacing codes. */ $replaceMark = '//**//'; - $modelLines .= "\n$replaceMark\n}"; + $targetLines .= "\n$replaceMark\n}"; - /* 生成一个临时的model扩展文件,并加载,用于后续的hook文件加载使用。Create a tmp merged model file and import it for merge hook codes using. */ - $tmpModelFile = $mergedModelDir . "tmp$moduleName.php"; - if(@file_put_contents($tmpModelFile, $modelLines)) + /* 生成一个临时的target扩展文件,并加载,用于后续的hook文件加载使用。Create a tmp merged target file and import it for merge hook codes using. */ + $tmpTargetFile = $mergedTargetDir . "tmp$moduleName.php"; + if(@file_put_contents($tmpTargetFile, $targetLines)) { - if(!class_exists($tmpModelClass)) include $tmpModelFile; - return $modelLines; + if(!class_exists($tmpTargetClass)) include $tmpTargetFile; + return $targetLines; } - $this->triggerError("ERROR: $tmpModelFile not writable.", __FILE__, __LINE__, true); + $this->triggerError("ERROR: $tmpTargetFile not writable.", __FILE__, __LINE__, true); } /** - * 合并model的hook脚本。Merge hook files for a model. + * 合并target的hook脚本。Merge hook files for a target. * + * @param string $moduleName + * @param string $mainTargetFile + * @param string $targetLines + * @param array $hookFiles + * @param string $mergedTargetDir + * @param string $mergedTargetFile + * @param array $apiFiles + * @param string $class model | zen | tao * @access public * @return void */ - public function mergeModelHookFiles($moduleName, $mainModelFile, $modelLines, $hookFiles, $mergedModelDir, $mergedModelFile, $apiFiles, $type = 'model') + public function mergeTargetHookFiles($moduleName, $mainTargetFile, $targetLines, $hookFiles, $mergedTargetDir, $mergedTargetFile, $apiFiles, $class = 'model') { /* 定义相关变量。Init vars. */ - $modelClass = $moduleName . ucfirst($type); - $extModelClass = 'ext' . $modelClass; - $tmpModelClass = 'tmpExt' . $modelClass; - $tmpModelFile = $mergedModelDir . "tmp$moduleName.php"; + $targetClass = $moduleName . ucfirst($class); + $extTargetClass = 'ext' . $targetClass; + $tmpTargetClass = 'tmpExt' . $targetClass; + $tmpTargetFile = $mergedTargetDir . "tmp$moduleName.php"; $replaceMark = '//**//'; /* 读取hook文件。Get hook codes need to merge. */ @@ -1961,31 +1969,31 @@ class baseRouter /* 合并Hook文件。Cycle the hook methods and merge hook codes. */ $hookedMethods = array_keys($hookCodes); - $mainModelCodes = file($mainModelFile); - $mergedModelCodes = file($tmpModelFile); + $mainTargetCodes = file($mainTargetFile); + $mergedTargetCodes = file($tmpTargetFile); foreach($hookedMethods as $method) { /* 通过反射获得hook脚本对应的方法所在的文件和起止行数。Reflection the hooked method to get it's defined position. */ - if(!method_exists($tmpModelClass, $method)) continue; - $methodRelfection = new reflectionMethod($tmpModelClass, $method); + if(!method_exists($tmpTargetClass, $method)) continue; + $methodRelfection = new reflectionMethod($tmpTargetClass, $method); $definedFile = $methodRelfection->getFileName(); $startLine = $methodRelfection->getStartLine(); $endLine = $methodRelfection->getEndLine(); /* 将Hook脚本和老的代码合并在一起,并替换原来的定义。Merge hook codes with old codes and replace back. */ - $oldCodes = $definedFile == $tmpModelFile ? $mergedModelCodes : $mainModelCodes; + $oldCodes = $definedFile == $tmpTargetFile ? $mergedTargetCodes : $mainTargetCodes; $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); - if($definedFile == $tmpModelFile) $modelLines = str_replace($oldCodes, $newCodes, $modelLines); - if($definedFile != $tmpModelFile) $modelLines = str_replace($replaceMark, $newCodes . "\n$replaceMark", $modelLines); + if($definedFile == $tmpTargetFile) $targetLines = str_replace($oldCodes, $newCodes, $targetLines); + if($definedFile != $tmpTargetFile) $targetLines = str_replace($replaceMark, $newCodes . "\n$replaceMark", $targetLines); } - /* 保存最终的Model文件。Save the last merged model file. */ - $modelLines = str_replace($tmpModelClass, $extModelClass, $modelLines); - file_put_contents($mergedModelFile, $modelLines); - unlink($tmpModelFile); + /* 保存最终的Target文件。Save the last merged target file. */ + $targetLines = str_replace($tmpTargetClass, $extTargetClass, $targetLines); + file_put_contents($mergedTargetFile, $targetLines); + unlink($tmpTargetFile); } /**