你能帮我解决我遇到的问题吗?我创建了一个命令,用于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';
}
}
Run Code Online (Sandbox Code Playgroud)
编辑#1
我的目录中有 2 个存根文件:
app/Console/Commands/stubs/MakeRepository
ModelRepository.stub
ModelRepositoryInterface.stub
Run Code Online (Sandbox Code Playgroud)
我希望当您执行命令...ex: 时php artisan make:repository Blog,这两个文件将在以下目录中创建:
/app/Repositories/Blog/BlogRepository.php
/app/Repositories/Blog/BlogRepositoryInterface.php
Run Code Online (Sandbox Code Playgroud)
您可以使用 glob 将存根放入数组中,然后循环它们以创建多个文件
foreach(glob(stubs_path('stubs/Repository/*.stub') as $stub){
copy(stubs_path('stubs/Repository/'.$stub), $repositoryLocation . 'Repository.php');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2443 次 |
| 最近记录: |