/** * 通过分析控制器文件,获取指定模块的控制器信息,并入库 * @param integer $amcaId * @return void */ public function synch($amcaId) { Log::echoTrace('Synch Begin ...'); // 从数据库中读取模块数据 Log::echoTrace('Query mod from table Begin ...'); $ret = $this->findByPk($amcaId); if ($ret['err_no'] !== ErrorNo::SUCCESS_NUM) { Log::errExit(__LINE__, 'Query mod from table Failed!'); } $mod = $ret['data']; if (!$this->isMod($mod['category'])) { Log::errExit(__LINE__, 'Amcas must be "' . DataAmcas::CATEGORY_MOD . '" category!'); } Log::echoTrace('Query mod from table Successfully'); // 从数据库中读取应用数据 Log::echoTrace('Query app from table Begin ...'); $ret = $this->findByPk($mod['amca_pid']); if ($ret['err_no'] !== ErrorNo::SUCCESS_NUM) { Log::errExit(__LINE__, 'Query app from table Failed!'); } $app = $ret['data']; Log::echoTrace('Query app from table Successfully'); $appName = $app['amca_name']; $modName = $mod['amca_name']; $modId = $mod['amca_id']; // 从数据库中读取控制器数据 Log::echoTrace('Query ctrls from table Begin ...'); $ret = $this->findAllByAmcaPid($modId); if ($ret['err_no'] !== ErrorNo::SUCCESS_NUM) { Log::errExit(__LINE__, 'Query ctrls from table Failed!'); } $dbCtrls = array(); foreach ($ret['data'] as $rows) { $dbCtrls[$rows['amca_name']] = $rows; } Log::echoTrace('Query ctrls from table Successfully'); // 从文件中读取控制器数据 Log::echoTrace('Query ctrls from files Begin ...'); $fileManager = new FileManager(); $directory = DIR_ROOT . DS . 'app' . DS . $appName . DS . 'modules' . DS . $modName . DS . 'controller'; if (!$fileManager->isDir($directory)) { Log::errExit(__LINE__, sprintf('Ctrl Path "%s" is not a valid directory.', $directory)); } $ctrls = array(); $sort = 0; $filePaths = $fileManager->scanDir($directory); foreach ($filePaths as $filePath) { $ctrlName = basename($filePath, '.php'); if ($ctrlName === 'index.html') { continue; } $clsName = 'modules\\' . $modName . '\\controller\\' . $ctrlName; require_once $filePath; $reflector = new \ReflectionClass($clsName); $amcaName = strtolower(substr($ctrlName, 0, -10)); $prompt = preg_replace('/.+class\\s+file\\s+\\*\\s+(\\S+)\\s+\\*\\s+\\@author.+/is', '\\1', $reflector->getDocComment()); $ctrls[$amcaName] = array('amca_pid' => $modId, 'amca_name' => $amcaName, 'prompt' => $prompt, 'sort' => $sort++, 'category' => DataAmcas::CATEGORY_CTRL); } Log::echoTrace('Query ctrls from files Successfully'); Log::echoTrace('Analyser db and files Begin ...'); $amcas = array('insert' => array(), 'update' => array(), 'delete' => array()); foreach ($ctrls as $amcaName => $rows) { if (isset($dbCtrls[$amcaName])) { if ($dbCtrls[$amcaName]['prompt'] != $rows['prompt'] || $dbCtrls[$amcaName]['sort'] != $rows['sort']) { $amcas['update'][$dbCtrls[$amcaName]['amca_id']] = $rows; } } else { $amcas['insert'][] = $rows; } } foreach ($dbCtrls as $amcaName => $rows) { if (!isset($ctrls[$amcaName])) { $amcas['delete'][] = $rows['amca_id']; } } Log::echoTrace('Analyser db and files Successfully'); $dbAmcas = new DbAmcas(); Log::echoTrace('Import to db Begin ...'); foreach ($amcas['insert'] as $attributes) { $ret = $dbAmcas->create($attributes); if (!$ret) { Log::errExit(__LINE__, sprintf('Insert to table "%s" Failed!', $attributes['amca_name'])); } Log::echoTrace(sprintf('Insert into table "%s" Successfully', $attributes['amca_name'])); } foreach ($amcas['update'] as $amcaId => $attributes) { $ret = $dbAmcas->modifyByPk($amcaId, $attributes); if (!$ret) { Log::errExit(__LINE__, sprintf('Update table "%s" Failed!', $attributes['amca_name'])); } Log::echoTrace(sprintf('Update table "%s" Successfully', $attributes['amca_name'])); } foreach ($amcas['delete'] as $amcaId) { $ret = $dbAmcas->removeByPk($amcaId); if (!$ret) { Log::errExit(__LINE__, sprintf('Delete from table "%d" Failed!', $amcaId)); } Log::echoTrace(sprintf('Delete from "%d" Successfully', $amcaId)); } Log::echoTrace('Import to db Successfully'); Log::echoTrace('Synch Successfully'); }
/** * 初始化表单字段数据 * @return instance of modules\builder\model\gc\Schema */ protected function _initValidators() { $object = Service::getInstance('Validators', self::SRV_NAME); $tableName = $object->getTableName(); Log::echoTrace('Query from ' . $tableName . ' Begin ...'); foreach ($this->fields as $key => $fields) { $data = $object->findAllByAttributes(array('field_id' => $fields['field_id']), 'sort', 0, 1000); if ($data === false) { Log::errExit(__LINE__, 'Query from ' . $tableName . ' Failed!'); } $temp = array(); foreach ($data as $rows) { $validatorId = (int) $rows['validator_id']; $validatorName = trim($rows['validator_name']); $temp[$validatorId] = array('validator_id' => $validatorId, 'validator_name' => $validatorName, 'options' => $rows['options'], 'option_category' => strtolower(trim($rows['option_category'])), 'message' => trim($rows['message']), 'when' => strtolower(trim($rows['when'])), 'lang_key' => 'SRV_FILTER_' . $this->upTblName . '_' . $fields['up_field_name'] . '_' . strtoupper($validatorName)); } $this->fields[$key]['validators'] = $temp; } Log::echoTrace('Query from ' . $tableName . ' Successfully'); return $this; }
/** * 通过表Metadata生成Builders数据 * @param string $tblName * @return void */ public function gb($tblName) { Ap::getResponse()->contentType(); $tableNames = $this->_metadata->getTableNames($tblName); if (!in_array($tblName, $tableNames)) { Log::errExit(__LINE__, 'Table Name Not Exists!'); } Log::echoTrace('Generate Begin, Table Name "' . $tblName . '"'); $tableSchema = $this->_metadata->getTableSchema($tblName); $comments = $this->_metadata->getComments($tableSchema->name); $tblPrefix = $this->getDbProxy()->getTblprefix(); $tblPreLen = strlen($tblPrefix); Log::echoTrace('Import to builders Begin ...'); $params = array('builder_name' => isset($comments['__table__']) ? $comments['__table__'] : $tableSchema->name, 'tbl_name' => substr($tableSchema->name, $tblPreLen), 'tbl_profile' => DataBuilders::TBL_PROFILE_N, 'tbl_engine' => DataBuilders::TBL_ENGINE_INNODB, 'tbl_charset' => DataBuilders::TBL_CHARSET_UTF8, 'tbl_comment' => isset($comments['__table__']) ? $comments['__table__'] : '', 'srv_type' => DataBuilders::SRV_TYPE_NORMAL, 'srv_name' => 'undefined', 'app_name' => 'administrator', 'mod_name' => 'undefined', 'ctrl_name' => substr($tableSchema->name, strrpos($tableSchema->name, '_') + 1), 'cls_name' => substr($tableSchema->name, strrpos($tableSchema->name, '_') + 1), 'fk_column' => '', 'act_index_name' => 'index', 'act_view_name' => 'view', 'act_create_name' => 'create', 'act_modify_name' => 'modify', 'act_remove_name' => 'remove', 'index_row_btns' => array(DataBuilders::INDEX_ROW_BTNS_PENCIL, DataBuilders::INDEX_ROW_BTNS_REMOVE), 'description' => '', 'author_name' => UserIdentity::getNick(), 'author_mail' => UserIdentity::getName()); $mod = Service::getInstance('Builders', $this->_srvName); $builderId = $mod->create($params); if ($builderId > 0) { Log::echoTrace('Import to builders Successfully ...'); } else { $errors = $mod->getErrors(); Log::errExit(__LINE__, 'Import to builders Failed! ' . serialize($errors)); } Log::echoTrace('Import to builder_fields Begin ...'); $sort = 0; foreach ($tableSchema->columns as $columnSchema) { $sort++; if ($columnSchema->type === 'integer') { $columnLength = $columnSchema->size; } elseif (stripos($columnSchema->dbType, 'enum') !== false) { $columnLength = str_replace(array('\'', ','), array('', '|'), substr(substr($columnSchema->dbType, 5), 0, -1)); } elseif (stripos($columnSchema->dbType, 'char') !== false) { $columnLength = $columnSchema->size; } else { $columnLength = ''; } if ($columnSchema->isPrimaryKey) { $formRequired = DataFields::FORM_REQUIRED_N; } elseif (stripos($columnSchema->dbType, 'enum') !== false) { $formRequired = DataFields::FORM_REQUIRED_N; } else { $formRequired = DataFields::FORM_REQUIRED_Y; } if ($columnLength === 'y|n') { $typeId = 4; } elseif (stripos($columnSchema->dbType, 'enum') !== false) { $typeId = 5; } elseif ($columnSchema->isPrimaryKey) { $typeId = 9; } elseif ($columnSchema->type === 'integer') { $typeId = 2; } elseif (in_array($columnSchema->dbType, array('text', 'longtext'))) { $typeId = 10; } else { $typeId = 1; } $params = array('field_name' => $columnSchema->name, 'column_length' => $columnLength, 'column_auto_increment' => $columnSchema->isAutoIncrement ? DataFields::COLUMN_AUTO_INCREMENT_Y : DataFields::COLUMN_AUTO_INCREMENT_N, 'column_unsigned' => stripos($columnSchema->dbType, 'unsigned') !== false ? DataFields::COLUMN_UNSIGNED_Y : DataFields::COLUMN_UNSIGNED_N, 'column_comment' => isset($comments[$columnSchema->name]) ? $comments[$columnSchema->name] : '', 'builder_id' => $builderId, 'group_id' => 1, 'type_id' => $typeId, 'sort' => $sort, 'html_label' => isset($comments[$columnSchema->name]) ? $comments[$columnSchema->name] : $columnSchema->name, 'form_prompt' => '', 'form_required' => $formRequired, 'form_modifiable' => DataFields::FORM_MODIFIABLE_N, 'index_show' => DataFields::INDEX_SHOW_Y, 'index_sort' => $columnSchema->isPrimaryKey ? 1000 : $sort, 'form_create_show' => $columnSchema->isPrimaryKey ? DataFields::FORM_CREATE_SHOW_N : DataFields::FORM_CREATE_SHOW_Y, 'form_create_sort' => $sort, 'form_modify_show' => $columnSchema->isPrimaryKey ? DataFields::FORM_MODIFY_SHOW_N : DataFields::FORM_MODIFY_SHOW_Y, 'form_modify_sort' => $sort, 'form_search_show' => DataFields::FORM_SEARCH_SHOW_Y, 'form_search_sort' => $sort); $mod = Service::getInstance('Fields', $this->_srvName); $fieldId = $mod->create($params); if ($fieldId > 0) { Log::echoTrace('Import to builder_fields "' . $columnSchema->name . '" Successfully ...'); } else { $errors = $mod->getErrors(); Log::errExit(__LINE__, 'Import to builder_fields "' . $columnSchema->name . '" Failed! ' . serialize($errors)); } } Log::echoTrace('Import to builder_fields Successfully ...'); Log::echoTrace('Generate End, Table Name "' . $tblName . '"'); exit; }