Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing table inspection to gracefully fail so other IDE helpers for models may still be generated #1528

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion config/ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
|
*/

'filename' => '_ide_helper.php',
'filename' => '_ide_helper.php',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice


/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -155,6 +155,19 @@

],

/*
|--------------------------------------------------------------------------
| Models to silence warnings
|--------------------------------------------------------------------------
|
| Define which models should have warnings silenced
|
clayzar marked this conversation as resolved.
Show resolved Hide resolved
*/

'silenced_models' => [
// App\Models\User::class
],

/*
|--------------------------------------------------------------------------
| Models hooks
Expand Down
67 changes: 65 additions & 2 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
18 changes: 18 additions & 0 deletions src/Enums/ModelWarning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Barryvdh\LaravelIdeHelper\Enums;

enum ModelWarning
{
case TableInspectionFailed;

public function message(): string
{
return match ($this) {
default => str($this->name)
->headline()
->lower()
->ucfirst()
};
}
}