Я создал 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()
из файла Repositories.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-команды. Итак, как мне создать файл и заполнить его значениями по умолчанию. Я не мог найти ничего связанного с этим в документации.
Так может ли кто-нибудь мне с этим помочь?
Заранее спасибо.