Skip to content
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

Include Eloquent Model Observers in model:show command #44884

Merged
merged 14 commits into from
Nov 9, 2022
58 changes: 53 additions & 5 deletions src/Illuminate/Foundation/Console/ShowModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function handle()
$model->getConnection()->getTablePrefix().$model->getTable(),
$this->getAttributes($model),
$this->getRelations($model),
$this->getObservers($model),
);
}

Expand Down Expand Up @@ -223,6 +224,38 @@ protected function getRelations($model)
->values();
}

/**
* Get the Observers watching this model.
*
* @return Illuminate\Support\Collection
*/
protected function getObservers($model)
{
$listeners = $this->getLaravel()->make('events')->getRawListeners();

// Distill down to Eloquent observers relevant to this model.
$listeners = array_filter($listeners, function ($v, $key) use ($model) {
return Str::startsWith($key, 'eloquent.') && Str::endsWith($key, $model::class);
}, ARRAY_FILTER_USE_BOTH);

// Format as Eloquent verb => Observer methods
$extractVerb = function ($key) {
preg_match('/eloquent.([a-zA-Z]+)\: /', $key, $matches);
return $matches[1] ?? '?';
};

$formatted = [];

foreach ($listeners as $key => $observerMethods) {
$formatted[] = [
'event' => $extractVerb($key),
'observer' => array_map(fn ($obs) => is_string($obs) ? $obs : 'Closure', $observerMethods),
];
}

return collect($formatted);
}

/**
* Render the model information.
*
Expand All @@ -231,13 +264,14 @@ protected function getRelations($model)
* @param string $table
* @param \Illuminate\Support\Collection $attributes
* @param \Illuminate\Support\Collection $relations
* @param \Illuminate\Support\Collection $observers
* @return void
*/
protected function display($class, $database, $table, $attributes, $relations)
protected function display($class, $database, $table, $attributes, $relations, $observers)
{
$this->option('json')
? $this->displayJson($class, $database, $table, $attributes, $relations)
: $this->displayCli($class, $database, $table, $attributes, $relations);
? $this->displayJson($class, $database, $table, $attributes, $relations, $observers)
: $this->displayCli($class, $database, $table, $attributes, $relations, $observers);
}

/**
Expand All @@ -248,9 +282,10 @@ protected function display($class, $database, $table, $attributes, $relations)
* @param string $table
* @param \Illuminate\Support\Collection $attributes
* @param \Illuminate\Support\Collection $relations
* @param \Illuminate\Support\Collection $observers
* @return void
*/
protected function displayJson($class, $database, $table, $attributes, $relations)
protected function displayJson($class, $database, $table, $attributes, $relations, $observers)
{
$this->output->writeln(
collect([
Expand All @@ -259,6 +294,7 @@ protected function displayJson($class, $database, $table, $attributes, $relation
'table' => $table,
'attributes' => $attributes,
'relations' => $relations,
'observers' => $observers,
])->toJson()
);
}
Expand All @@ -271,9 +307,10 @@ protected function displayJson($class, $database, $table, $attributes, $relation
* @param string $table
* @param \Illuminate\Support\Collection $attributes
* @param \Illuminate\Support\Collection $relations
* @param \Illuminate\Support\Collection $observers
* @return void
*/
protected function displayCli($class, $database, $table, $attributes, $relations)
protected function displayCli($class, $database, $table, $attributes, $relations, $observers)
{
$this->newLine();

Expand Down Expand Up @@ -325,6 +362,17 @@ protected function displayCli($class, $database, $table, $attributes, $relations
}

$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']));
}
}

$this->newLine();
}

/**
Expand Down