drewlabs/g-cli 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

drewlabs/g-cli

Composer 安装命令:

composer require drewlabs/g-cli

包简介

Generates classes, services, models and controller's components or entire project source code from databse structure

README 文档

README

This project uses the code generator package to create components like controllers, services, models etc for laravel projects...

Usage

Programming API

This section present you with PHP classes API for creating controllers, Services, Model, ViewModel and Data Transfert Object

The Controller Builder

This package provide you with a controller class builder that can be used to build a resource controller, an invokable controller or a pre-made CRUD controller.

// ...
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()
)

Service class builder

A service is like a delegate that handle controller action and has access to the database model.

You can create a service that provides empty implementation or is pre-filled with CRUD operations.

// ...
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()
)

View Model class builder

A view model is a class that wrap arround user provided values, validation rules and if possible a reference to the authenticated user.

// ...
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()
)

Model class builder

A database model is like en entity manager class that interact with database on your behalf. They packahe provides an implementation of the Eloquent ORM model.

This implementation use the drewlabs/database package for the builder and it suppose that package is install as dependency. You are free to provide an implementation of your on builder.

// ....
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 Commands interfaces

The package offers some laravel framework commands for easily creating component from your terminal application. Those commands are:

drewlabs:mvc:create command

This command allow devolpper to generate an entire api stack from database tables, with a pre-defined structure. The generated output is configurable using artisan command interface input:

  • Creating mvc component along with controllers and routes
php artisan drewlabs:mvc:create --http 

Disabling caching

By default the command use caching to optimize the task when generating more than once required components. using the command below, the command will ignore the cache and re-generate previously generated files:

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

Removing schema prefix

In some application, databases are prefixed using schema name. Generated code will mostly add the schema prefix to classes and interfaces. To prevent such output, developper can use --schema option to allow the tell the command to trim the schema part from generated classes:

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

Adding middleware

Sometimes developpers might want to group generated routes in a given middleware. The command interface provide an option to specify the middleware to use when grouping generated route using:

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

Table filters

The command interface also support an --only option wich allow developpers to specify the list of table to include when generating code:

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

Warning The --only option is in an experimental status, and as it can rewrite your routing file removing previously generated routes.

Setting route file name

By default the routing file name used by the command is web.php located in the /routes directory of the laravel project. To override the default:

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

The command above use or create a file in the project /routes directory for routing.

Model relations

drewlabs:mvc:create provides a flag for generating relation method definition while generating model class definition. Using --relations flag, developpers can generate model with corresponding relations.

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

FAQ How can model relation methods can be customized ?

The command support argument for model relation customization. using --manytomany, --toones, --manythroughs, --onethroughs, developpers are able to specify relation that are many to many, one to one, many through and one through relation respectively.

The syntax for various customization are:

  • 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

Note [method] part of the syntax can be omitted and will be generated by the command.

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

Note [method] part of the syntax can be omitted and will be generated by the command.

  • onethroughs & manythroughs source_table->pivot_table->destination_table:method

Note [method] part of the syntax can be omitted and will be generated by the command.

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

Policy & Guard

From version 2.9, drewlabs:mvc:create command suport flag for adding policy guard definition to your project with default to allowing every controller action using authorize method. To generate policy classes use the --policies flag when running command.

Note A service provider class [\App\Providers\PoliciesServiceProvider::class] class is generated at the end of the command output. Please add it to let laravel know how to guard your model classes using generated policies.

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

Note To preview the list of available options, please use the php artisan drewlabs:mvc:create --help

Create a database model

# 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

Create a php class

Note Use php artisan drewlabs:mvc:make:class --help command view the list of option available

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

Create a data transfert object

Note Use php artisan drewlabs:mvc:make:dto --help command view the list of option available

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

Create a MVC service

Note Use php artisan drewlabs:mvc:make:service --help command view the list of option available

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

Create a MVC view model

Note Use php artisan drewlabs:mvc:make:viewmodel --help command view the list of option available

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

Create a MVC controller

Note Use php artisan drewlabs:mvc:make:controller --help command view the list of option available

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

drewlabs/g-cli 适用场景与选型建议

drewlabs/g-cli 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 132 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 05 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 drewlabs/g-cli 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 drewlabs/g-cli 我们能提供哪些服务?
定制开发 / 二次开发

基于 drewlabs/g-cli 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 132
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-31