diff --git a/README.md b/README.md index d4e8a45..224c886 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,14 @@ composer require rmitesh/laravel-pantry To create a new Pantry class ```bash -php artisan make:pantry FoodPantry Food +php artisan make:pantry Food +``` +> Based on the Pantry class name, automatically consider model name will be same as Pantry class. + +Or you want to generate for specific model. + +```bash +php artisan make:pantry Food --model=FoodItem ``` It will create `FoodPantry` class inside the `app/Pantries` directory. diff --git a/src/Console/Commands/MakePantryCommand.php b/src/Console/Commands/MakePantryCommand.php index 2934958..8efbe0b 100644 --- a/src/Console/Commands/MakePantryCommand.php +++ b/src/Console/Commands/MakePantryCommand.php @@ -17,7 +17,7 @@ class MakePantryCommand extends Command * @var string */ protected $signature = ' - make:pantry {pantry?} {model?} + make:pantry {pantry?} {--model= : Generate pantry class for given model} '; /** @@ -25,7 +25,7 @@ class MakePantryCommand extends Command * * @var string */ - protected $description = 'Create a new pantry'; + protected $description = 'Create a new pantry class.'; /** * Execute the console command. @@ -47,7 +47,7 @@ public function handle() $modelNamespace = 'App\\Models'; // Pantry Name - $pantry = (string) Str::of($this->argument('pantry') ?? $this->askRequired('Pantry (e.g. `UserPantry`)', 'pantry')) + $pantry = (string) Str::of($this->argument('pantry') ?? $this->askRequired('Pantry (e.g. `FoodPantry`)', 'pantry')) ->studly() ->trim('/') ->trim('\\') @@ -77,12 +77,15 @@ public function handle() ->append('.php'); // Model Name - $model = (string) Str::of($this->argument('model') ?? $this->askRequired('Model (e.g. `User`)', 'model')) - ->studly() - ->trim('/') - ->trim('\\') - ->trim(' ') - ->replace('/', '\\'); + $model = (string) Str::of($className)->replace('Pantry', ''); + if ( $this->option('model') ) { + $model = (string) Str::of($this->option('model') ?? $this->askRequired('Model (e.g. `Food`)', 'model')) + ->studly() + ->trim('/') + ->trim('\\') + ->trim(' ') + ->replace('/', '\\'); + } list($className, $namespace) = $this->getClassName($model); @@ -98,6 +101,10 @@ public function handle() 'model' => $model, 'modelNamespace' => $modelNamespace, ]); + + $this->info("Panrty [{$path}] created successfully."); + + return static::SUCCESS; } private function getClassName($name): array