feat: crud

This commit is contained in:
Maxime 2024-04-19 19:03:17 +02:00
parent 4eae25ab02
commit 8af5aebfe3
6 changed files with 173 additions and 26 deletions

View File

@ -1,31 +1,45 @@
# BAG Laravel Helper
This package provides Laravel commands to execute BAG for Laravel Stack.
Ce package Laravel fournit des commandes pour générer du code via BAG par Bluesquare.
## Installation
Edit your `composer.json` file to include the following dependency:
Mettre à jour les sources de `composer.json` avec :
```
"require": {
"bag/laravel-helper": "dev-main"
},
"repositories": [
{
"type": "vcs",
"url": "https://git.bluesquare.io/bag/laravel-helper.git"
}
]
```shell
composer config repositories.bag vcs https://git.bluesquare.io/bag/laravel-helper.git -n
```
Then run:
Puis installer le package :
```bash
composer update bag/laravel-helper
```shell
composer require bag/laravel-helper
```
## Usage
Pour installer BAG :
```shell
php artisan bag:install
```
Pour générer un CRUD en créant/partant d'un model :
```shell
php artisan bag:crud
```
```shell
sail artisan bag:crud
```
Pour générer des migrations automatiquement :
```shell
php artisan bag:migrations
```
```shell
sail artisan bag:migrations
```

View File

@ -15,7 +15,7 @@ class Install extends ProxyCommand
/**
* @var string
*/
protected $description = 'Install required packages';
protected $description = 'Installer les packages requis';
/**
* @return int|null
@ -23,7 +23,7 @@ class Install extends ProxyCommand
public function handle()
{
if (env('LARAVEL_SAIL')) {
$this->error("🐳 To install packages, please run 'php artisan bag:install' outside Sail.");
$this->error("🐳 Pour installer les packages, exécuter 'php artisan bag:install' en dehors de Sail.");
return 0;
}

123
src/Console/MakeCRUD.php Normal file
View 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);
}
}

View File

@ -15,13 +15,21 @@ class MakeMigrations extends ProxyCommand
/**
* @var string
*/
protected $description = 'Generate migrations based on current database schema and models';
protected $description = 'Génère des migrations en comparant les models et les tables existants.';
/**
* @return int|null
*/
public function proxyHandle()
{
return app(\BAG\Stack\Laravel\MakeMigrations::class)->run();
$result = app(\BAG\Stack\Laravel\MakeMigrations::class)->run();
if ($result) {
$this->info('Migrations générées !');
} else {
$this->info('Aucune migration générée.');
}
return 0;
}
}

View File

@ -26,7 +26,7 @@ abstract class ProxyCommand extends Command
$package_path = "{$this->base_path}/packages/$package";
if (! is_dir($package_path)) {
$this->error("🚨 Missing package bag/$package. Please run 'php artisan bag:install' first.");
$this->error("🚨 Le package bag/$package est manquant. Merci d'exécuter 'php artisan bag:install' pour commencer.");
return 1;
}
@ -57,28 +57,28 @@ abstract class ProxyCommand extends Command
$package_path = "$install_path/$package";
if (! is_dir($package_path)) {
$this->info("🚚 Installing bag/$package...");
$this->info("🚚 Installation de bag/$package...");
$result = Process::path($install_path)
->run("git clone git@git.bluesquare.io:bag/$package.git $package");
if ($result->failed()) {
throw new \Exception("Failed to fetch bag/$package");
throw new \Exception("Récupération de bag/$package impossible.");
}
} elseif (! cache()->has("bag/$package")) {
$this->info("🤖 Updating bag/$package...");
$this->info("🤖 Mise à jour de bag/$package...");
$result = Process::path($package_path)->run("git pull");
if ($result->failed()) {
$this->warn("😰 Failed to update!");
$this->warn("😰 Échec de mise à jour !");
}
cache()->put("bag/$package", true, now()->addMinutes(5));
}
$this->info("🐠 Loading bag/$package...");
$this->info("🐠 Chargement de bag/$package...");
$result = Process::path($package_path)->run("composer install --ignore-platform-reqs");
$this->info("⚡️ Installed!");
$this->info("⚡️ Installation réussie !");
}
}

View File

@ -30,6 +30,7 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider implements Def
$this->commands([
Console\Install::class,
Console\MakeMigrations::class,
Console\MakeCRUD::class,
//...
]);
}
@ -44,6 +45,7 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider implements Def
return [
Console\Install::class,
Console\MakeMigrations::class,
Console\MakeCRUD::class,
];
}
}