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
|
4 |
|
public function handle(ModelInspector $modelInspector) |
29
|
|
|
{ |
30
|
|
|
try { |
31
|
4 |
|
$info = $modelInspector->inspect( |
32
|
4 |
|
$this->argument('model'), |
33
|
4 |
|
$this->option('database'), |
34
|
4 |
|
); |
35
|
|
|
} catch (BindingResolutionException $e) { |
36
|
|
|
$this->components->error($e->getMessage()); |
37
|
|
|
|
38
|
|
|
return 1; |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
$this->display( |
42
|
4 |
|
$info['class'], |
43
|
4 |
|
$info['database'], |
44
|
4 |
|
$info['table'], |
45
|
4 |
|
$info['policy'], |
46
|
4 |
|
$info['attributes'], |
47
|
4 |
|
$info['relations'], |
48
|
4 |
|
$info['events'], |
49
|
4 |
|
$info['observers'], |
50
|
4 |
|
); |
51
|
|
|
|
52
|
4 |
|
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
|
4 |
|
protected function display($class, $database, $table, $policy, $attributes, $relations, $events, $observers) |
69
|
|
|
{ |
70
|
4 |
|
$this->option('json') |
71
|
|
|
? $this->displayJson($class, $database, $table, $policy, $attributes, $relations, $events, $observers) |
72
|
4 |
|
: $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
|
4 |
|
protected function displayCli($class, $database, $table, $policy, $attributes, $relations, $events, $observers) |
89
|
|
|
{ |
90
|
4 |
|
$this->newLine(); |
91
|
|
|
|
92
|
4 |
|
$this->components->twoColumnDetail('<fg=green;options=bold>' . $class . '</>'); |
93
|
4 |
|
$this->components->twoColumnDetail('Database', $database); |
94
|
4 |
|
$this->components->twoColumnDetail('Table', $table); |
95
|
|
|
|
96
|
4 |
|
if ($policy) { |
97
|
|
|
$this->components->twoColumnDetail('Policy', $policy); |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
$this->newLine(); |
101
|
|
|
|
102
|
4 |
|
$this->components->twoColumnDetail( |
103
|
4 |
|
'<fg=green;options=bold>Attributes</>', |
104
|
4 |
|
'type <fg=gray>/</> <fg=yellow;options=bold>cast</>', |
105
|
4 |
|
); |
106
|
|
|
|
107
|
4 |
|
foreach ($attributes as $attribute) { |
108
|
4 |
|
$first = trim(sprintf( |
109
|
4 |
|
'%s %s', |
110
|
4 |
|
$attribute['name'], |
111
|
4 |
|
collect(['computed', 'increments', 'unique', 'nullable', 'fillable', 'hidden', 'appended']) |
|
|
|
|
112
|
4 |
|
->filter(fn($property) => $attribute[$property]) |
113
|
4 |
|
->map(fn($property) => sprintf('<fg=gray>%s</>', $property)) |
114
|
4 |
|
->implode('<fg=gray>,</> '), |
115
|
4 |
|
)); |
116
|
|
|
|
117
|
4 |
|
$second = collect([ |
|
|
|
|
118
|
4 |
|
(is_array($attribute['type'])) ? implode(', ', $attribute['type']) : $attribute['type'], |
119
|
4 |
|
$attribute['cast'] ? '<fg=yellow;options=bold>' . $attribute['cast'] . '</>' : null, |
120
|
4 |
|
])->filter()->implode(' <fg=gray>/</> '); |
121
|
|
|
|
122
|
4 |
|
$this->components->twoColumnDetail($first, $second); |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
$this->newLine(); |
126
|
|
|
|
127
|
4 |
|
$this->components->twoColumnDetail('<fg=green;options=bold>Relations</>'); |
128
|
|
|
|
129
|
4 |
|
foreach ($relations as $relation) { |
130
|
3 |
|
$this->components->twoColumnDetail( |
131
|
3 |
|
sprintf('%s <fg=gray>%s</>', $relation['name'], $relation['type']), |
132
|
3 |
|
$relation['related'], |
133
|
3 |
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
4 |
|
$this->newLine(); |
137
|
|
|
|
138
|
4 |
|
$this->displayCliEvents($events, $observers); |
139
|
|
|
|
140
|
4 |
|
$this->newLine(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Collection $events |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
4 |
|
public function displayCliEvents(Collection $events, Collection $observers): void |
148
|
|
|
{ |
149
|
4 |
|
$this->components->twoColumnDetail('<fg=green;options=bold>Events</>'); |
150
|
|
|
|
151
|
4 |
|
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
|
4 |
|
$this->newLine(); |
161
|
|
|
|
162
|
4 |
|
$this->components->twoColumnDetail('<fg=green;options=bold>Observers</>'); |
163
|
|
|
|
164
|
4 |
|
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
|
|
|
} |
176
|
|
|
|