public function __construct(\Lib\DbConfig $dbConfig) { $maxTry = 3; $index = 1; \Lib\State::notice('Connecting to MySQL...'); while ($index <= $maxTry) { \Lib\State::notice('Try ' . $index . '...'); $index++; try { $pdo = new \PDO($dbConfig->getDsn(), $dbConfig->getUsername(), $dbConfig->getPasswd()); } catch (\Exception $e) { \Lib\State::warning($e->getMessage()); if ($index <= $maxTry) { \Lib\State::notice('Try to connect again after 3 seconds'); sleep(3); } continue; } break; } if (!isset($pdo) || !$pdo instanceof \PDO) { \Lib\State::notice($dbConfig->toString()); \Lib\State::error('MySQL connection failed.'); } $options = $dbConfig->getOptions(); if (!empty($options)) { foreach ($options as $option) { $pdo->query($option); } } \Lib\State::notice('connect successed'); $this->_db = $pdo; $this->_dbname = $dbConfig->getDbname(); }
/** * model contents build * * @return boolean */ public function build() { $this->touchFile(); file_put_contents($this->getFile(), \Model\Content::getInstance()->toString()); \Lib\State::notice('File [' . $this->_file . '], create successed'); }
/** * Get Help info * * @return string */ protected function getHelp() { $this->_isHelp = true; $item = array(); $item[] = 'f Model Class保存路径, 默认保存在work.php相应目录下的BuildResult文件夹下'; $item[] = ' e Model Class父类 (未开启命名空间,\'\\\' 以 \'_\' 代替)'; $item[] = ' i Model Class类所需接口类 (未开启命名空间,\'\\\' 以 \'_\' 代替)'; $item[] = ' x Model Class文件后缀名, 默认 php'; $item[] = ' l Model Class文件名/类名是否保留下划线, 默认 false'; $item[] = ' L Model Class方法名是否保留下划线, 默认 true [弃用]'; $item[] = ' m Model Class命名类型, 默认 1,1. %sModel 2. Model%s 3.%s_Model 4. Model_%s'; $item[] = ' R 自定义替换部份类名及文件名,格式 source:target, target相应字符首字母将被自动大写'; $item[] = ' N Model Class的命名空间,默认 \\ '; $item[] = ' F Model Class能支持写 final 关键字, 默认 false'; $item[] = ' U 文件名/类名/列名所有 _ 分隔单词首字母大写,否则仅第一单词首字母大写, 默认 false'; $item[] = ' o 是否开启命名空间, 默认 true'; $item[] = ' d 从Config中读取的数据库配置,默认 false'; $item[] = ' T 设置N个空格替代一个TAB,为0时将以TAB出现不替换, 默认 4'; $item[] = ' u 连接mysql用户名,使用此项 +d 将失效'; $item[] = ' p 连接mysql密码,使用此项 +d 将失效, 不建议直接在命令行输入密码'; $item[] = ' h 连接mysql主机, 默认 127.0.0.1'; $item[] = ' P 连接mysql主机端口, 默认 3306'; $item[] = ' n 连接mysql数据库名'; $item[] = ' O 数据库驱动选项处理, 多个时用 \',\' 分隔'; $item[] = ' t 指定Build的表名,多个时用 \',\' 分隔'; $item[] = ' H 显示帮助'; \Lib\State::notice(implode("\n", $item)); }
/** * 转成字符串按顺序合并 * * @return string */ public function toString() { $buffer = \Model\Buffer::getInstance(); $options = \Lib\Options::getInstance(); $items = array(); \Lib\State::notice('Building php head'); $items[] = $buffer->pullHeader(); $namespace = $options->getOnNamespace() === false ? $options->getNamespace() : ''; \Lib\State::notice('Building class [' . $namespace . sprintf($options->getModelType(), $this->_className) . ']'); $items[] = $buffer->pullClass(); \Lib\State::notice('Building class property'); $items[] = $buffer->pullProperty(); \Lib\State::notice('Building class function'); $items[] = $buffer->pullFunc(); $items[] = $buffer->pullToArray(); $items[] = $buffer->pullEnd(); $buffer->clearAll(); return implode("\n", $items); }