From 8fde0eafc95d7d166594a248585f532755cab031 Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Wed, 8 May 2024 19:34:06 +0100 Subject: [PATCH] [11.x] Add the events to be displayed on the model:show command (#51324) * [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 --- .../Database/Console/ShowModelCommand.php | 43 ++++++++++++++++--- .../Database/Eloquent/Concerns/HasEvents.php | 10 +++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Database/Console/ShowModelCommand.php b/src/Illuminate/Database/Console/ShowModelCommand.php index 4ab262546c15..4ac2ab09f593 100644 --- a/src/Illuminate/Database/Console/ShowModelCommand.php +++ b/src/Illuminate/Database/Console/ShowModelCommand.php @@ -90,6 +90,7 @@ public function handle() $this->getPolicy($model), $this->getAttributes($model), $this->getRelations($model), + $this->getEvents($model), $this->getObservers($model), ); @@ -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. * @@ -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); } /** @@ -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([ @@ -300,6 +318,7 @@ protected function displayJson($class, $database, $table, $policy, $attributes, 'policy' => $policy, 'attributes' => $attributes, 'relations' => $relations, + 'events' => $events, 'observers' => $observers, ])->toJson() ); @@ -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(); @@ -374,6 +394,19 @@ protected function displayCli($class, $database, $table, $policy, $attributes, $ $this->newLine(); + $this->components->twoColumnDetail('Events'); + + if ($events->count()) { + foreach ($events as $event) { + $this->components->twoColumnDetail( + sprintf('%s', $event['event']), + sprintf('%s', $event['class']), + ); + } + } + + $this->newLine(); + $this->components->twoColumnDetail('Observers'); if ($observers->count()) { diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php b/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php index 0730dcb10971..9bd0615c4734 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php @@ -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. *