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'); - } -} diff --git a/src/Illuminate/Foundation/Console/ShowModelCommand.php b/src/Illuminate/Foundation/Console/ShowModelCommand.php index 2cd9316b4323..5e09950aa2f4 100644 --- a/src/Illuminate/Foundation/Console/ShowModelCommand.php +++ b/src/Illuminate/Foundation/Console/ShowModelCommand.php @@ -3,18 +3,14 @@ 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; use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Support\Collection; use Illuminate\Support\Str; use ReflectionClass; 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 +77,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,7 +119,6 @@ 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, @@ -151,9 +145,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 []; } @@ -163,7 +157,6 @@ 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), @@ -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->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->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 $attribute) { + $first = sprintf('%s %s', $attribute['name'], collect(['nullable', 'fillable', 'hidden', 'appended']) + ->filter(fn ($property) => $attribute[$property]) + ->map(fn ($property) => sprintf('%s', $property)) + ->implode(', ')); + + $second = collect([ + $attribute['type'], + $attribute['cast'] ? ''.$attribute['cast'].'' : null, + ])->filter()->implode(' / '); + + $this->components->twoColumnDetail( + str($first)->trim(), $second, + ); + } + + $this->newLine(); + + $this->components->twoColumnDetail('Relations'); + + foreach ($relations as $relation) { + $this->components->twoColumnDetail( + $relation['name'].' '.$relation['type'].'', + $relation['related'] + ); + } + + $this->newLine(); } /** @@ -322,11 +305,11 @@ protected function formatForCli($value) 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; @@ -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. *