使用make命令创建Service类

使用以下命令生成命令行文件

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下面

默认不传 model 参数的存根/模板文件

文件名 service.stub

<?php

namespace {{ namespace }};

class {{ class }}
{

}

传 model 参数的存根/模板文件

文件名 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 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 3

:joy: 怎么不行啊 模板里面的变量没有替换掉 用的laravel6

1年前 评论
阿珂 (楼主) 1年前
bing (作者) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!