使用make命令建立Service類

阿珂發表於2022-05-25

使用以下命令生成命令列檔案

php artisan make:command MakeService
<?php

namespace App\Console\Commands;

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

class MakeService extends GeneratorCommand
{

    /**
     * The console command name.
     * 控制檯命令名。
     * @var string
     */
    protected $name = 'make:service';

    /**
     * The console command description.
     * console命令說明。
     * @var string
     */
    protected $description = '生成service物件類';

    /**
     * The type of class being generated.
     * 生成的類的型別。
     * @var string
     */
    protected $type = 'Service';

    public function buildClass($name)
    {
        $stub = parent::buildClass($name);

        $model = $this->option('model');

        return $model ? $this->replaceModel($stub, $model) : $stub;
    }

    /**
     * @description:Get the stub file for the generator.
     * @description:獲取生成器的存根檔案。
     * @return string
     * @Author:AKE
     * @Date:2022/5/24 10:50
     */
    protected function getStub()
    {
        return $this->option('model') ?
            $this->laravel->basePath('/stubs/service.plain.stub') :
            $this->laravel->basePath('/stubs/service.stub');
    }

    /**
     * @description:Get the default namespace for the class.
     * @description:獲取預設名稱空間
     * @param string $rootNamespace
     * @return string
     * @Author:AKE
     * @Date:2022/5/24 10:50
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace . '\Services';
    }

    /**
     * @description:替換給定存根的模型
     * @param $stub
     * @param $model
     * @return array|string|string[]
     * @Author:AKE
     * @Date:2022/5/24 11:50
     */
    private function replaceModel($stub, $model)
    {
        $modelClass = $this->buildModel($model);

        $replace = [
            'DummyFullModelClass' => $modelClass,
            '{{namespacedModel}}' => $modelClass,
            '{{ namespacedModel }}' => $modelClass,
        ];

        return str_replace(
            array_keys($replace), array_values($replace), $stub
        );
    }

    /**
     * @description:構建model名
     * @param $model
     * @return string
     * @Author:AKE
     * @Date:2022/5/24 11:47
     */
    private function buildModel($model)
    {
        if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
            throw new \InvalidArgumentException('模型名稱包含無效字元。');
        }
        return $this->qualifyModel($model);
    }

    /**
     * @description:新增引數
     * @return array[]
     * @Author:AKE
     * @Date:2022/5/24 13:04
     */
    protected function getOptions()
    {
        #第一個引數,為變數名,即 --model 呼叫或者 --model= 呼叫
        #第二個引數,為別名,即簡寫 -m 呼叫
        #第三個引數,Symfony\Component\Console\Input\InputOption 中的常量
        #第四個引數,為描述
        #第五個引數,為預設值 InputOption::VALUE_NONE 時必須為 null
        return [
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the service applies to.'],
        ];
    }
}

存根/模板檔案預設放在根目錄的 stubs下面

檔名 service.stub

<?php

namespace {{ namespace }};

class {{ class }}
{

}

檔名 service.plain.stub

<?php

namespace {{ namespace }};

use {{ namespacedModel }} as model;

class {{ class }}
{

}

#使用命令

// 不新增 model 引數
php artisan make:service IndexService
// 新增 model 引數 (以下三種命令都可)
php artisan make:service IndexService --model=Index
php artisan make:service IndexService --model Index
php artisan make:service IndexService -m Index
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章