From bdec5ef1710d297e53dd2dcd22349ad338e9c58c Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 13 Jul 2022 12:41:24 +0100 Subject: [PATCH 1/9] Updates layout of `model:show` --- .../Foundation/Console/ShowModelCommand.php | 110 ++++++------------ 1 file changed, 34 insertions(+), 76 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ShowModelCommand.php b/src/Illuminate/Foundation/Console/ShowModelCommand.php index 2cd9316b4323..2612e44b9842 100644 --- a/src/Illuminate/Foundation/Console/ShowModelCommand.php +++ b/src/Illuminate/Foundation/Console/ShowModelCommand.php @@ -3,7 +3,6 @@ namespace Illuminate\Foundation\Console; use Doctrine\DBAL\Schema\Column; -use Doctrine\DBAL\Types\DecimalType; use Illuminate\Console\Command; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Database\Eloquent\Model; @@ -14,7 +13,6 @@ use ReflectionMethod; use SplFileObject; use Symfony\Component\Console\Attribute\AsCommand; -use Symfony\Component\Console\Helper\TableCell; #[AsCommand(name: 'model:show')] class ShowModelCommand extends Command @@ -81,14 +79,13 @@ public function handle() { if (! interface_exists('Doctrine\DBAL\Driver')) { return $this->components->error( - 'To display model information, you must install the `doctrine/dbal` package using the Composer package manager.' + 'Displaying model information requires [doctrine/dbal].' ); } $class = $this->qualifyModel($this->argument('model')); try { - /** @var Model */ $model = $this->laravel->make($class); } catch (BindingResolutionException $e) { return $this->components->error($e->getMessage()); @@ -124,10 +121,8 @@ protected function getAttributes($model) 'name' => $column->getName(), 'type' => $this->getColumnType($column), 'nullable' => ! $column->getNotnull(), - 'default' => $this->getColumnDefault($column, $model), 'fillable' => $model->isFillable($column->getName()), 'hidden' => $this->attributeIsHidden($column->getName(), $model), - 'appended' => null, 'cast' => $this->getCastType($column->getName(), $model), ]) ->merge($this->getVirtualAttributes($model, $columns)); @@ -163,10 +158,8 @@ protected function getVirtualAttributes($model, $columns) 'name' => $name, 'type' => null, 'nullable' => null, - 'default' => null, 'fillable' => $model->isFillable($name), 'hidden' => $this->attributeIsHidden($name, $model), - 'appended' => $model->hasAppended($name), 'cast' => $cast, ]) ->values(); @@ -259,57 +252,47 @@ protected function displayJson($class, $database, $table, $attributes, $relation */ protected function displayCli($class, $database, $table, $attributes, $relations) { + $this->newLine(); + $this->components->twoColumnDetail(''.$class.''); $this->components->twoColumnDetail('Database', $database); $this->components->twoColumnDetail('Table', $table); $this->newLine(); + $this->components->twoColumnDetail('Relations'); + + foreach ($relations as $relation) { + $this->components->twoColumnDetail( + ucfirst($relation['name']).' '.$relation['type'].'', + $relation['related'] + ); + } - $this->components->table( - [ - 'Attribute', - 'Type', - 'Nullable', - 'Default', - 'Fillable', - 'Hidden', - 'Appended', - 'Cast', - ], - [ - ...$attributes->map(fn ($attribute) => collect($attribute) - ->map(fn ($property) => $this->formatForCli($property)) - ->all() - ), - [''], - [ - 'Relation', - 'Type', - new TableCell('Related', ['colspan' => 6]), - ], - ...$relations->map(fn ($relation) => [ - $relation['name'], - $relation['type'], - new TableCell($relation['related'], ['colspan' => 6]), - ]), - ] + $this->newLine(); + + $this->components->twoColumnDetail( + 'Attributes', + 'type / cast', ); - } - /** - * Format a value for the CLI. - * - * @param bool|null|string $value - * @return string - */ - protected function formatForCli($value) - { - return match ($value) { - true => 'Yes', - false => 'No', - null => '-', - default => $value, - }; + foreach ($attributes as $key => $attribute) { + $first = sprintf('%s %s', $attribute['name'], collect(['nullable', 'fillable', 'hidden']) + ->filter(fn ($property) => $attribute[$property]) + ->map(fn ($property) => sprintf('%s', $property)) + ->implode(', ')); + + $second = $attribute['type']; + + if ($attribute['cast']) { + $second = ''.$attribute['cast'].''; + } + + $this->components->twoColumnDetail( + str($first)->trim(), str($second)->when(! class_exists($attribute['cast']))->lower(), + ); + } + + $this->newLine(); } /** @@ -356,36 +339,11 @@ protected function getColumnType($column) { $name = $column->getType()->getName(); - $details = match (get_class($column->getType())) { - DecimalType::class => $column->getPrecision().','.$column->getScale(), - default => $column->getLength(), - }; - $unsigned = $column->getUnsigned() ? ' unsigned' : ''; - if ($details) { - return sprintf('%s(%s)%s', $name, $details, $unsigned); - } - return sprintf('%s%s', $name, $unsigned); } - /** - * Get the default value for the given column. - * - * @param \Doctrine\DBAL\Schema\Column $column - * @param \Illuminate\Database\Eloquent\Model $model - * @return string|null - */ - protected function getColumnDefault($column, $model) - { - if ($column->getAutoincrement()) { - return 'increments'; - } - - return $model->getAttributes()[$column->getName()] ?? $column->getDefault(); - } - /** * Determine if the given attribute is hidden. * From a562d3870a47ae0e71391ef25f6a71aa98671e4b Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 13 Jul 2022 13:10:06 +0100 Subject: [PATCH 2/9] Removes upper case on relation --- src/Illuminate/Foundation/Console/ShowModelCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Console/ShowModelCommand.php b/src/Illuminate/Foundation/Console/ShowModelCommand.php index 2612e44b9842..9176adc6baae 100644 --- a/src/Illuminate/Foundation/Console/ShowModelCommand.php +++ b/src/Illuminate/Foundation/Console/ShowModelCommand.php @@ -263,7 +263,7 @@ protected function displayCli($class, $database, $table, $attributes, $relations foreach ($relations as $relation) { $this->components->twoColumnDetail( - ucfirst($relation['name']).' '.$relation['type'].'', + $relation['name'].' '.$relation['type'].'', $relation['related'] ); } From ff28e6a204143310f5d08b76bfe95e5651aecf74 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 13 Jul 2022 23:20:52 +1000 Subject: [PATCH 3/9] Show appended status for virtual attributes --- src/Illuminate/Foundation/Console/ShowModelCommand.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Console/ShowModelCommand.php b/src/Illuminate/Foundation/Console/ShowModelCommand.php index 9176adc6baae..82cc2a2fb18a 100644 --- a/src/Illuminate/Foundation/Console/ShowModelCommand.php +++ b/src/Illuminate/Foundation/Console/ShowModelCommand.php @@ -123,6 +123,7 @@ protected function getAttributes($model) 'nullable' => ! $column->getNotnull(), 'fillable' => $model->isFillable($column->getName()), 'hidden' => $this->attributeIsHidden($column->getName(), $model), + 'appended' => null, 'cast' => $this->getCastType($column->getName(), $model), ]) ->merge($this->getVirtualAttributes($model, $columns)); @@ -160,6 +161,7 @@ protected function getVirtualAttributes($model, $columns) 'nullable' => null, 'fillable' => $model->isFillable($name), 'hidden' => $this->attributeIsHidden($name, $model), + 'appended' => $model->hasAppended($name), 'cast' => $cast, ]) ->values(); @@ -276,7 +278,7 @@ protected function displayCli($class, $database, $table, $attributes, $relations ); foreach ($attributes as $key => $attribute) { - $first = sprintf('%s %s', $attribute['name'], collect(['nullable', 'fillable', 'hidden']) + $first = sprintf('%s %s', $attribute['name'], collect(['nullable', 'fillable', 'hidden', 'appended']) ->filter(fn ($property) => $attribute[$property]) ->map(fn ($property) => sprintf('%s', $property)) ->implode(', ')); From 42e83f712750b7b4e4ff560d4acc6b158a4dbc86 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 13 Jul 2022 23:21:26 +1000 Subject: [PATCH 4/9] Lower-case accessors and attributes --- src/Illuminate/Foundation/Console/ShowModelCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ShowModelCommand.php b/src/Illuminate/Foundation/Console/ShowModelCommand.php index 82cc2a2fb18a..e0602ef6286b 100644 --- a/src/Illuminate/Foundation/Console/ShowModelCommand.php +++ b/src/Illuminate/Foundation/Console/ShowModelCommand.php @@ -147,9 +147,9 @@ protected function getVirtualAttributes($model, $columns) ) ->mapWithKeys(function (ReflectionMethod $method) use ($model) { if (preg_match('/^get(.*)Attribute$/', $method->getName(), $matches) === 1) { - return [Str::snake($matches[1]) => 'Accessor']; + return [Str::snake($matches[1]) => 'accessor']; } elseif ($model->hasAttributeMutator($method->getName())) { - return [Str::snake($method->getName()) => 'Attribute']; + return [Str::snake($method->getName()) => 'attribute']; } else { return []; } @@ -307,11 +307,11 @@ protected function displayCli($class, $database, $table, $attributes, $relations protected function getCastType($column, $model) { if ($model->hasGetMutator($column) || $model->hasSetMutator($column)) { - return 'Accessor'; + return 'accessor'; } if ($model->hasAttributeMutator($column)) { - return 'Attribute'; + return 'attribute'; } return $this->getCastsWithDates($model)->get($column) ?? null; From 27ed0a03e638749975beb2912b42576dd5e8f4bd Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 13 Jul 2022 23:22:38 +1000 Subject: [PATCH 5/9] Display type and cast --- .../Foundation/Console/ShowModelCommand.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ShowModelCommand.php b/src/Illuminate/Foundation/Console/ShowModelCommand.php index e0602ef6286b..2f43ae737fe2 100644 --- a/src/Illuminate/Foundation/Console/ShowModelCommand.php +++ b/src/Illuminate/Foundation/Console/ShowModelCommand.php @@ -283,14 +283,13 @@ protected function displayCli($class, $database, $table, $attributes, $relations ->map(fn ($property) => sprintf('%s', $property)) ->implode(', ')); - $second = $attribute['type']; - - if ($attribute['cast']) { - $second = ''.$attribute['cast'].''; - } + $second = collect([ + $attribute['type'], + $attribute['cast'] ? ''.$attribute['cast'].'' : null, + ])->filter()->implode(' / '); $this->components->twoColumnDetail( - str($first)->trim(), str($second)->when(! class_exists($attribute['cast']))->lower(), + str($first)->trim(), $second, ); } From 15fc044240774b1206fcca6f8e91085b16b35d25 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 13 Jul 2022 23:23:15 +1000 Subject: [PATCH 6/9] Move relations to bottom --- .../Foundation/Console/ShowModelCommand.php | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ShowModelCommand.php b/src/Illuminate/Foundation/Console/ShowModelCommand.php index 2f43ae737fe2..755abd5a7956 100644 --- a/src/Illuminate/Foundation/Console/ShowModelCommand.php +++ b/src/Illuminate/Foundation/Console/ShowModelCommand.php @@ -260,16 +260,6 @@ protected function displayCli($class, $database, $table, $attributes, $relations $this->components->twoColumnDetail('Database', $database); $this->components->twoColumnDetail('Table', $table); - $this->newLine(); - $this->components->twoColumnDetail('Relations'); - - foreach ($relations as $relation) { - $this->components->twoColumnDetail( - $relation['name'].' '.$relation['type'].'', - $relation['related'] - ); - } - $this->newLine(); $this->components->twoColumnDetail( @@ -294,6 +284,17 @@ protected function displayCli($class, $database, $table, $attributes, $relations } $this->newLine(); + + $this->components->twoColumnDetail('Relations'); + + foreach ($relations as $relation) { + $this->components->twoColumnDetail( + $relation['name'].' '.$relation['type'].'', + $relation['related'] + ); + } + + $this->newLine(); } /** From 4b70f6c4d9933988704acfde70c5aafb481ea326 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 13 Jul 2022 23:23:25 +1000 Subject: [PATCH 7/9] Remove unused var --- src/Illuminate/Foundation/Console/ShowModelCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Console/ShowModelCommand.php b/src/Illuminate/Foundation/Console/ShowModelCommand.php index 755abd5a7956..61e24e87ffbe 100644 --- a/src/Illuminate/Foundation/Console/ShowModelCommand.php +++ b/src/Illuminate/Foundation/Console/ShowModelCommand.php @@ -267,7 +267,7 @@ protected function displayCli($class, $database, $table, $attributes, $relations 'type / cast', ); - foreach ($attributes as $key => $attribute) { + foreach ($attributes as $attribute) { $first = sprintf('%s %s', $attribute['name'], collect(['nullable', 'fillable', 'hidden', 'appended']) ->filter(fn ($property) => $attribute[$property]) ->map(fn ($property) => sprintf('%s', $property)) From f4f619c807cd9672c5dedcf0bdd2d9eb34a45faf Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 13 Jul 2022 23:39:09 +1000 Subject: [PATCH 8/9] Remove unused table component --- .../Console/View/Components/Table.php | 44 ------------------- 1 file changed, 44 deletions(-) delete mode 100644 src/Illuminate/Console/View/Components/Table.php diff --git a/src/Illuminate/Console/View/Components/Table.php b/src/Illuminate/Console/View/Components/Table.php deleted file mode 100644 index 84aa6ba5fc4e..000000000000 --- a/src/Illuminate/Console/View/Components/Table.php +++ /dev/null @@ -1,44 +0,0 @@ -toArray(); - } - - (new SymfonyTable($this->output)) - ->setStyle($this->tableStyle()) - ->setHeaders((array) $headers) - ->setRows($rows) - ->render(); - } - - /** - * Return a custom table style for Laravel. - * - * @return \Symfony\Component\Console\Helper\TableStyle - */ - protected function tableStyle() - { - return (new TableStyle()) - ->setHorizontalBorderChars('') - ->setVerticalBorderChars(' ') - ->setDefaultCrossingChar('') - ->setCellHeaderFormat('%s'); - } -} From 0a2dd4ac1f9be823abc0777607f0765c48dd8932 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 13 Jul 2022 23:41:45 +1000 Subject: [PATCH 9/9] Linting --- src/Illuminate/Foundation/Console/ShowModelCommand.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ShowModelCommand.php b/src/Illuminate/Foundation/Console/ShowModelCommand.php index 61e24e87ffbe..5e09950aa2f4 100644 --- a/src/Illuminate/Foundation/Console/ShowModelCommand.php +++ b/src/Illuminate/Foundation/Console/ShowModelCommand.php @@ -5,9 +5,7 @@ use Doctrine\DBAL\Schema\Column; use Illuminate\Console\Command; use Illuminate\Contracts\Container\BindingResolutionException; -use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Support\Collection; use Illuminate\Support\Str; use ReflectionClass; use ReflectionMethod;