From e5c20626541547bb441a6dbc72490112b1981a4d Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Sun, 23 Jun 2024 22:21:24 +0300 Subject: [PATCH] Added check for model existence during generation --- src/Console/ModelMakeCommand.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Console/ModelMakeCommand.php b/src/Console/ModelMakeCommand.php index fdcdb73..cf03181 100644 --- a/src/Console/ModelMakeCommand.php +++ b/src/Console/ModelMakeCommand.php @@ -12,7 +12,9 @@ use function array_filter; use function array_merge; +use function class_exists; use function compact; +use function Laravel\Prompts\error; use function Laravel\Prompts\select; use function Laravel\Prompts\text; @@ -27,7 +29,14 @@ class ModelMakeCommand extends Command public function handle(): void { - $model = $this->model(); + $model = $this->model(); + + if (! $this->validatedModel($model)) { + error("The model at `$model` namespace was not found."); + + return; + } + $columns = $this->columns(); $this->generateModel($model, $columns); @@ -50,18 +59,23 @@ protected function generateHelper(string $model): void $this->call(ModelsHelperCommand::class, compact('model')); } - protected function model(): string + protected function model(): ?string { if ($model = $this->argument('model')) { return $model; } return select( - label : 'Select the model for which you want to create a translation repository:', + label : 'Select a model to create a translation repository:', options: $this->models() ); } + protected function validatedModel(string $model): ?string + { + return class_exists($model) ? $model : null; + } + protected function columns(): array { if ($columns = $this->option('columns')) {