You've already forked laravel-helper
feat: crud
This commit is contained in:
123
src/Console/MakeCRUD.php
Normal file
123
src/Console/MakeCRUD.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace BAG\Laravel\Console;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
|
||||
#[AsCommand(name: 'bag:crud')]
|
||||
class MakeCRUD extends ProxyCommand
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'bag:crud {model?}';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Génère un CRUD complet.';
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function proxyHandle()
|
||||
{
|
||||
$this->comment("Note : si le model existe déjà, il ne sera pas écrasé.");
|
||||
|
||||
$model = $this->argument('model');
|
||||
|
||||
if (! $model) {
|
||||
$model = $this->ask("Nom du modèle ? (ex : User)");
|
||||
|
||||
if (empty($model)) {
|
||||
$this->error("Nom de model invalide.");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
$controller = $this->confirm("Générer un Controller ?", true);
|
||||
$policy = $this->confirm("Générer une Policy ?", $controller);
|
||||
$resource = $this->confirm("Générer une Resource ?", $controller);
|
||||
$views = $this->confirm("Générer des vues ?", true);
|
||||
|
||||
if (! $controller) {
|
||||
$routing = false;
|
||||
} else {
|
||||
$routing = $this->confirm("Ajouter automatiquement les routes ?", true);
|
||||
}
|
||||
|
||||
$model_name = "App\\Models\\$model";
|
||||
|
||||
if (class_exists($model_name)) {
|
||||
$this->comment("Le model existe déjà - ignoré.");
|
||||
}
|
||||
|
||||
$controller_name = "App\\Http\\Controllers\\{$model}Controller";
|
||||
|
||||
if ($controller && class_exists($controller_name)) {
|
||||
$this->comment("Le controller existe déjà - ignoré.");
|
||||
$controller = false;
|
||||
}
|
||||
|
||||
$policy_name = "App\\Policies\\{$model}Policy";
|
||||
|
||||
if ($policy && class_exists($policy_name)) {
|
||||
$this->comment("La policy existe déjà - ignorée.");
|
||||
$policy = false;
|
||||
}
|
||||
|
||||
$resource_name = "App\\Http\\Resources\\{$model}Resource";
|
||||
|
||||
if ($resource && class_exists($resource_name)) {
|
||||
$this->comment("La resource existe déjà - ignorée.");
|
||||
$resource = false;
|
||||
}
|
||||
|
||||
$result = app(\BAG\Stack\Laravel\MakeCRUD::class)->run($model, [
|
||||
'model' => ! class_exists($model_name),
|
||||
'controller' => $controller,
|
||||
'policy' => $policy,
|
||||
'resource' => $resource,
|
||||
'views' => $views,
|
||||
]);
|
||||
|
||||
if (! $result) {
|
||||
$this->error("Erreur lors de la génération du CRUD.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ($routing) {
|
||||
$this->addRoutes($model);
|
||||
}
|
||||
|
||||
$this->info("CRUD généré avec succès.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function addRoutes($model)
|
||||
{
|
||||
$file = base_path('routes/web.php');
|
||||
|
||||
if (! file_exists($file)) {
|
||||
$this->warn("Le fichier $file n'existe pas - ajout des routes ignoré.");
|
||||
return;
|
||||
}
|
||||
|
||||
$prefix = Str::snake(Str::plural($model));
|
||||
$test = "Route::resource('{$prefix}',";
|
||||
$line = "Route::resource('{$prefix}', \App\Http\Controllers\\{$model}Controller::class)->withTrashed(['show'])->middleware('auth');";
|
||||
|
||||
$routes = trim(file_get_contents($file));
|
||||
|
||||
if (strpos($routes, $test) !== false) {
|
||||
$this->warn("Les routes existent déjà - ajout des routes ignoré.");
|
||||
return;
|
||||
}
|
||||
|
||||
$routes .= "\n\n$line\n";
|
||||
|
||||
file_put_contents(base_path('routes/web.php'), $routes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user