/** * 流程处理 * * @return boolean * @throws \Lib\Exception */ public function process() { if ($this->_state === false) { if (\Lib\Params::getInstance()->showHelp() === true) { $this->getHelp(); } return false; } $db = $this->getDbResponse(); $op = \Lib\Options::getInstance(); if (empty($this->_dbname)) { \Lib\State::error('The database is not specified'); } \Lib\State::notice('Scanning the database table...'); $tables = $op->getTable(); if (empty($tables)) { $tables = $db->findTables(); } else { foreach ($tables as $table) { if ($db->isExistTable($table) === false) { \Lib\State::warning('Unkown table \'' . $table . '\''); } } } if (empty($tables)) { \Lib\State::warning('Not found any tables'); } \Lib\State::notice('Found ' . sizeof($tables) . ' table(s)'); $modelFile = \Model\File::getInstance(); $modelContents = \Model\Content::getInstance(); $replaceArr = $op->getReplace() ?: []; foreach ($tables as $table) { $tableName = \Lib\Func::uc($table); $className = $tableName; if (!empty($replaceArr['source']) && !empty($replaceArr['target'])) { $className = str_ireplace($replaceArr['source'], ucfirst($replaceArr['target']), $className); } if (preg_match('/^[0-9]+/', $tableName)) { $tableName = ltrim(preg_replace('/^[0-9]+/', '', $tableName), '_'); } \Lib\State::notice('-----------------'); \Lib\State::notice('Processing [' . $table . ']'); $modelContents->setTableInfo($db->findTableInfo($table)); $modelContents->setClassName($className); $modelContents->setTableName($tableName); $modelContents->setColumns($db->findCols($table)); $modelContents->build(); \Lib\State::notice('Done'); $modelFile->setFileName($className)->build(); $modelContents->reset(); $modelFile->reset(); } return true; }
/** * 创建toArray方法 * * @param array $sets * @return string */ public function toToArray(array $sets) { $items = array(str_repeat($this->_tab, 2) . 'return array('); $citem = array(); $lenArr = array_map(function ($name) { return mb_strlen($name); }, $sets); sort($lenArr); $maxLen = array_pop($lenArr) + 1; foreach ($sets as $name) { $len = $maxLen - mb_strlen($name); $citem[] = str_repeat($this->_tab, 3) . '\'' . $name . '\'' . str_repeat(' ', $len) . '=> $this->_' . lcfirst(\Lib\Func::ucc($name)); } $items[] = implode(',' . "\n", $citem); unset($citem); $items[] = str_repeat($this->_tab, 2) . ");"; return $this->toFunc('toArray', $items); }
/** * 创建get方法内容 * * @param \Model\Columnstruct $struct * @param array $commentArr */ protected function buildGetfuncContent(\Model\Columnstruct $struct, array $commentArr) { $build = \Model\Build::getInstance(); $buffer = \Model\Buffer::getInstance(); $name = strtolower($struct->getColumn_name()); $propName = lcfirst(\Lib\Func::ucc($name)); $commentArr[] = '@return ' . $this->getDateType($struct->getData_type()); $buffer->pushFunc($build->toComment($commentArr)); $buffer->pushFunc($build->toGetFunc(ucfirst($name), array(str_repeat($this->_tab, 2) . 'return $this->_' . $propName . ';'))); }