Skip to content

Commit

Permalink
[11.x] Add the events to be displayed on the model:show command (#51324)
Browse files Browse the repository at this point in the history
* [11.x] Add the events to be displayed on the model:show command

* Code style issue

* Code style issue

* Removed not needed local variable

* Code style issue

* Update HasEvents.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
WendellAdriel and taylorotwell committed May 8, 2024
1 parent 192ec66 commit 8fde0ea
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/Illuminate/Database/Console/ShowModelCommand.php
Expand Up @@ -90,6 +90,7 @@ public function handle()
$this->getPolicy($model),
$this->getAttributes($model),
$this->getRelations($model),
$this->getEvents($model),
$this->getObservers($model),
);

Expand Down Expand Up @@ -225,6 +226,21 @@ protected function getRelations($model)
->values();
}

/**
* Get the Events that the model dispatches.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Support\Collection
*/
protected function getEvents($model)
{
return collect($model->dispatchesEvents())
->map(fn (string $class, string $event) => [
'event' => $event,
'class' => $class,
])->values();
}

/**
* Get the Observers watching this model.
*
Expand Down Expand Up @@ -268,14 +284,15 @@ protected function getObservers($model)
* @param string $policy
* @param \Illuminate\Support\Collection $attributes
* @param \Illuminate\Support\Collection $relations
* @param \Illuminate\Support\Collection $events
* @param \Illuminate\Support\Collection $observers
* @return void
*/
protected function display($class, $database, $table, $policy, $attributes, $relations, $observers)
protected function display($class, $database, $table, $policy, $attributes, $relations, $events, $observers)
{
$this->option('json')
? $this->displayJson($class, $database, $table, $policy, $attributes, $relations, $observers)
: $this->displayCli($class, $database, $table, $policy, $attributes, $relations, $observers);
? $this->displayJson($class, $database, $table, $policy, $attributes, $relations, $events, $observers)
: $this->displayCli($class, $database, $table, $policy, $attributes, $relations, $events, $observers);
}

/**
Expand All @@ -287,10 +304,11 @@ protected function display($class, $database, $table, $policy, $attributes, $rel
* @param string $policy
* @param \Illuminate\Support\Collection $attributes
* @param \Illuminate\Support\Collection $relations
* @param \Illuminate\Support\Collection $events
* @param \Illuminate\Support\Collection $observers
* @return void
*/
protected function displayJson($class, $database, $table, $policy, $attributes, $relations, $observers)
protected function displayJson($class, $database, $table, $policy, $attributes, $relations, $events, $observers)
{
$this->output->writeln(
collect([
Expand All @@ -300,6 +318,7 @@ protected function displayJson($class, $database, $table, $policy, $attributes,
'policy' => $policy,
'attributes' => $attributes,
'relations' => $relations,
'events' => $events,
'observers' => $observers,
])->toJson()
);
Expand All @@ -314,10 +333,11 @@ protected function displayJson($class, $database, $table, $policy, $attributes,
* @param string $policy
* @param \Illuminate\Support\Collection $attributes
* @param \Illuminate\Support\Collection $relations
* @param \Illuminate\Support\Collection $events
* @param \Illuminate\Support\Collection $observers
* @return void
*/
protected function displayCli($class, $database, $table, $policy, $attributes, $relations, $observers)
protected function displayCli($class, $database, $table, $policy, $attributes, $relations, $events, $observers)
{
$this->newLine();

Expand Down Expand Up @@ -374,6 +394,19 @@ protected function displayCli($class, $database, $table, $policy, $attributes, $

$this->newLine();

$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()) {
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasEvents.php
Expand Up @@ -386,6 +386,16 @@ public static function flushEventListeners()
}
}

/**
* Get the event map for the model.
*
* @return array
*/
public function dispatchesEvents()
{
return $this->dispatchesEvents;
}

/**
* Get the event dispatcher instance.
*
Expand Down

0 comments on commit 8fde0ea

Please sign in to comment.