Laravel 创建自定义的 artisan make 命令

我在项目中用到了repository 模式 需要新创建repository模板,我在想是不是也用命令生成来比较好一些,我搜了一些命令找到 相对应的文件

           Illuminate\Foundation\Console\ProviderMakeCommand

           Illuminate\Routing\Console\ControllerMakeCommand

使用命令创建一个新的命令生成文件

        php artisan make:command MakeRepository

文件内容

<?php

namespace App\Console\Commands;

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

class RepositoryMakeCommand extends GeneratorCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'make:repository';


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

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


    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return '.\resources\stubs\Repository\repository.stub';    对应你的模板文件目录
    }

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

    /**
     * Replace the namespace for the given stub.
     *
     * @param  string $stub
     * @param  string $name
     * @return $this
     */
    protected function replaceNamespace(&$stub, $name)
    {

        $stub = str_replace(
            ['RepositoryNamespace', 'ModelName'],
            [$this->getNamespace($name), $this->setModel()],
            $stub
        );

        return $this;
    }


    /**
     * set Model
     *
     */
    private function setModel()
    {
        if (!empty($this->option('model'))) {
            return $this->option('model');
        } else {
            $name = explode('/', $this->getNameInput('name'));
            return str_replace('Repository','',$name[count($name)-1]);
        }
    }


    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'Injection  model.']
        ];
    }
}

 

然后创建 Repository 模板文件 repository.stub  

 /resources/stubs/repository.stub  我是把模板目录放到这个地方,和上面引入的路径一样就行

文件内容(也可以自已按照需求定义自己的模板)

 

<?php

namespace RepositoryNamespace;

use App\Model\ModelName;
use App\Repositories\EloquentRepository;

class DummyClass  extends EloquentRepository
{

    public function __construct(ModelName $model)
    {
        parent::__construct($model);
    }

}

 

RepositoryNamespace和ModelName 这俩个是自动分配到模板的

 

注册命令类

将RepositoryMakeCommand添加到 App\Console\Kernel.php

protected $commands = [  

           Commands\RepositoryMakeCommand::class

];

最后使用

php artisan make:repository /Test/TestRepository  生成到 app/repositorys/Test/TestRepository.php

php artisan make:repository TestRepository  生成到 app/repositorys/TestRepository.php

php artisan make:repository TestRepository --model=AAA  生成到 app/repositorys/TestRepository.php,指定model AAA

我们还设置了一个model的参数 用来指定model 不设置默认为 你的Repository文件名,你也可以设置自己需要的参数

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值