Skip to content

Commit bea07ea

Browse files
Merge pull request #180 from LaravelFreelancerNL/177-support-modelshow
177 support modelshow
2 parents 9991ed1 + af0ada1 commit bea07ea

19 files changed

+534
-59
lines changed

src/AranguentServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use Illuminate\Support\ServiceProvider;
88
use LaravelFreelancerNL\Aranguent\Eloquent\Model;
9+
use LaravelFreelancerNL\Aranguent\Eloquent\ModelInspector;
910
use LaravelFreelancerNL\Aranguent\Schema\Grammar as SchemaGrammar;
11+
use Illuminate\Database\Eloquent\ModelInspector as IlluminateModelInspector;
1012

1113
class AranguentServiceProvider extends ServiceProvider
1214
{
@@ -58,6 +60,10 @@ public function register()
5860
return $app['migrator'];
5961
});
6062

63+
$this->app->extend(IlluminateModelInspector::class, function () {
64+
return new ModelInspector($this->app);
65+
});
66+
6167
$this->app->resolving(
6268
'db',
6369
function ($db) {

src/Console/ShowCommand.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected function tables(ConnectionInterface $connection, $schema)
119119
// Get per table statistics
120120
$tableStats = [];
121121
foreach ($tables as $table) {
122-
$tableStats[] = $schema->getTable($table->name);
122+
$tableStats[] = $schema->getTable($table['name']);
123123
}
124124

125125
return collect($tableStats)->map(fn($table) => [
@@ -148,8 +148,8 @@ protected function views(ConnectionInterface $connection, IlluminateSchemaBuilde
148148

149149
return collect($schema->getViews())
150150
->map(fn($view) => [
151-
'name' => $view->name,
152-
'type' => $view->type,
151+
'name' => $view['name'],
152+
'type' => $view['type'],
153153
]);
154154
}
155155

@@ -163,8 +163,8 @@ protected function analyzers(SchemaBuilder $schema)
163163
{
164164
return collect($schema->getAnalyzers())
165165
->map(fn($analyzer) => [
166-
'name' => $analyzer->name,
167-
'type' => $analyzer->type,
166+
'name' => $analyzer['name'],
167+
'type' => $analyzer['type'],
168168
]);
169169
}
170170

@@ -178,8 +178,8 @@ protected function graphs(SchemaBuilder $schema)
178178
{
179179
return collect($schema->getGraphs())
180180
->map(fn($graph) => [
181-
'name' => $graph->name,
182-
'edgeDefinitions' => count($graph->edgeDefinitions),
181+
'name' => $graph['name'],
182+
'edgeDefinitions' => count($graph['edgeDefinitions']),
183183
]);
184184
}
185185

src/Console/ShowModelCommand.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
}

src/Console/TableCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function handle(ConnectionResolverInterface $connections)
5050
? $schema->getAllTables()
5151
: $schema->getTables(),
5252
)
53-
->keyBy(fn($table) => (string) $table->name)
53+
->keyBy(fn($table) => (string) $table['name'])
5454
->all();
5555

5656
$tableName = (string) $this->argument('table') ?: select(
@@ -178,7 +178,7 @@ protected function displayForCli(array $data)
178178
$columns->each(function ($column) {
179179
$this->components->twoColumnDetail(
180180
$column['name'],
181-
implode(', ', $column['types']),
181+
implode(', ', $column['type']),
182182
);
183183
});
184184
$this->components->info('ArangoDB is schemaless by default. Hence, the column & types are a representation of current data within the table.');

0 commit comments

Comments
 (0)