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