90 lines
2.2 KiB
PHP
90 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
class MakeRepository extends Command
|
|
{
|
|
protected $signature = 'make:repository {name}';
|
|
protected $description = 'Create a new repository with interface';
|
|
|
|
public function handle()
|
|
{
|
|
$name = $this->argument('name');
|
|
$interfaceName = "{$name}RepositoryInterface";
|
|
$repositoryName = "{$name}Repository";
|
|
|
|
// Paths
|
|
$interfacePath = app_path("Repositories/Contracts/{$interfaceName}.php");
|
|
$repositoryPath = app_path("Repositories/Eloquent/{$repositoryName}.php");
|
|
|
|
// Make directories if not exist
|
|
if (!File::exists(app_path('Repositories/Contracts'))) {
|
|
File::makeDirectory(app_path('Repositories/Contracts'), 0755, true);
|
|
}
|
|
if (!File::exists(app_path('Repositories/Eloquent'))) {
|
|
File::makeDirectory(app_path('Repositories/Eloquent'), 0755, true);
|
|
}
|
|
|
|
// Interface stub
|
|
$interfaceContent = "<?php
|
|
|
|
namespace App\Repositories\Contracts;
|
|
|
|
interface {$interfaceName}
|
|
{
|
|
public function getAll();
|
|
public function findById(\$id);
|
|
public function create(array \$data);
|
|
public function update(\$id, array \$data);
|
|
public function delete(\$id);
|
|
}
|
|
";
|
|
File::put($interfacePath, $interfaceContent);
|
|
|
|
// Repository stub
|
|
$repositoryContent = "<?php
|
|
|
|
namespace App\Repositories\Eloquent;
|
|
|
|
use App\Models\\{$name};
|
|
use App\Repositories\Contracts\\{$interfaceName};
|
|
|
|
class {$repositoryName} implements {$interfaceName}
|
|
{
|
|
public function getAll()
|
|
{
|
|
return {$name}::all();
|
|
}
|
|
|
|
public function findById(\$id)
|
|
{
|
|
return {$name}::findOrFail(\$id);
|
|
}
|
|
|
|
public function create(array \$data)
|
|
{
|
|
return {$name}::create(\$data);
|
|
}
|
|
|
|
public function update(\$id, array \$data)
|
|
{
|
|
\$model = {$name}::findOrFail(\$id);
|
|
\$model->update(\$data);
|
|
return \$model;
|
|
}
|
|
|
|
public function delete(\$id)
|
|
{
|
|
\$model = {$name}::findOrFail(\$id);
|
|
return \$model->delete();
|
|
}
|
|
}";
|
|
File::put($repositoryPath, $repositoryContent);
|
|
|
|
$this->info("Repository & Interface for {$name} created successfully!");
|
|
}
|
|
}
|