Laravel 半自动代码生成器

1、创建 command 命令

创建 Controller模板、Service 模板、Model 模板、Api Resource模板

1. php artisan make:command MakeController  # Controller模板
2. php artisan make:command MakeService #Service 模板
3. php artisan make:command MakeModel #Model 模板
4. php artisan make:command MakeResource #Api Resource模板
5. php artisan make:command MakeCollection #Api Resource模板
6. php artisan make:command MakeModule #其他模板集合

生成效果如下:
在这里插入图片描述

2、各个命令文件详情

1、MakeController

<?php

namespace App\Console\Commands\Generator;


use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;

class MakeController extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generator:controller {name}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成controller对象类';

    protected $type = "Controller";


    protected function getStub()
    {
        return base_path("stubs\controller.stub");
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace."\Http\Controllers\V1";
    }
}

2、MakeService

<?php

namespace App\Console\Commands\Generator;


use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;

class MakeService extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generator:service {name}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成service对象类';

    protected $type = "Service";


    protected function getStub()
    {
        return base_path("stubs\\service.stub");
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace."\Repositories\Services";
    }
}

3、MakeModel

<?php

namespace App\Console\Commands\Generator;


use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;

class MakeModel extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generator:model {name}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成model对象类';

    protected $type = "Model";


    protected function getStub()
    {
        return base_path("stubs\\model.stub");
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace."\Repositories\Models";
    }
}

4、MakeResource

<?php

namespace App\Console\Commands\Generator;


use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;

class MakeResource extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generator:resource {name} {raw-name?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成resource-collection对象类';

    protected $type = "Resource";


    protected function getStub()
    {
        return base_path("stubs\\resource\\resource.stub");
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace."\Http\Resources\\".$this->argument("raw-name")?:$this->argument("name");
    }
}

5、MakeCollection

<?php

namespace App\Console\Commands\Generator;


use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;

class MakeCollection extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generator:collection {name} {raw-name?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成resource-collection对象类';

    protected $type = "Collection";


    protected function getStub()
    {
        return base_path("stubs\\resource\\collection.stub");
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace."\Http\Resources\\".$this->argument("raw-name")?:$this->argument("name");
    }
}

6、MakeModule

<?php

namespace App\Console\Commands;


use App\Console\Commands\Generator\MakeCollection;
use App\Console\Commands\Generator\MakeController;
use App\Console\Commands\Generator\MakeModel;
use App\Console\Commands\Generator\MakeResource;
use App\Console\Commands\Generator\MakeService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class MakeModule extends Command
{
    /**
     * 控制台命令的名称和签名
     *
     * @var string
     */
    protected $signature = 'generator:module {name}';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = '生成模板文件';


    public function handle()
    {
        $this->generatorController();
        $this->generatorModel();
        $this->generatorService();
        $this->generatorResource();
    }


    /**
     * 生成 Controller 类
     */
    private function generatorController()
    {
        $name = $this->argument("name");
        Artisan::call(MakeController::class,['name'=>$name.'Controller']);
        $this->line("Generator Controller Success");
    }

    /**
     * 生成 Service 类
     */
    private function generatorService()
    {
        $name = $this->argument("name");
        Artisan::call(MakeService::class,['name'=>$name.'Service']);
        $this->line("Generator Service Success");
    }

    /**
     * 生成 Model 类
     */
    private function generatorModel()
    {
        $name = $this->argument("name");
        Artisan::call(MakeModel::class,['name'=>$name]);
        $this->line("Generator Model Success");
    }

    /**
     * 生成资源类
     */
    private function generatorResource()
    {
        $name = $this->argument("name");
        Artisan::call(MakeResource::class,['name'=>$name.'Resource','raw-name'=>$name]);
        Artisan::call(MakeCollection::class,['name'=>$name.'Collection','raw-name'=>$name]);
        $this->line("Generator Resource Success");
    }


}

3、创建 stub 模板文件

模板目录文件结构:
在这里插入图片描述
1、controller.stub

<?php

namespace DummyNamespace;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


class DummyClass extends Controller
{

    public function index()
    {
        //
    }


    public function store(Request $request)
    {
        //
    }


    public function show($id)
    {
        //
    }


    public function update(Request $request, $id)
    {
        //
    }


    public function destroy($id)
    {
        //
    }
}

2、service.stub

<?php

namespace DummyNamespace;

class DummyClass
{
    //
}

3、model.stub

<?php

namespace DummyNamespace;

use Illuminate\Database\Eloquent\Factories\HasFactory;


class DummyClass extends Model
{
    use HasFactory;

    public $guarded=["*"];

}

4、resource.stub

<?php

namespace DummyNamespace;

use Illuminate\Http\Resources\Json\JsonResource;

class DummyClass extends JsonResource
{
    public function toArray($request)
    {
        return parent::toArray($request);
    }

}

5、collection.stub

<?php

namespace DummyNamespace;

use Illuminate\Http\Resources\Json\ResourceCollection;

class DummyClass extends ResourceCollection
{
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

4、查看 Artisan 命令

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲁元

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值