Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ke-healy-9.x
  • Loading branch information
taylorotwell committed Nov 9, 2022
2 parents 7d53fbc + b57e2b0 commit bef6d2a
Showing 1 changed file with 53 additions and 5 deletions.
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

0 comments on commit bef6d2a

Please sign in to comment.