|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace LaravelFreelancerNL\Aranguent\Console; |
| 6 | + |
| 7 | +use Illuminate\Contracts\Container\BindingResolutionException; |
| 8 | +use Illuminate\Database\Console\ShowModelCommand as IlluminateShowModelCommand; |
| 9 | +use Illuminate\Database\Eloquent\ModelInspector; |
| 10 | +use Illuminate\Support\Collection; |
| 11 | + |
| 12 | +class ShowModelCommand extends IlluminateShowModelCommand |
| 13 | +{ |
| 14 | + /** |
| 15 | + * The console command signature. |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $signature = 'model:show {model : The model to show} |
| 20 | + {--database= : The database connection to use} |
| 21 | + {--json : Output the model as JSON}'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Execute the console command. |
| 25 | + * |
| 26 | + * @return int |
| 27 | + */ |
| 28 | + public function handle(ModelInspector $modelInspector) |
| 29 | + { |
| 30 | + try { |
| 31 | + $info = $modelInspector->inspect( |
| 32 | + $this->argument('model'), |
| 33 | + $this->option('database'), |
| 34 | + ); |
| 35 | + } catch (BindingResolutionException $e) { |
| 36 | + $this->components->error($e->getMessage()); |
| 37 | + |
| 38 | + return 1; |
| 39 | + } |
| 40 | + |
| 41 | + $this->display( |
| 42 | + $info['class'], |
| 43 | + $info['database'], |
| 44 | + $info['table'], |
| 45 | + $info['policy'], |
| 46 | + $info['attributes'], |
| 47 | + $info['relations'], |
| 48 | + $info['events'], |
| 49 | + $info['observers'], |
| 50 | + ); |
| 51 | + |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Render the model information. |
| 57 | + * |
| 58 | + * @param class-string<\Illuminate\Database\Eloquent\Model> $class |
| 59 | + * @param string $database |
| 60 | + * @param string $table |
| 61 | + * @param class-string|null $policy |
| 62 | + * @param Collection $attributes |
| 63 | + * @param Collection $relations |
| 64 | + * @param Collection $events |
| 65 | + * @param Collection $observers |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + protected function display($class, $database, $table, $policy, $attributes, $relations, $events, $observers) |
| 69 | + { |
| 70 | + $this->option('json') |
| 71 | + ? $this->displayJson($class, $database, $table, $policy, $attributes, $relations, $events, $observers) |
| 72 | + : $this->displayCli($class, $database, $table, $policy, $attributes, $relations, $events, $observers); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Render the model information for the CLI. |
| 77 | + * |
| 78 | + * @param class-string<\Illuminate\Database\Eloquent\Model> $class |
| 79 | + * @param string $database |
| 80 | + * @param string $table |
| 81 | + * @param class-string|null $policy |
| 82 | + * @param Collection $attributes |
| 83 | + * @param Collection $relations |
| 84 | + * @param Collection $events |
| 85 | + * @param Collection $observers |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + protected function displayCli($class, $database, $table, $policy, $attributes, $relations, $events, $observers) |
| 89 | + { |
| 90 | + $this->newLine(); |
| 91 | + |
| 92 | + $this->components->twoColumnDetail('<fg=green;options=bold>' . $class . '</>'); |
| 93 | + $this->components->twoColumnDetail('Database', $database); |
| 94 | + $this->components->twoColumnDetail('Table', $table); |
| 95 | + |
| 96 | + if ($policy) { |
| 97 | + $this->components->twoColumnDetail('Policy', $policy); |
| 98 | + } |
| 99 | + |
| 100 | + $this->newLine(); |
| 101 | + |
| 102 | + $this->components->twoColumnDetail( |
| 103 | + '<fg=green;options=bold>Attributes</>', |
| 104 | + 'type <fg=gray>/</> <fg=yellow;options=bold>cast</>', |
| 105 | + ); |
| 106 | + |
| 107 | + foreach ($attributes as $attribute) { |
| 108 | + $first = trim(sprintf( |
| 109 | + '%s %s', |
| 110 | + $attribute['name'], |
| 111 | + collect(['computed', 'increments', 'unique', 'nullable', 'fillable', 'hidden', 'appended']) |
| 112 | + ->filter(fn($property) => $attribute[$property]) |
| 113 | + ->map(fn($property) => sprintf('<fg=gray>%s</>', $property)) |
| 114 | + ->implode('<fg=gray>,</> '), |
| 115 | + )); |
| 116 | + |
| 117 | + $second = collect([ |
| 118 | + (is_array($attribute['type'])) ? implode(', ', $attribute['type']) : $attribute['type'], |
| 119 | + $attribute['cast'] ? '<fg=yellow;options=bold>' . $attribute['cast'] . '</>' : null, |
| 120 | + ])->filter()->implode(' <fg=gray>/</> '); |
| 121 | + |
| 122 | + $this->components->twoColumnDetail($first, $second); |
| 123 | + } |
| 124 | + |
| 125 | + $this->newLine(); |
| 126 | + |
| 127 | + $this->components->twoColumnDetail('<fg=green;options=bold>Relations</>'); |
| 128 | + |
| 129 | + foreach ($relations as $relation) { |
| 130 | + $this->components->twoColumnDetail( |
| 131 | + sprintf('%s <fg=gray>%s</>', $relation['name'], $relation['type']), |
| 132 | + $relation['related'], |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + $this->newLine(); |
| 137 | + |
| 138 | + $this->displayCliEvents($events, $observers); |
| 139 | + |
| 140 | + $this->newLine(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @param Collection $events |
| 145 | + * @return void |
| 146 | + */ |
| 147 | + public function displayCliEvents(Collection $events, Collection $observers): void |
| 148 | + { |
| 149 | + $this->components->twoColumnDetail('<fg=green;options=bold>Events</>'); |
| 150 | + |
| 151 | + if ($events->count()) { |
| 152 | + foreach ($events as $event) { |
| 153 | + $this->components->twoColumnDetail( |
| 154 | + sprintf('%s', $event['event']), |
| 155 | + sprintf('%s', $event['class']), |
| 156 | + ); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + $this->newLine(); |
| 161 | + |
| 162 | + $this->components->twoColumnDetail('<fg=green;options=bold>Observers</>'); |
| 163 | + |
| 164 | + if ($observers->count()) { |
| 165 | + foreach ($observers as $observer) { |
| 166 | + $this->components->twoColumnDetail( |
| 167 | + sprintf('%s', $observer['event']), |
| 168 | + implode(', ', $observer['observer']), |
| 169 | + ); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments