/** * model file create * * @return boolean */ public function touchFile() { $options = \Lib\Options::getInstance(); $dir = $options->getFilepath(); if (!file_exists($dir)) { mkdir($dir, 0755, true); } $this->_file = $dir . DS . $this->_fileName . $options->getExt(); return true; }
/** * 文件名、类名处理 * * @param string $name * @return string */ public static function uc($name) { $options = \Lib\Options::getInstance(); $ucwords = (bool) $options->getUcwords(); $underline = (bool) $options->getUnderline(); $name = ucfirst($name); if ($ucwords) { $name = $underline ? self::ucc($name, '_', '_') : self::ucc($name); } else { $name = $underline ? ucfirst($name) : str_replace('_', '', $name); } return $name; }
/** * 创建类名 * * @param string $name * @return string */ public function toClass($name) { $options = \Lib\Options::getInstance(); $finalCls = $options->getFinal() === true ? 'final ' : ''; $items = array($finalCls . 'class ' . sprintf($options->getModelType(), $name)); $extendName = $options->getExtendName(); $implements = $options->getImplements(); if (!empty($extendName)) { $items[] = ' extends ' . $extendName; } if (!empty($implements)) { $items[] = ' implements ' . $implements; } $items[] = " {\n"; return implode('', $items); }
/** * * @param type $option * @param type $value * @return boolean * @throws \Lib\Exception */ protected function setOption($option, $value) { $optionInstance = \Lib\Options::getInstance(); $optionName = $optionInstance->getOptionsName(ord(trim($option, '+'))); if (empty($optionName)) { throw new \Lib\Exception('unknown option \'' . $option . '\''); } $funcName = 'set' . ucfirst(strtolower($optionName)); if (method_exists($optionInstance, $funcName)) { if (isset($this->_defBool[$value])) { $value = $this->_defBool[$value]; } $optionInstance->{$funcName}($value); return true; } return false; }
/** * 获取 DB 资源 * * @return Db */ protected function getDbResponse() { if (!$this->_db instanceof \Lib\Db) { $options = \Lib\Options::getInstance(); $dbConfig = \Lib\DbConfig::getInstance(); $userName = $options->getUsername(); $passwd = $options->getPasswd(); $confName = empty($userName) || empty($passwd) ? $options->getDbConfig() : false; $params = array('host', 'dbname', 'port', 'options'); if (!empty($confName)) { $predefined = '\\Config\\' . ucfirst(strtolower($confName)); $preConfig = new $predefined(); if ($preConfig instanceof \Config\ConfigAbstract) { $dbConfig->setHost($preConfig->get('host')); $dbConfig->setPort($preConfig->get('port')); $dbConfig->setDbname($preConfig->get('dbname')); $dbConfig->setOptions($preConfig->get('options')); $dbConfig->setUsername($preConfig->get('username')); $dbConfig->setPasswd($preConfig->get('passwd')); } } else { array_push($params, 'username'); array_push($params, 'passwd'); } foreach ($params as $name) { $get = 'get' . ucfirst(strtolower($name)); if (method_exists($options, $get)) { $val = $options->{$get}(); $set = 'set' . ucfirst(strtolower($name)); if (!empty($val) && method_exists($dbConfig, $set)) { $dbConfig->{$set}($val); } } } $this->_db = new \Lib\Db($dbConfig); $this->_dbname = $dbConfig->getDbname(); } return $this->_db; }
/** * 创建set方法内容 * * @param \Model\Columnstruct $struct * @param array $commentArr */ protected function buildSetfuncContent(\Model\Columnstruct $struct, array $commentArr) { $build = \Model\Build::getInstance(); $buffer = \Model\Buffer::getInstance(); $options = \Lib\Options::getInstance(); $name = strtolower($struct->getColumn_name()); $propName = lcfirst(\Lib\Func::ucc($name)); $dataType = $this->getDateType($struct->getData_type()); $commentArr[] = '@param ' . $dataType . ' $' . $propName; $commentArr[] = '@return ' . ltrim($options->getNamespace(), '_') . sprintf($options->getModelType(), $this->_className); $buffer->pushFunc($build->toComment($commentArr)); $buffer->pushFunc($build->toSetFunc(ucfirst($name), array(str_repeat($this->_tab, 2) . '$this->_' . $propName . ' = (' . $dataType . ')$' . $propName . ';', '', str_repeat($this->_tab, 2) . 'return $this;'), $propName)); }