Skip to content

Commit bef6d2a

Browse files
committed
Merge branch '9.x' of https://github.com/mike-healy/framework into mike-healy-9.x
2 parents 7d53fbc + b57e2b0 commit bef6d2a

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

src/Illuminate/Foundation/Console/ShowModelCommand.php

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public function handle()
103103
$model->getConnection()->getTablePrefix().$model->getTable(),
104104
$this->getAttributes($model),
105105
$this->getRelations($model),
106+
$this->getObservers($model),
106107
);
107108
}
108109

@@ -223,6 +224,38 @@ protected function getRelations($model)
223224
->values();
224225
}
225226

227+
/**
228+
* Get the Observers watching this model.
229+
*
230+
* @return Illuminate\Support\Collection
231+
*/
232+
protected function getObservers($model)
233+
{
234+
$listeners = $this->getLaravel()->make('events')->getRawListeners();
235+
236+
// Distill down to Eloquent observers relevant to this model.
237+
$listeners = array_filter($listeners, function ($v, $key) use ($model) {
238+
return Str::startsWith($key, 'eloquent.') && Str::endsWith($key, $model::class);
239+
}, ARRAY_FILTER_USE_BOTH);
240+
241+
// Format as Eloquent verb => Observer methods
242+
$extractVerb = function ($key) {
243+
preg_match('/eloquent.([a-zA-Z]+)\: /', $key, $matches);
244+
return $matches[1] ?? '?';
245+
};
246+
247+
$formatted = [];
248+
249+
foreach ($listeners as $key => $observerMethods) {
250+
$formatted[] = [
251+
'event' => $extractVerb($key),
252+
'observer' => array_map(fn ($obs) => is_string($obs) ? $obs : 'Closure', $observerMethods),
253+
];
254+
}
255+
256+
return collect($formatted);
257+
}
258+
226259
/**
227260
* Render the model information.
228261
*
@@ -231,13 +264,14 @@ protected function getRelations($model)
231264
* @param string $table
232265
* @param \Illuminate\Support\Collection $attributes
233266
* @param \Illuminate\Support\Collection $relations
267+
* @param \Illuminate\Support\Collection $observers
234268
* @return void
235269
*/
236-
protected function display($class, $database, $table, $attributes, $relations)
270+
protected function display($class, $database, $table, $attributes, $relations, $observers)
237271
{
238272
$this->option('json')
239-
? $this->displayJson($class, $database, $table, $attributes, $relations)
240-
: $this->displayCli($class, $database, $table, $attributes, $relations);
273+
? $this->displayJson($class, $database, $table, $attributes, $relations, $observers)
274+
: $this->displayCli($class, $database, $table, $attributes, $relations, $observers);
241275
}
242276

243277
/**
@@ -248,9 +282,10 @@ protected function display($class, $database, $table, $attributes, $relations)
248282
* @param string $table
249283
* @param \Illuminate\Support\Collection $attributes
250284
* @param \Illuminate\Support\Collection $relations
285+
* @param \Illuminate\Support\Collection $observers
251286
* @return void
252287
*/
253-
protected function displayJson($class, $database, $table, $attributes, $relations)
288+
protected function displayJson($class, $database, $table, $attributes, $relations, $observers)
254289
{
255290
$this->output->writeln(
256291
collect([
@@ -259,6 +294,7 @@ protected function displayJson($class, $database, $table, $attributes, $relation
259294
'table' => $table,
260295
'attributes' => $attributes,
261296
'relations' => $relations,
297+
'observers' => $observers,
262298
])->toJson()
263299
);
264300
}
@@ -271,9 +307,10 @@ protected function displayJson($class, $database, $table, $attributes, $relation
271307
* @param string $table
272308
* @param \Illuminate\Support\Collection $attributes
273309
* @param \Illuminate\Support\Collection $relations
310+
* @param \Illuminate\Support\Collection $observers
274311
* @return void
275312
*/
276-
protected function displayCli($class, $database, $table, $attributes, $relations)
313+
protected function displayCli($class, $database, $table, $attributes, $relations, $observers)
277314
{
278315
$this->newLine();
279316

@@ -325,6 +362,17 @@ protected function displayCli($class, $database, $table, $attributes, $relations
325362
}
326363

327364
$this->newLine();
365+
366+
$this->components->twoColumnDetail('<fg=green;options=bold>Observers</>');
367+
368+
if ($observers->count()) {
369+
foreach ($observers as $observer) {
370+
$this->components->twoColumnDetail(
371+
sprintf('%s', $observer['event']), implode(', ', $observer['observer']));
372+
}
373+
}
374+
375+
$this->newLine();
328376
}
329377

330378
/**

0 commit comments

Comments
 (0)