1 Star 11 Fork 2

ZX/php-code-generator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
ZX 更新字段 c5a3f4a 11个月前
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

code-generator

English doc

介绍

基于php的代码生成器,根据数据库表快速生成CURD方法,是基于渲染模板的方法

运行环境

php ^7.2|^8.0

支持范围

目前仅支持laravel,webman,后续会支持goravel,drogon等,也欢迎提交模板

  • PHP-laravel 蛇形命名
  • PHP-laravel 驼峰命名
  • PHP-webman 蛇形命名
  • PHP-webman 驼峰命名
  • Golang-goravel 蛇形命名
  • Golang-goravel 驼峰命名
  • PHP-hyperf 蛇形命名
  • PHP-hyperf 驼峰命名
  • CPP-drogon 蛇形命名
  • CPP-drogon 驼峰命名

支持 composer

composer require zx/php-code-generator

关于软删除版本

1,不在单独提供laravel,webman等使用Eloquent ORM的框架,因只需要单独在model里面添加use SoftDeletes即可
2,驼峰命令在orm里面的转换,composer require kirkbushell/eloquence

添加 eloquence service provider 在你的 config/app.php 文件中

'providers' => [

        /*
         * Application Service Providers...
         */
        Eloquence\EloquenceServiceProvider::class,
    ],

实例:

<?php

namespace App\Models;

use Eloquence\Behaviours\CamelCasing;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Feedback extends Model
{
    // 蛇形命名转驼峰
    use CamelCasing;

    // 软删除
    use SoftDeletes;

    // 表名
    protected $table = 'feedback';
    // 主键id
    protected $primaryKey = 'id';
    // 不可被批量赋值的字段
    protected $guarded = [];
    // 不维护时间字段
    public $timestamps = false;
    // 返回隐藏的字段
    protected $hidden = ['delete_at'];
    // 返回显示的字段
    protected $visible = [];
    // 自定义软删除字段 默认 deleted_at
    const DELETED_AT = 'delete_at';
}

Eloquent ORM软删除使用文档:https://www.cnblogs.com/zx-admin/p/17497555.html
Eloquent ORM驼峰命名转换使用文档:https://www.cnblogs.com/zx-admin/p/17493699.html

使用方法

1, 下载该工具包代码,保障当前环境有php的运行环境 2, 查看tests/test.php的代码有测试用例
3, 在 tests/Tools有针对laravel和webman的代码工具包
4, 如果不理解怎么使用可以参看 webman项目https://github.com/zh7314/zx-webman-website OR laravel项目https://github.com/zh7314/zx-website
5, 每次生成前建议删除tests的目录下,然后吧生成好的代码复制到对应的项目文件下,直接生成在项目目录 容易造成代码覆盖,所以推荐自己复制进去

使用例子

<?php

include_once './Function.php';
include_once './../src/Tool/File.php';
include_once './../src/Tool/Hump.php';
include_once './../src/Tool/Mysql.php';
include_once './../src/Tool/MysqlOperation.php';
include_once './../src/Generator.php';
include_once './../src/Generator/Laravel.php';
include_once './../src/Generator/LaravelSoftDelZx.php';
include_once './../src/Generator/Webman.php';
include_once './../src/Generator/Goravel.php';

use ZX\Tool\MysqlOperation;
use ZX\Generator\Laravel;
use ZX\Generator\LaravelSoftDelZx;
use ZX\Generator\Webman;
use ZX\Generator\Goravel;

$param = [
    'type' => 'mysql',
    'host' => '127.0.0.1',
    'port' => '3306',
    'dbname' => 'web',
    'charset' => 'utf8',
    'user' => 'root',
    'pwd' => 'root'
];

MysqlOperation::setConnection($param);


/*
 * laravel生成器,参考项目: https://github.com/zh7314/zx-laravel-cms   https://gitee.com/open-php/zx-laravel-cms
 * $camel true => case camel,true => case snake
 */

Laravel::genAllTable();
Laravel::genAllRouter();

Laravel::genTable("table_name");
Laravel::genRouter("table_name");

/*
 * webman 生成器,配合使用项目: https://github.com/zh7314/zx-webman-cms   https://gitee.com/open-php/zx-webman-cms
 * $camel true => case camel,true => case snake
 */

Webman::genAllTable();
Webman::genAllRouter();

Webman::genTable("table_name");
Webman::genRouter("table_name");

/*
 * goravel 生成器,配合使用项目: https://github.com/zh7314/zx-goravel-cms   https://gitee.com/open-php/zx-goravel-cms
 * $camel true => case camel,true => case snake
 */

Goravel::genAllTable();
Goravel::genAllRouter();

Goravel::genTable("table_name");
Goravel::genRouter("table_name");

/*
 * drogon 生成器,配合使用项目: https://github.com/zh7314/zx-drogon-cms   https://gitee.com/open-php/zx-drogon-cms
 * $camel true => case camel,true => case snake
 */

Drogon::genAllTable();
Drogon::genAllRouter();

Drogon::genTable("table_name");
Drogon::genRouter("table_name");

/*
 * laravelzx 定制化生成器
 */

LaravelSoftDelZx::genAllTable();
LaravelSoftDelZx::genAllRouter();

LaravelSoftDelZx::genTable("table_name");
LaravelSoftDelZx::genRouter("table_name");

6, 如果你直接使用默认模板你可能需要一些辅助代码
他们会在tests/Toolslaravel/utils里面 在composer.json里面加上

"autoload": {
        "files": [
          "app/Utils/Function.php"
        ]
    }

7, 如果你不喜欢使用一些辅助代码,你可以把返回的代码改成

return response()->json(['code' => 200, 'msg' => '成功']);

这样标准laravel写法 ,代码检查也可以改成

$where['weixin_phone'] = !empty($request->weixin_phone) ? (string)htmlspecialchars(trim($request->weixin_phone), ENT_QUOTES, "UTF-8") : '';

自定义模板

1,继承 ZX\BaseGenerator
2,实现抽象方法,参照XXGenerator 的方法去实现自己的模板,通用的方法都有提供

问题反馈

QQ群:247823727
博客:https://www.cnblogs.com/zx-admin/
gitee:https://gitee.com/open-php/php-code-generator
github:https://github.com/zh7314

MIT License Copyright (c) 2021 Z.X Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于php的代码生成器 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/open-php/php-code-generator.git
git@gitee.com:open-php/php-code-generator.git
open-php
php-code-generator
php-code-generator
master

搜索帮助