diff --git a/config/ide-helper.php b/config/ide-helper.php index e3bdc574c..afe3ecad4 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -11,7 +11,7 @@ | */ - 'filename' => '_ide_helper.php', + 'filename' => '_ide_helper.php', /* |-------------------------------------------------------------------------- @@ -155,6 +155,19 @@ ], + /* + |-------------------------------------------------------------------------- + | Models to silence warnings + |-------------------------------------------------------------------------- + | + | Define which models should have warnings silenced + | + */ + + 'silenced_models' => [ + // App\Models\User::class + ], + /* |-------------------------------------------------------------------------- | Models hooks diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index d2d484ff5..020324706 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -12,6 +12,7 @@ namespace Barryvdh\LaravelIdeHelper\Console; use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface; +use Barryvdh\LaravelIdeHelper\Enums\ModelWarning; use Barryvdh\Reflection\DocBlock; use Barryvdh\Reflection\DocBlock\Context; use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer; @@ -116,6 +117,8 @@ class ModelsCommand extends Command */ protected $foreignKeyConstraintsColumns = []; + protected $modelWarnings = []; + /** * During initialization we use Laravels Date Facade to * determine the actual date class and store it here. @@ -175,7 +178,9 @@ public function handle() $content = $this->generateDocs($model, $ignore); - if (!$this->write || $this->write_mixin) { + $this->handleWarnings(); + + if (! $this->write || $this->write_mixin) { $written = $this->files->put($filename, $content); if ($written !== false) { $this->info("Model information was written to $filename"); @@ -280,7 +285,11 @@ protected function generateDocs($loadModels, $ignore = '') $model = $this->laravel->make($name); - $this->getPropertiesFromTable($model); + try { + $this->getPropertiesFromTable($model); + } catch (Throwable $e) { + $this->tableInspectionFailedForModel($model, $e); + } if (method_exists($model, 'getCasts')) { $this->castPropertiesType($model); @@ -1626,4 +1635,58 @@ protected function setForeignKeys($schema, $table) } } } + + protected function handleWarnings(): void + { + if (! empty($this->modelWarnings)) { + foreach ($this->modelWarnings as $modelClass => $warnings) { + $this->newline(); + $this->warn("{$modelClass} has the following warnings:"); + foreach ($warnings as $warning) { + $this->warn('• '.$warning->message()); + } + } + + $this->newline(); + $this->warn('There are warnings for some of the models that we tried to generate docs for. Please see the output above for more information.'); + } + } + + protected function gracefullyHandleExceptions(): bool + { + return ! is_null($this->laravel['config']->get('ide-helper.silenced_models')); + } + + protected function warningsAreSilencedForModel(Model $model): bool + { + $silencedModels = $this->laravel['config']->get('ide-helper.silenced_models'); + + return collect($silencedModels)->contains($model::class); + } + + protected function tableInspectionFailedForModel(Model $model, ?Throwable $e = null): void + { + if ($e && ! $this->gracefullyHandleExceptions()) { + throw $e; + } + + if ($this->warningsAreSilencedForModel($model)) { + return; + } + + $modelName = $model::class; + + $connectionName = $model->getConnectionName() ?? $this->laravel['config']->get('database.default'); + $driver = $model->getConnection()::class; + + $this->warn("Could not get table properties for model [{$modelName}] using connection [{$connectionName}]. The underlying database driver [{$driver}] may not be supported."); + + if ($e) { + $this->warn(get_class($e).' '.$e->getMessage()); + } + + $this->modelWarnings[$model::class] ??= []; + + $this->modelWarnings[$model::class][] = ModelWarning::TableInspectionFailed; + } } diff --git a/src/Enums/ModelWarning.php b/src/Enums/ModelWarning.php new file mode 100644 index 000000000..6fa8163bc --- /dev/null +++ b/src/Enums/ModelWarning.php @@ -0,0 +1,18 @@ + str($this->name) + ->headline() + ->lower() + ->ucfirst() + }; + } +}