添加Dao层
<?php $tables = <<<EOF -- 资讯 - 分类表 DROP TABLE IF EXISTS `category`; CREATE TABLE `category` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '分类主键id', `category_name` varchar(50) NOT NULL DEFAULT '' COMMENT '分类名称', `parent_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '分类的父类ID', `cms_total` int(10) NOT NULL DEFAULT '0' COMMENT '该分类下cms总数', `sort` int(10) NOT NULL DEFAULT '0' COMMENT '排序(权重大越靠前)', `status` tinyint(2) NOT NULL DEFAULT '1' COMMENT '状态 1:正常 3:删除', `extend` json NOT NULL, `create_time` int(10) unsigned NOT NULL, `update_time` int(10) unsigned NOT NULL, `cas_token` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='cms分类表'; EOF; $statusStr = <<<EOF /** * 状态:正常 */ const STATUS_NORMAL = 1; /** * 状态:删除 */ const STATUS_DELETE = 3; private static \$statusDesc = [ self::STATUS_NORMAL => [ 'kw' => 'STATUS_NORMAL', 'desc' => '正常' ], self::STATUS_DELETE => [ 'kw' => 'STATUS_DELETE', 'desc' => '删除' ], ]; /** * 获取状态描述 * @return array */ static public function getsStatusDesc() { return self::\$statusDesc; } EOF; preg_match_all('#CREATE TABLE `(.*)`#', $tables, $matches); $daoDir = '/usr/local/var/www/S/shuntu/main/lib/Dao'; $projectFields = array( 'id', 'extend', 'create_time','update_time','cas_token' ); foreach($matches[1] as $tableName) { $words = explode('_', $tableName); $tmpName = 'D'; foreach($words as $word) { $tmpName .= ucfirst($word); } $className = $tmpName; $tmpName .= '.class.php'; preg_match("#CREATE TABLE `$tableName` (.*) COMMENT=\'(.*)\';#iUs", $tables, $matches); $filedsStr = $matches[1]; $tableNameDesc = $matches[2]; // 表的文字描述 $filedsRows = array(); // 获取其他字段 $otherFieldStr = ''; $rows = explode("\n", $filedsStr); foreach($rows as $row) { if(strpos($row, '`') === false || strpos($row, 'KEY') !== false) { continue; } $filedsRows[] = $row; } $filedsStr = implode("\n", $filedsRows); preg_match_all('#`(.*)`(.*),#iU', $filedsStr, $matches); $fieldStatusDesc = ''; foreach($matches[1] as $key => $filedName) { if(in_array($filedName, $projectFields)) { continue; } $filedDesc = trim($matches[2][$key]); $otherFieldStr .= "'{$filedName}', //{$filedDesc}" . "\n"; // 'nickname', //char(40) NOT NULL DEFAULT '' COMMENT '微信昵称', if($filedName == 'status') { $fieldStatusDesc = $statusStr; } } $otherFieldStr = trim($otherFieldStr, "\n"); $fileName = $tmpName; echo $fileName; echo PHP_EOL; $daoFile = $daoDir . '/' . $fileName; // if(file_exists($daoFile)) { // unlink($daoFile); // } $myfile = fopen($daoFile, "w"); $content = <<<EOF <?php /** * 数据层 - {$tableNameDesc} * {$fileName} 2019/8/15 @bing */ namespace Main\Dao; use KIF\Dao\DBAgileDev; class {$className} extends DBAgileDev { /** * 表名 * * @var string */ protected \$tableName = '{$tableName}'; protected \$other_field = array( {$otherFieldStr} ); {$fieldStatusDesc} } EOF; fwrite($myfile, $content); fclose($myfile); }
添加Module层
<?php $tables = <<<EOF -- 资讯 - 分类表 DROP TABLE IF EXISTS `category`; CREATE TABLE `category` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '分类主键id', `category_name` varchar(50) NOT NULL DEFAULT '' COMMENT '分类名称', `parent_id` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '分类的父类ID', `cms_total` int(10) NOT NULL DEFAULT '0' COMMENT '该分类下cms总数', `sort` int(10) NOT NULL DEFAULT '0' COMMENT '排序(权重大越靠前)', `status` tinyint(2) NOT NULL DEFAULT '1' COMMENT '状态 1:正常 3:删除', `extend` json NOT NULL, `create_time` int(10) unsigned NOT NULL, `update_time` int(10) unsigned NOT NULL, `cas_token` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='cms分类表'; EOF; $statusStr = <<<EOF /** * 状态:正常 */ const STATUS_NORMAL = 1; /** * 状态:删除 */ const STATUS_DELETE = 3; private static \$statusDesc = [ self::STATUS_NORMAL => [ 'kw' => 'STATUS_NORMAL', 'desc' => '正常' ], self::STATUS_DELETE => [ 'kw' => 'STATUS_DELETE', 'desc' => '删除' ], ]; /** * 获取状态描述 * @return array */ static public function getsStatusDesc() { return self::\$statusDesc; } EOF; preg_match_all('#CREATE TABLE `(.*)`#', $tables, $matches); $daoDir = '/usr/local/var/www/S/shuntu/main/lib/Module'; foreach($matches[1] as $tableName) { $words = explode('_', $tableName); $tmpName = 'M'; foreach($words as $word) { $tmpName .= ucfirst($word); } $className = $tmpName; $tmpName = 'D'; foreach($words as $word) { $tmpName .= ucfirst($word); } $daoClassName = $tmpName; $tmpName = $className . '.class.php'; preg_match("#CREATE TABLE `$tableName` (.*) COMMENT=\'(.*)\';#iUs", $tables, $matches); $filedsStr = $matches[1]; $tableNameDesc = $matches[2]; // 表的文字描述 $tableNameDesc = str_replace('表', '', $tableNameDesc); $filedsRows = array(); $fileName = $tmpName; echo $fileName; echo PHP_EOL; $daoFile = $daoDir . '/' . $fileName; // if(file_exists($daoFile)) { // unlink($daoFile); // } $myfile = fopen($daoFile, "w"); $content = <<<EOF <?php /** * 模型层 - {$tableNameDesc} * {$fileName} 2019/8/15 @bing */ namespace Main\Module; use KIF\Data\ResultWrapper; use KIF\Sugar\AbstractModule; use Main\Dao\\{$daoClassName}; class {$className} extends AbstractModule { public function __construct() { \$this->objDao = new {$daoClassName}(); } } EOF; fwrite($myfile, $content); fclose($myfile); }