Skip to content

Commit

Permalink
Merge pull request #1 from rmitesh/1.x
Browse files Browse the repository at this point in the history
improve command syntax and update README.md file
  • Loading branch information
rmitesh committed Sep 18, 2023
2 parents 2cb37e7 + 96e1fca commit d129fcc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 16 additions & 9 deletions src/Console/Commands/MakePantryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class MakePantryCommand extends Command
* @var string
*/
protected $signature = '
make:pantry {pantry?} {model?}
make:pantry {pantry?} {--model= : Generate pantry class for given model}
';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new pantry';
protected $description = 'Create a new pantry class.';

/**
* Execute the console command.
Expand All @@ -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('\\')
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down

0 comments on commit d129fcc

Please sign in to comment.