zx/php-code-generator

php代码生成器

v0.1.0 2023-09-21 06:23 UTC

This package is auto-updated.

Last update: 2024-09-25 09:01:21 UTC


README

English doc

介绍

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

运行环境

php ^7.2|^8.0

支持范围

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

  • [x] PHP-laravel 蛇形命名
  • [x] PHP-laravel 驼峰命名
  • [x] PHP-webman 蛇形命名
  • [x] PHP-webman 驼峰命名
  • [x] Golang-goravel 蛇形命名
  • [x] Golang-goravel 驼峰命名
  • [x] PHP-hyperf 蛇形命名
  • [x] 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