【问题标题】:Generate multiple files with laravel command使用 laravel 命令生成多个文件
【发布时间】:2020-05-26 23:40:16
【问题描述】:

您能帮我解决我遇到的问题吗?我使用php artisan make:command 创建了一个命令,用于从现有模型生成存储库类型类。问题是我需要生成 2 个或更多文件,而不是从存根生成单个文件。我找不到有关该主题的文档。目前我已经实现的是我只从一个模板生成一个文件。

<?php

namespace App\Console\Commands;

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

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

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

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

    /**
     * @inheritDoc
     */
    protected function getStub()
    {
        return __DIR__ . '/stubs/MakeRepository/ModelRepositoryInterface.stub';
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [
            ['name', InputArgument::REQUIRED, 'The name of the model to which the repository will be generated'],
        ];
    }

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

编辑#1

我在目录中有 2 个存根文件:

app/Console/Commands/stubs/MakeRepository

ModelRepository.stub
ModelRepositoryInterface.stub

我希望当你执行命令时...例如:php artisan make:repository Blog,这两个文件是在以下目录中创建的:

/app/Repositories/Blog/BlogRepository.php
/app/Repositories/Blog/BlogRepositoryInterface.php

【问题讨论】:

  • 您要创建哪些文件?
  • @Rwd 感谢您的回答,请参阅我的编辑#1

标签: php laravel laravel-artisan


【解决方案1】:

您可以编写一个新的命令来创建存储库接口,然后在 MakeRepository 中调用它。

我认为这种方法符合SRP。

// In MakeRepository.php

// Override handle method
public function handle()
{
    if (parent::handle() === false && ! $this->option('force')) {
        return false;
    }

    if ($this->option('interface')) {
        $this->call('make:repository-interface', ['name' => $this->getNameInput() . 'Interface']);

    }
}

/**
 * Get the console command arguments.
 *
 * @return array
 */
protected function getArguments()
{
    return [
        ['name', InputArgument::REQUIRED, 'The name of the model to which the repository will be generated'],
        ['interface', 'i', InputOption::VALUE_NONE, 'Create a new interface for the repository'],
    ];
}

也可以参考官方make model的代码。 https://github.com/laravel/framework/blob/6.x/src/Illuminate/Foundation/Console/ModelMakeCommand.php

【讨论】:

  • 这个解决方案看起来不错,但理想的情况是在单个命令中使用它,并传递一个参数来定义是否为该存储库创建接口,例如:php artisan make:repository Blog --interface ,无论如何,现在你的答案似乎是一个可能的解决方案
  • 如果你只想写一个命令,你可能需要重写更多的GeneratorCommand方法或者自己写一个新的。
【解决方案2】:

您可以使用 glob 将存根放入数组中,然后遍历它们以创建多个文件

foreach(glob(stubs_path('stubs/Repository/*.stub') as $stub){
            copy(stubs_path('stubs/Repository/'.$stub), $repositoryLocation . 'Repository.php');
}

【讨论】:

    猜你喜欢
    • 2021-07-25
    • 2017-05-19
    • 2020-07-09
    • 2020-10-24
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多