【发布时间】: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