Reputation: 4020
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.
Upvotes: 1
Views: 7030
Reputation: 57
This is code I created to help me recreate the View Composer file using Artisan command, since the command is not provided by default. Perhaps this may help you. Note: This is for 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!");
}
}
}
}
Upvotes: 2
Reputation: 31
I think that it would be best to implement existing Laravel Generator - \Illuminate\Console\GeneratorCommand
Take a look at \Illuminate\Foundation\Console\ModelMakeCommand to see how it is done. I'm sure you can create template and inject some thing that can be replaced on the fly.
Upvotes: 3