Skip to content

Commit d129fcc

Browse files
authored
Merge pull request #1 from rmitesh/1.x
improve command syntax and update README.md file
2 parents 2cb37e7 + 96e1fca commit d129fcc

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ composer require rmitesh/laravel-pantry
2525

2626
To create a new Pantry class
2727
```bash
28-
php artisan make:pantry FoodPantry Food
28+
php artisan make:pantry Food
29+
```
30+
> Based on the Pantry class name, automatically consider model name will be same as Pantry class.
31+
32+
Or you want to generate for specific model.
33+
34+
```bash
35+
php artisan make:pantry Food --model=FoodItem
2936
```
3037

3138
It will create `FoodPantry` class inside the `app/Pantries` directory.

src/Console/Commands/MakePantryCommand.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class MakePantryCommand extends Command
1717
* @var string
1818
*/
1919
protected $signature = '
20-
make:pantry {pantry?} {model?}
20+
make:pantry {pantry?} {--model= : Generate pantry class for given model}
2121
';
2222

2323
/**
2424
* The console command description.
2525
*
2626
* @var string
2727
*/
28-
protected $description = 'Create a new pantry';
28+
protected $description = 'Create a new pantry class.';
2929

3030
/**
3131
* Execute the console command.
@@ -47,7 +47,7 @@ public function handle()
4747
$modelNamespace = 'App\\Models';
4848

4949
// Pantry Name
50-
$pantry = (string) Str::of($this->argument('pantry') ?? $this->askRequired('Pantry (e.g. `UserPantry`)', 'pantry'))
50+
$pantry = (string) Str::of($this->argument('pantry') ?? $this->askRequired('Pantry (e.g. `FoodPantry`)', 'pantry'))
5151
->studly()
5252
->trim('/')
5353
->trim('\\')
@@ -77,12 +77,15 @@ public function handle()
7777
->append('.php');
7878

7979
// Model Name
80-
$model = (string) Str::of($this->argument('model') ?? $this->askRequired('Model (e.g. `User`)', 'model'))
81-
->studly()
82-
->trim('/')
83-
->trim('\\')
84-
->trim(' ')
85-
->replace('/', '\\');
80+
$model = (string) Str::of($className)->replace('Pantry', '');
81+
if ( $this->option('model') ) {
82+
$model = (string) Str::of($this->option('model') ?? $this->askRequired('Model (e.g. `Food`)', 'model'))
83+
->studly()
84+
->trim('/')
85+
->trim('\\')
86+
->trim(' ')
87+
->replace('/', '\\');
88+
}
8689

8790
list($className, $namespace) = $this->getClassName($model);
8891

@@ -98,6 +101,10 @@ public function handle()
98101
'model' => $model,
99102
'modelNamespace' => $modelNamespace,
100103
]);
104+
105+
$this->info("Panrty [{$path}] created successfully.");
106+
107+
return static::SUCCESS;
101108
}
102109

103110
private function getClassName($name): array

0 commit comments

Comments
 (0)