drewlabs/g-cli

从数据库结构生成类、服务、模型和控制器组件或整个项目源代码

v0.2.80 2024-04-24 08:50 UTC

README

此项目使用代码生成器包来为laravel项目创建控制器、服务、模型等组件...

用法

编程API

本节向您展示了用于创建控制器、服务、模型、ViewModel和数据传输对象的PHP类API

控制器构建器

本包提供控制器类构建器,可用于构建资源控制器、可调用控制器或预制的CRUD控制器。

// ...
use function Drewlabs\GCli\Proxy\ComponentsScriptWriter;
use function Drewlabs\GCli\Proxy\MVCControllerBuilder;
// ...

// This code creates an invokable controller
ComponentsScriptWriter(__DIR__ . '/examples/src')->write(
    (MVCControllerBuilder())
        ->bindServiceClass(
            "App\\Services\\PersonsService"
        )
        ->asInvokableController()
        ->build()
);

// This code creates a resource controller and bind it to a model
// If conroller name is not provided, it's generated from the model name
ComponentsScriptWriter(__DIR__ . '/examples/src/')->write(
    (MVCControllerBuilder('PostsController', '\\App\\Http\\Controllers\\Common'))
        ->bindServiceClass(
            "App\\Services\\PersonsService"
        )
        ->build()
)

服务类构建器

服务类似于处理控制器操作的代理,并有权访问数据库模型。

您可以创建一个提供空实现或预填充CRUD操作的服务。

// ...
use function Drewlabs\GCli\Proxy\ComponentsScriptWriter;
use function Drewlabs\GCli\Proxy\MVCServiceBuilder;
// ...

// Creating a prefilled service with name derived from a model class name
ComponentsScriptWriter(__DIR__ . '/examples/src/')->write(
    (MVCServiceBuilder())
        ->bindModel(
            "App\\Models\\Human"
        )
        ->asCRUDService()
        ->build()
)

// Creates a simple service with only a handle method
ComponentsScriptWriter(__DIR__ . '/examples/src/')->write(
    (MVCServiceBuilder())
        ->bindModel(
            "App\\Models\\Person"
        )
        ->build()
)

ViewModel类构建器

ViewModel是一个类,它围绕用户提供的值、验证规则以及可能的认证用户引用进行包装。

// ...
use function Drewlabs\GCli\Proxy\ComponentsScriptWriter;
use function Drewlabs\GCli\Proxy\ViewModelBuilder;
// ...

// Creating a fully complete view model
ComponentsScriptWriter(__DIR__ . '/examples/src/')->write(
    (ViewModelBuilder())
        ->bindModel(
            "App\\Models\\Person"
        )
        ->addInputsTraits()
        ->addFileInputTraits()
        ->addAuthenticatableTraits()
        ->setRules([
            'firstname' => 'required|max:50',
            'lastname' => 'required|max:50'
        ])
        ->build()
);

// Create a view model that can only be used with the validator to validate user input
ComponentsScriptWriter(__DIR__ . '/examples/src/'))->write(
    (ViewModelBuilder())
        ->bindModel(
            "App\\Models\\Person"
        )
        ->asSingleActionValidator()
        ->setRules([
            'firstname' => 'required|max:50',
            'lastname' => 'required|max:50'
        ])
        ->build()
)

模型类构建器

数据库模型类似于代表您与数据库交互的实体管理器类。此包提供了Eloquent ORM模型的实现。

此实现使用drewlabs/database包进行构建,并假定该包作为依赖项安装。您可以自由提供自己的构建器实现。

// ....
use function Drewlabs\GCli\Proxy\ComponentsScriptWriter;
use function Drewlabs\GCli\Proxy\EloquentORMModelBuilder;
use function Drewlabs\GCli\Proxy\ORMColumnDefinition;
use function Drewlabs\GCli\Proxy\ORMModelDefinition;
//...

// Building a model
ComponentsScriptWriter(__DIR__ . '/examples/src'))->write(EloquentORMModelBuilder(ORMModelDefinition([
    'primaryKey' => 'id',
    'name' => null,
    'table' => 'persons',
    'columns' => [
        ORMColumnDefinition([
            'name' => 'firstname',
            'type' => 'string'
        ]),
        ORMColumnDefinition([
            'name' => 'lastname',
            'type' => 'string'
        ])
    ],
    'increments' => false,
    'namespace' => "App\\Models"
]))->build()


// To build a model as a view model
ComponentsScriptWriter(__DIR__ . '/examples/src')->write((EloquentORMModelBuilder(ORMModelDefinition([
    'primaryKey' => 'id',
    'name' => null,
    'table' => 'humans',
    'columns' => [
        ORMColumnDefinition([
            'name' => 'firstname',
            'type' => 'string'
        ]),
        ORMColumnDefinition([
            'name' => 'lastname',
            'type' => 'string'
        ])
    ],
    'increments' => false,
    'namespace' => "App\\Models"
])))->asViewModel()->build();

Laravel命令接口

该包提供了一些laravel框架命令,用于轻松从终端应用程序创建组件。这些命令包括

drewlabs:mvc:create命令

此命令允许开发人员从数据库表生成整个API堆栈,具有预定义的结构。生成的输出可通过Artisan命令接口输入进行配置。

  • 创建带有控制器和路由的MVC组件
php artisan drewlabs:mvc:create --http 

禁用缓存

默认情况下,命令使用缓存来优化生成多个所需组件时的任务。使用以下命令,命令将忽略缓存并重新生成先前生成的文件

php artisan drewlabs:mvc:create --http --force

移除模式前缀

在某些应用程序中,数据库使用模式名称进行前缀。生成的代码通常会将模式前缀添加到类和接口中。为了防止此类输出,开发人员可以使用--schema选项来允许命令从生成的类中删除模式部分。

php artisan drewlabs:mvc:create --http --schema=test

添加中间件

有时开发人员可能希望将生成的路由分组到给定的中间件中。命令接口提供了一个选项,允许指定在分组生成的路由时使用的中间件。

php artisan drewlabs:mvc:create --http --middleware=auth

表过滤器

命令接口还支持--only选项,允许开发人员指定在生成代码时要包含的表列表。

php artisan drewlabs:mvc:create --http --only=users

警告 --only选项处于实验状态,并且可能会重写您的路由文件,删除先前生成的路由。

设置路由文件名

默认情况下,命令使用位于laravel项目路由目录(/routes)中的web.php作为路由文件名。要覆盖默认值

php artisan drewlabs:mvc:create --http --routingfilename=api.php

上述命令在项目路由目录(/routes)中使用或创建文件进行路由。

模型关系

drewlabs:mvc:create在生成模型类定义时提供了生成关系方法定义的标志。使用--relations标志,开发人员可以生成带有相应relations的模型。

php artisan drewlabs:mvc:create --http --relations

常见问题解答 如何自定义模型关系方法?

该命令支持模型关系自定义的参数。使用 --manytomany--toones--manythroughs--onethroughs,开发者可以分别指定为多对多一对一多对多通过一对通过关系。

各种自定义的语法如下:

  • manytomany source_table->pivot_table->destination_table:method
php artisan drewlabs:mvc:create --http --relations --manytomany=posts->post_comments->comments --manytomany=posts->post_tags->tags

注意 语法中的 [method] 部分可以省略,将由命令自动生成。

  • toones source_table->destination_table:method
php artisan drewlabs:mvc:create --http --relations --toones=employees->employee_managers

注意 语法中的 [method] 部分可以省略,将由命令自动生成。

  • onethroughsmanythroughs source_table->pivot_table->destination_table:method

注意 语法中的 [method] 部分可以省略,将由命令自动生成。

php artisan drewlabs:mvc:create --http --relations --manythroughs=post_types->posts->comments:comments

策略 & 守卫

从版本 2.9 开始,drewlabs:mvc:create 命令支持添加策略守卫定义到项目中,默认允许所有控制器操作使用 authorize 方法。在运行命令时使用 --policies 标志来生成策略类。

注意 命令输出末尾将生成一个服务提供者类 [\App\Providers\PoliciesServiceProvider::class]。请将其添加到项目中,以便让 Laravel 知道如何使用生成的策略来守卫模型类。

php artisan drewlabs:mvc:create --http --policies

注意 要预览可用选项列表,请使用 php artisan drewlabs:mvc:create --help

创建数据库模型

# Create a database model
php artisan drewlabs:mvc:make:model --table=comments --columns="label|string" --columns="description|text" --columns="likes|number" --hidden=likes --path=src --namespace="\\Application\\Models" --appends=posts

创建 PHP 类

注意 使用 php artisan drewlabs:mvc:make:class --help 命令查看可用选项列表

php artisan drewlabs:mvc:make:class --path='src' --name=Post

创建数据传输对象

注意 使用 php artisan drewlabs:mvc:make:dto --help 命令查看可用选项列表

php artisan drewlabs:mvc:make:dto --path=src --namespace=\\Application\\Dto

创建 MVC 服务

注意 使用 php artisan drewlabs:mvc:make:service --help 命令查看可用选项列表

php artisan drewlabs:mvc:make:service --model=\\Application\\Models\\Comment --asCRUD --path=src

创建 MVC 视图模型

注意 使用 php artisan drewlabs:mvc:make:viewmodel --help 命令查看可用选项列表

php artisan drewlabs:mvc:make:viewmodel --model=\\Application\\Models\\Comment --single --path=src

创建 MVC 控制器

注意 使用 php artisan drewlabs:mvc:make:controller --help 命令查看可用选项列表

php artisan drewlabs:mvc:make:controller --name=Posts --path=src --model="\\Application\\Models\\Comment"