连续复制
一键复制
一键打包

Controller.json

{
  "template": "Controller",
  // 模块列表
  "list": {
    "application\\admin\\controller": [
  // ["abc","后台首页控制器"]
    ]
  }
}

Controller.tpl

<?php

namespace {{namespace}};

use think\Controller;

/**
* {{ctlComment}}
* Class {{className}}
* @author {{author}}
* @copyright {{copyright}}
* @package {{namespace}}
* @since 1.0.0
* @version 1.0.0
*/
class {{className}} extends Controller
{
    /**
     * 列表
     */
    public function index()
    {
    }

    /**
     * 新增
     */
    public function add()
    {

    }

    /**
     * 删除
     */
    public function del()
    {

    }

    /**
     * 修改
     */
    public function edit()
    {

    }

    /**
     * 启用
     */
    public function resume()
    {

    }

    /**
     * 禁用
     */
    public function forbid()
    {

    }

}
?>

Generator.json

{
  "author":"yinluobing <yinluobing@mcake.com>",
  "copyright":"上海卡法电子商务有限公司",
  "projectPath":"D:\\wwwroot\\dev.mcake.admin\\"
}

Generator.php

<?php
require_once 'util.php';
$config = object2array(json_decode(preg_replace('/\/\/(.+?)\n/', '', file_get_contents('Generator.json'))));
$modelConfig = array_merge(
    $config,
    object2array(json_decode(preg_replace('/\/\/(.+?)\n/', '', file_get_contents('Model.json'))))
);

// 初始化
$tables = [];
$Mysqli = new mysqli(
    $modelConfig['Db_host'],
    $modelConfig['Db_user'],
    $modelConfig['Db_passwd'],
    $modelConfig['Db_database'],
    $modelConfig['Db_port']
);
$Mysqli->query("SET NAMES utf8");

// 模型生成
if (!$Mysqli->connect_errno) {

    // 模型模板
    $modelTplContent = file_get_contents($modelConfig['template'] . '.tpl');

    // 获取所有表名
    foreach ($Mysqli->query('show table status from ' . $modelConfig['Db_database'])->fetch_all() as $k => $v) {
        // 表名
        $tables[$k]['name'] = $v[0];
        // 注释
        $tables[$k]['comment'] = $v[17];
    }


    // 获取所有表备注和表列
    foreach ($tables as $k => $v) {
        // 列
        $sql = 'show full columns from ' . $v['name'];
        foreach ($Mysqli->query($sql) as $column) {
            $tables[$k]['column'][] = [
                'name' => $column['Field'],
                'comment' => $column['Comment'],
                'type' => $column['Type']
            ];
        }
    }

    $modelConfig['copyright'] = date('m/d/Y', $_SERVER['REQUEST_TIME'])
        . '-' . date('m/d/Y', strtotime("+2 year"))
        . ' ' . $modelConfig['copyright'];
    replaceModel($modelConfig, replace($modelConfig, $modelTplContent), $tables);
    unset($sql);
} else {
    echo '数据库链接失败';
}

// 控制器生成
$ctlConfig = array_merge(
    $config,
    object2array(json_decode(preg_replace('/\/\/(.+?)\n/', '', file_get_contents('Controller.json'))))
);

// 控制器模板
$ctlTplContent = file_get_contents($ctlConfig['template'] . '.tpl');

foreach ($ctlConfig['list'] as $k => $v) {
    if (!file_exists($config['projectPath'] . str_replace('\\', '/', $k)))
        mkdir($config['projectPath'] . str_replace('\\', '/', $k), 0777, true);
    foreach ($v as $i) {
        $ctlConfig['namespace'] = $k;
        $ctlFileContent = replace($ctlConfig, $ctlTplContent);
        $ctlFileContent = str_replace('{{className}}', getModelName($i[0]), $ctlFileContent);
        $ctlFileContent = str_replace('{{ctlComment}}', $i[1], $ctlFileContent);
        if (!file_exists($config['projectPath']
            . str_replace('\\', '/', $k)
            . '/' . getModelName($i[0]) . '.php'))
            file_put_contents($config['projectPath']
                . str_replace('\\', '/', $k) . '/' . getModelName($i[0]) . '.php', $ctlFileContent);
    }
}

Model.json

{
  // 模板文件
  "template": "Model",

  // 数据库
  "Db_host":"127.0.0.1",
  "Db_port":"3306",
  "Db_user":"root",
  "Db_passwd":"root",
  "Db_database":"apiadmin",

  // 模板
  "namespace":"application\\common\\model",
  "columnTpl":"\r\n    \/**\r\n    * {{columnCommnet}}\r\n    * @var {{columnType}}\r\n    *\/\r\n    public ${{columnName}};\r\n"

}

Model.tpl

<?php

namespace {{namespace}};

use think\Model;

/**
* {{tableComment}}
* Class {{className}}
* @author {{author}}
* @copyright {{copyright}}
* @package {{namespace}}
* @since 1.0.0
* @version 1.0.0
*/
class {{className}} extends Model
{
    protected $table='{{tableName}}';
{{column}}
}
?>

util.php

<?php
/**
 * 模板替换
 * @param $config
 * @param $content
 * @return mixed
 */
function replace($config, $content)
{
    if (strpos($content, '{{namespace}}') > -1 && isset($config['namespace']))
        $content = str_replace('{{namespace}}', $config['namespace'], $content);
    if (strpos($content, '{{author}}') > -1 && isset($config['author']))
        $content = str_replace('{{author}}', $config['author'], $content);
    if (strpos($content, '{{copyright}}') > -1 && isset($config['copyright']))
        $content = str_replace('{{copyright}}', $config['copyright'], $content);

    return $content;
}

/**
 * 数据替换
 * @param $config
 * @param $tables
 * @param $content
 */
function replaceModel($config, $content, $tables=[])
{
    if (isset($config['namespace']) && !$config['namespace'] !== '') {
        if (!file_exists($config['projectPath'] . str_replace('\\', '/', $config['namespace'])))
            mkdir($config['projectPath'] . str_replace('\\', '/', $config['namespace']), 0777, true);
    }

    foreach ($tables as $v) {
        $filecontent = str_replace('{{tableComment}}', $v['comment'], $content);
        $filecontent = str_replace('{{tableName}}', $v['name'], $filecontent);
        $filecontent = str_replace('{{className}}', getModelName($v['name']), $filecontent);
        $columns = '';
        foreach ($v['column'] as $columnItem) {
            $columns .= str_replace('{{columnCommnet}}', $columnItem['comment'],
                str_replace('{{columnType}}', returnType($columnItem['type']),
                    str_replace('{{columnName}}', $columnItem['name'], $config['columnTpl'])));
        }

        $filecontent = str_replace('{{column}}', $columns, $filecontent);
        if (!file_exists($config['projectPath']
            . str_replace('\\', '/', $config['namespace'])
            . '/' . getModelName($v['name']) . '.php'))
            file_put_contents($config['projectPath']
                . str_replace('\\', '/', $config['namespace']) . '/' . getModelName($v['name']) . '.php', $filecontent);
    }
}

/**
 * 返回php数据类型
 * @param $str
 * @return string
 */
function returnType($str)
{

    /**
     * MYSQL 数据类型
     * 整数类型:BIT、BOOL、TINY INT、SMALL INT、MEDIUM INT、 INT、 BIG INT
     * 浮点数类型:FLOAT、DOUBLE、DECIMAL
     * 字符串类型:CHAR、VARCHAR、TINY TEXT、TEXT、MEDIUM TEXT、LONGTEXT、TINY BLOB、BLOB、MEDIUM BLOB、LONG BLOB
     * 日期类型:Date、DateTime、TimeStamp、Time、Year
     * 其他数据类型:BINARY、VARBINARY、ENUM、SET、Geometry、Point、MultiPoint、LineString、MultiLineString、Polygon、GeometryCollection等
     */
    $str = strtolower($str);
    if (strpos($str, 'int') > -1 || strpos($str, 'bit') > -1 || strpos($str, 'bool') > -1) {
        $str = 'integer';
    }

    if (strpos($str, 'float') > -1 || strpos($str, 'double') > -1 || strpos($str, 'decimal') > -1)
        $str = 'float';

    if (strpos($str, 'date') > -1 || strpos($str, 'time') > -1 || strpos($str, 'year') > -1)
        $str = 'string';

    if (strpos($str, 'char') > -1 || strpos($str, 'text') > -1 || strpos($str, 'blob') > -1)
        $str = 'string';

    return $str;
}

/**
 * 打印
 * @param $param
 */
function p($param)
{
    if (is_array($param) || is_object($param)) {
        echo '<pre>';
        print_r($param);
        echo '</pre>';
    } else {
        echo $param;
    }
}

/**
 * 获取模型名称
 * @param $tableName
 * @return mixed
 */
function getModelName($tableName)
{
    return str_replace(' ', '', ucwords(str_replace('_', ' ', $tableName)));
}

/**
 * 数组转对象
 * @param $array
 * @return StdClass
 */
function array2object($array)
{

    if (is_array($array)) {
        $obj = new StdClass();
        foreach ($array as $key => $val) {
            $obj->$key = $val;
        }
    } else {
        $obj = $array;
    }
    return $obj;
}

/**
 * 对象转数组
 * @param $object
 * @return mixed
 */
function object2array($object)
{
    if (is_object($object)) {
        foreach ($object as $key => $value) {
            $array[$key] = $value;
        }
    } else {
        $array = $object;
    }
    return $array;
}

文件目录

└── Generator
    ├── Controller.json 
    ├── Controller.tpl 
    ├── Generator.json 
    ├── Generator.php 
    ├── Model.json 
    ├── Model.tpl 
    ├── util.php