Skip to content

177 support modelshow #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/AranguentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use Illuminate\Support\ServiceProvider;
use LaravelFreelancerNL\Aranguent\Eloquent\Model;
use LaravelFreelancerNL\Aranguent\Eloquent\ModelInspector;
use LaravelFreelancerNL\Aranguent\Schema\Grammar as SchemaGrammar;
use Illuminate\Database\Eloquent\ModelInspector as IlluminateModelInspector;

class AranguentServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -58,6 +60,10 @@ public function register()
return $app['migrator'];
});

$this->app->extend(IlluminateModelInspector::class, function () {
return new ModelInspector($this->app);
});

$this->app->resolving(
'db',
function ($db) {
Expand Down
14 changes: 7 additions & 7 deletions src/Console/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected function tables(ConnectionInterface $connection, $schema)
// Get per table statistics
$tableStats = [];
foreach ($tables as $table) {
$tableStats[] = $schema->getTable($table->name);
$tableStats[] = $schema->getTable($table['name']);
}

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

return collect($schema->getViews())
->map(fn($view) => [
'name' => $view->name,
'type' => $view->type,
'name' => $view['name'],
'type' => $view['type'],
]);
}

Expand All @@ -163,8 +163,8 @@ protected function analyzers(SchemaBuilder $schema)
{
return collect($schema->getAnalyzers())
->map(fn($analyzer) => [
'name' => $analyzer->name,
'type' => $analyzer->type,
'name' => $analyzer['name'],
'type' => $analyzer['type'],
]);
}

Expand All @@ -178,8 +178,8 @@ protected function graphs(SchemaBuilder $schema)
{
return collect($schema->getGraphs())
->map(fn($graph) => [
'name' => $graph->name,
'edgeDefinitions' => count($graph->edgeDefinitions),
'name' => $graph['name'],
'edgeDefinitions' => count($graph['edgeDefinitions']),
]);
}

Expand Down
175 changes: 175 additions & 0 deletions src/Console/ShowModelCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

declare(strict_types=1);

namespace LaravelFreelancerNL\Aranguent\Console;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Console\ShowModelCommand as IlluminateShowModelCommand;
use Illuminate\Database\Eloquent\ModelInspector;
use Illuminate\Support\Collection;

class ShowModelCommand extends IlluminateShowModelCommand
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'model:show {model : The model to show}
{--database= : The database connection to use}
{--json : Output the model as JSON}';

/**
* Execute the console command.
*
* @return int
*/
public function handle(ModelInspector $modelInspector)
{
try {
$info = $modelInspector->inspect(
$this->argument('model'),
$this->option('database'),
);
} catch (BindingResolutionException $e) {
$this->components->error($e->getMessage());

return 1;
}

$this->display(
$info['class'],
$info['database'],
$info['table'],
$info['policy'],
$info['attributes'],
$info['relations'],
$info['events'],
$info['observers'],
);

return 0;
}

/**
* Render the model information.
*
* @param class-string<\Illuminate\Database\Eloquent\Model> $class
* @param string $database
* @param string $table
* @param class-string|null $policy
* @param Collection $attributes
* @param Collection $relations
* @param Collection $events
* @param Collection $observers
* @return void
*/
protected function display($class, $database, $table, $policy, $attributes, $relations, $events, $observers)
{
$this->option('json')
? $this->displayJson($class, $database, $table, $policy, $attributes, $relations, $events, $observers)
: $this->displayCli($class, $database, $table, $policy, $attributes, $relations, $events, $observers);
}

/**
* Render the model information for the CLI.
*
* @param class-string<\Illuminate\Database\Eloquent\Model> $class
* @param string $database
* @param string $table
* @param class-string|null $policy
* @param Collection $attributes
* @param Collection $relations
* @param Collection $events
* @param Collection $observers
* @return void
*/
protected function displayCli($class, $database, $table, $policy, $attributes, $relations, $events, $observers)
{
$this->newLine();

$this->components->twoColumnDetail('<fg=green;options=bold>' . $class . '</>');
$this->components->twoColumnDetail('Database', $database);
$this->components->twoColumnDetail('Table', $table);

if ($policy) {
$this->components->twoColumnDetail('Policy', $policy);
}

$this->newLine();

$this->components->twoColumnDetail(
'<fg=green;options=bold>Attributes</>',
'type <fg=gray>/</> <fg=yellow;options=bold>cast</>',
);

foreach ($attributes as $attribute) {
$first = trim(sprintf(
'%s %s',
$attribute['name'],
collect(['computed', 'increments', 'unique', 'nullable', 'fillable', 'hidden', 'appended'])
->filter(fn($property) => $attribute[$property])
->map(fn($property) => sprintf('<fg=gray>%s</>', $property))
->implode('<fg=gray>,</> '),
));

$second = collect([
(is_array($attribute['type'])) ? implode(', ', $attribute['type']) : $attribute['type'],
$attribute['cast'] ? '<fg=yellow;options=bold>' . $attribute['cast'] . '</>' : null,
])->filter()->implode(' <fg=gray>/</> ');

$this->components->twoColumnDetail($first, $second);
}

$this->newLine();

$this->components->twoColumnDetail('<fg=green;options=bold>Relations</>');

foreach ($relations as $relation) {
$this->components->twoColumnDetail(
sprintf('%s <fg=gray>%s</>', $relation['name'], $relation['type']),
$relation['related'],
);
}

$this->newLine();

$this->displayCliEvents($events, $observers);

$this->newLine();
}

/**
* @param Collection $events
* @return void
*/
public function displayCliEvents(Collection $events, Collection $observers): void
{
$this->components->twoColumnDetail('<fg=green;options=bold>Events</>');

if ($events->count()) {
foreach ($events as $event) {
$this->components->twoColumnDetail(
sprintf('%s', $event['event']),
sprintf('%s', $event['class']),
);
}
}

$this->newLine();

$this->components->twoColumnDetail('<fg=green;options=bold>Observers</>');

if ($observers->count()) {
foreach ($observers as $observer) {
$this->components->twoColumnDetail(
sprintf('%s', $observer['event']),
implode(', ', $observer['observer']),
);
}
}

}

}
4 changes: 2 additions & 2 deletions src/Console/TableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function handle(ConnectionResolverInterface $connections)
? $schema->getAllTables()
: $schema->getTables(),
)
->keyBy(fn($table) => (string) $table->name)
->keyBy(fn($table) => (string) $table['name'])
->all();

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