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

[11.x] Add the events to be displayed on the model:show command #51324

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
47 changes: 42 additions & 5 deletions src/Illuminate/Database/Console/ShowModelCommand.php
Original file line number Diff line number Diff line change
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,25 @@ 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)
{
$formatted = [];
foreach ($model->dispatchesEvents() as $event => $class) {
$formatted[] = [
'event' => $event,
'class' => $class,
];
}

return collect($formatted);
WendellAdriel marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Get the Observers watching this model.
*
Expand Down Expand Up @@ -268,14 +288,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 +308,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 +322,7 @@ protected function displayJson($class, $database, $table, $policy, $attributes,
'policy' => $policy,
'attributes' => $attributes,
'relations' => $relations,
'events' => $events,
'observers' => $observers,
])->toJson()
);
Expand All @@ -314,10 +337,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 +398,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
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ public function removeObservableEvents($observables)
);
}

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

/**
* Register a model event with the dispatcher.
*
Expand Down