使用stub快速生成自定義檔案

神的孩子丶都在跳舞發表於2021-12-16

建一個stub模板,用於下面使用

在resources下新建stubs目錄用於儲存
新建一個query-download.stub

<?php

namespace DummyNamespace;

use App\Exports\QueryExport;

class DummyClass extends QueryExport
{

    public function headings(): array
    {
        //表頭
         return [
             //
         ];
    }

    public function columns($row): array
    {
        //每一列的資料
         return [
             //
         ];
    }

    public function stringColumns(): array
    {
        //要轉為字串的列標
         return [
            //
         ];
    }

}

stub佔位符介紹

stub檔案中會有很多佔位符,下面說幾個,還有很多佔位符,後面再熟悉一下

  • DummyNamespace : 名稱空間
  • DummyRootNamespace : 根名稱空間
  • DummyClass : 類名
 php artisan make:command Make/Download

生成好的新的command,需要如下操作

  • 修改繼承GeneratorCommand類並必須實現getStub方法
  • 移除自動生成的protected $signature改為protected $name,其中$name就是最後要執行的命令了

下面直接上程式碼

<?php

namespace App\Console\Commands\Make;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;

class Download extends GeneratorCommand
{
    /**
     * 執行的命令
     *
     * @var string
     */
    protected $name = 'make:download';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成下載中心';

    /**
     * 這裡是宣告生成的檔案型別,在新建成功時,命令列會顯示xxx created successfully.這裡的xxx在這就是Download
     *
     * @var string
     */
    protected $type = 'Download';


    /**
     * 檔案模板的地址,這裡就是第一步新建的stub檔案的路徑
     * @return string
     */
    public function getStub(): string
    {
        if ($this->option('array')) {
            return base_path('resources/stubs/array-download.stub');
        }else{
            return base_path('resources/stubs/query-download.stub');
        }
    }

    /**
     * 獲取檔案的預設的名稱空間,因為檔案打算建立在app下的Exports目錄下,所以修改了一下這個檔案,若要生成在app下,可不重寫此方法
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace): string
    {
        return $rootNamespace . '\Exports';
    }

    /**
     * 定義可輸入的引數
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [
            //VALUE_NONE 不接受選項的輸入,預設值
            //VALUE_REQUIRED 必須傳入一個值
            //VALUE_OPTIONAL 可以傳值也可以不傳值
            //VALUE_IS_ARRAY 可以傳多個值 --dir=/foo --dir=/bar
            //VALUE_NEGATABLE 可以正值也可以負值
            ['query', '', InputOption::VALUE_NONE, 'Generate an export for a query.'],
            ['array', 'a', InputOption::VALUE_NONE, 'Generate an export for an array.']
        ];
    }
}

其他可用方法

    protected function buildClass($name)
    {
        //可以根據傳入的引數,自定義其他操作
    }


    protected function replaceNamespace(&$stub, $name)
    {
       //可以修改底層佔位符和自定義佔位符
    }

使用

query型別測試

執行生成命令,生成query型別的下載器

使用stub快速生成自定義檔案

使用stub快速生成自定義檔案

array型別測試

執行生成命令,生成array型別的下載器

使用stub快速生成自定義檔案

使用stub快速生成自定義檔案

ok,就醬

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章