22. Laravel Stub 定制

Stub 定制

Laravel 中可以适用 Artisan的make命令创建各种类, 比如 make:controller 、 make:model 等。 这些类是使用 stub文件生成的。

可以通过 stub:publish 命令将最常见的 Stub 命令发布到你的应用程序中, 方便更改

root@7340a0562010:/var/www/ogenes/Genes-Admin# php81 artisan  stub:publish
root@7340a0562010:/var/www/ogenes/Genes-Admin# ls -l stubs/
total 156
-rw-r--r-- 1 root root  504 Sep  6 10:10 cast.inbound.stub
-rw-r--r-- 1 root root  821 Sep  6 10:10 cast.stub
-rw-r--r-- 1 root root  519 Sep  6 10:10 console.stub
…………

1. 修改Stub命令

以 make:request 命令, 生成的request 经过代码结构改造都需要重构方法 failedValidation(Validator $validator), 这里就可以做以下调整:

基类:

#vim app/Http/Requests/BaseRequest.php
<?php

namespace App\Http\Requests;

use App\Exceptions\CommonException;
use App\Exceptions\ErrorCode;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

class BaseRequest extends FormRequest
{
 /**
  * Desc:
  * @param Validator $validator
  * @return void
  * @throws CommonException
  * @author: ogenes 
  */
 public function failedValidation(Validator $validator): void
 {
     throw new CommonException(
         ErrorCode::INVALID_ARGUMENT,
         $validator->errors()->first()
     );
 }
}

修改:

#vim stubs/request.stub

<?php

namespace {{ namespace }};

use App\Http\Requests\BaseRequest;

class {{ class }} extends BaseRequest
{

 /**
  * Get the validation rules that apply to the request.
  *
  * @return array<string, mixed>
  */
 public function rules()
 {
     return [
     
     ];
 }
}

测试:

root@7340a0562010:/var/www/ogenes/Genes-Admin# php artisan make:request T/TestRequest
<?php

namespace App\Http\Requests\T;

use App\Http\Requests\BaseRequest;

class TestRequest extends BaseRequest
{

 /**
  * Get the validation rules that apply to the request.
  *
  * @return array
  */
 public function rules()
 {
     return [

     ];
 }
}

2. 自定义Stub命令

考虑到需要经常创建service, 这里创建一个service模板

自定义文件:

#vim stubs/service.stub
<?php

namespace {{ namespace }};

use {{ rootNamespace }}Services\BaseService;

class {{ class }} extends BaseService
{

}

自定义命令行:

#vim app/Console/Tools/ServiceMakeCommand.php
<?php

namespace App\Console\Tools;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'make:service')]
class ServiceMakeCommand extends GeneratorCommand
{
 /**
  * The console command name.
  *
  * @var string
  */
 protected $name = 'make:service';

 /**
  * The console command description.
  *
  * @var string
  */
 protected $description = 'Create a new service class';

 /**
  * The type of class being generated.
  *
  * @var string
  */
 protected $type = 'Service';

 /**
  * Get the stub file for the generator.
  *
  * @return string
  */
 protected function getStub()
 {
     return base_path() . '/stubs/service.stub';
 }

 /**
  * Get the default namespace for the class.
  *
  * @param  string  $rootNamespace
  * @return string
  */
 protected function getDefaultNamespace($rootNamespace)
 {
     return $rootNamespace.'\Services';
 }

 /**
  * Build the class with the given name.
  *
  * Remove the base controller import if we are already in the base namespace.
  *
  * @param  string  $name
  * @return string
  */
 protected function buildClass($name)
 {
     $controllerNamespace = $this->getNamespace($name);

     $replace["use {$controllerNamespace}\BaseService;\n"] = '';

     return str_replace(
         array_keys($replace), array_values($replace), parent::buildClass($name)
     );
 }
}


注册命令

#vim app/Console/Kernel.php
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');
        $this->load(__DIR__.'/Tools');

        require base_path('routes/console.php');
    }

测试

root@7340a0562010:/var/www/ogenes/Genes-Admin# php artisan make:service TestService

你可能感兴趣的:(laravel,vim,php)