我已经用这个命令为我的项目创建了一个Artisan Command
:
`php artisan make:console Repositories`
以上自定义命令的签名为:
protected $signature = 'make:repository {modelName : The name of the model}';
当这个命令被执行/触发时,将创建2个文件:
-
app/Http/Repositories/Contracts/modelNameRepositoryContract.php
-
app/Http/Repositories/Eloquent/modelNameRepository.php
现在,我希望名称空间,className应该在默认情况下写入。就像我们发射make:controller ModelController
或make:model Model
时得到的一样。这些文件已经编写了它需要的默认内容。我想要的只是类似的
我想在默认情况下使用名称空间、使用名称空间和类/契约名称填充文件。这里是handle()
方法,来自 repository。php文件:
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$modelName = $this->argument('modelName');
if ($modelName === '' || is_null($modelName) || empty($modelName)) {
$this->error('Model Name Invalid..!');
}
if (! file_exists('app/Http/Repositories/Contracts') && ! file_exists('app/Http/Repositories/Eloquent')) {
mkdir('app/Http/Repositories/Contracts', 0775, true);
mkdir('app/Http/Repositories/Eloquent', 0775, true);
$contractFileName = 'app/Http/Repositories/Contracts/' . $modelName . 'RepositoryContract.php';
$eloquentFileName = 'app/Http/Repositories/Eloquent/' . $modelName . 'Repository.php';
if(! file_exists($contractFileName) && ! file_exists($eloquentFileName)) {
$contractFileContent = "<?php'n'nnamespace App''Http''Repositories''Contracts;'n'ninterface " . $modelName . "RepositoryContract'n{'n}";
file_put_contents($contractFileName, $contractFileContent);
$eloquentFileContent = "<?php'n'nnamespace App''Http''Repositories''Eloquent;'n'nuse App''Repositories''Contracts''".$modelName."RepositoryContract;'n'nclass " . $modelName . "Repository implements " . $modelName . "RepositoryContract'n{'n}";
file_put_contents($eloquentFileName, $eloquentFileContent);
$this->info('Repository Files Created Successfully.');
} else {
$this->error('Repository Files Already Exists.');
}
}
}
我知道上面的方法不是使用Artisan命令创建文件的正确方法。那么,我应该如何创建文件并用默认的东西填充它呢?我在文档中找不到任何与此相关的内容。
有谁能帮我一下吗?
我认为最好实现现有的Laravel Generator - 'Illuminate'Console'GeneratorCommand
查看'Illuminate'Foundation'Console'ModelMakeCommand,看看它是如何完成的。我相信你可以创建模板并注入一些可以在飞行中替换的东西。
这是我创建的代码,用于帮助我使用Artisan命令重新创建View Composer文件,因为默认情况下没有提供该命令。也许这对你有帮助。注意:这是为Laravel 8
<?php
namespace viewmodelsimple'Console;
use Illuminate'Console'Command;
use Illuminate'Filesystem'Filesystem;
class ViewComposer extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'view:composer {composer}';
protected $files;
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a view composer';
/**
* Create a new command instance.
*
* @return void
*/
// public function __construct()
public function __construct(Filesystem $files)
{
$this->files=$files;
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$viewComposer=$this->argument('composer');
if ($viewComposer === '' || is_null($viewComposer) || empty($viewComposer)) {
return $this->error('Composer Name Invalid..!');
}
$contents=
'<?php
namespace App'ViewComposers;
use Illuminate'View'View;
class '.$viewComposer.'
{
/**
* Create a new '.$viewComposer.' composer.
*
* @return void
*/
public function __construct()
{
// Dependencies automatically resolved by service container...
}
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
//Bind data to view
}
}';
if ($this->confirm('Do you wish to create '.$viewComposer.' Composer file?')) {
$file = "${viewComposer}.php";
$path=app_path();
$file=$path."/ViewComposers/$file";
$composerDir=$path."/ViewComposers";
if($this->files->isDirectory($composerDir)){
if($this->files->isFile($file))
return $this->error($viewComposer.' File Already exists!');
if(!$this->files->put($file, $contents))
return $this->error('Something went wrong!');
$this->info("$viewComposer generated!");
}
else{
$this->files->makeDirectory($composerDir, 0777, true, true);
if(!$this->files->put($file, $contents))
return $this->error('Something went wrong!');
$this->info("$viewComposer generated!");
}
}
}
}