I have created a Artisan Command
for my project with this command:
`php artisan make:console Repositories`
The signature for the above custom command is:
protected $signature = 'make:repository {modelName : The name of the model}';
When this command gets executed / fired, 2 files are created:
app/Http/Repositories/Contracts/modelNameRepositoryContract.php
app/Http/Repositories/Eloquent/modelNameRepository.php
Now, I want that the namespace, the className should be written by default. Just like what we get when firing the make:controller ModelController
or make:model Model
. Those files have written the default things that it needs to have. I want similar to that only.
I want to populate the file with the namespace, the use namespace and the class/contract name by default. And for that here's the handle()
method from Repositories.php file:
/**
* 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.');
}
}
}
I know that the above method is not the correct way to create the file using the Artisan command. So, how should I create the file and populate it with the default things. I could not find it anything related to this in the docs.
So can anybody help me out with this ?
Thanks in advance.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire