Skip to content

Commit cabbe39

Browse files
committed
List Observers for the Model
1 parent e7ac9b1 commit cabbe39

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

src/Illuminate/Foundation/Console/ShowModelCommand.php

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public function handle()
130130
$model->getConnection()->getTablePrefix().$model->getTable(),
131131
$this->getAttributes($model),
132132
$this->getRelations($model),
133+
$this->getObservers($model),
133134
);
134135
}
135136

@@ -247,6 +248,34 @@ protected function getRelations($model)
247248
->values();
248249
}
249250

251+
/**
252+
* Get the Observers watching this model.
253+
* @return Illuminate\Support\Collection
254+
*/
255+
protected function getObservers($model)
256+
{
257+
$listeners = $this->getLaravel()->make('events')->getRawListeners();
258+
259+
// Distill down to Eloquent observers relevant to this model.
260+
$listeners = array_filter($listeners, function ($v, $key) use ($model) {
261+
return Str::startsWith($key, 'eloquent.') && Str::endsWith($key, $model::class);
262+
}, ARRAY_FILTER_USE_BOTH);
263+
264+
// Format as Eloquent verb => Observer methods
265+
$extractVerb = function ($key) {
266+
preg_match('/eloquent.([a-zA-Z]+)\: /', $key, $matches);
267+
return $matches[1] ?? '?';
268+
};
269+
270+
$formatted = [];
271+
272+
foreach($listeners as $key => $observerMethods) {
273+
$formatted[] = ['event' => $extractVerb($key), 'observer' => $observerMethods];
274+
}
275+
276+
return collect($formatted);
277+
}
278+
250279
/**
251280
* Render the model information.
252281
*
@@ -255,13 +284,14 @@ protected function getRelations($model)
255284
* @param string $table
256285
* @param \Illuminate\Support\Collection $attributes
257286
* @param \Illuminate\Support\Collection $relations
287+
* @param \Illuminate\Support\Collection $observers
258288
* @return void
259289
*/
260-
protected function display($class, $database, $table, $attributes, $relations)
290+
protected function display($class, $database, $table, $attributes, $relations, $observers)
261291
{
262292
$this->option('json')
263-
? $this->displayJson($class, $database, $table, $attributes, $relations)
264-
: $this->displayCli($class, $database, $table, $attributes, $relations);
293+
? $this->displayJson($class, $database, $table, $attributes, $relations, $observers)
294+
: $this->displayCli($class, $database, $table, $attributes, $relations, $observers);
265295
}
266296

267297
/**
@@ -272,9 +302,10 @@ protected function display($class, $database, $table, $attributes, $relations)
272302
* @param string $table
273303
* @param \Illuminate\Support\Collection $attributes
274304
* @param \Illuminate\Support\Collection $relations
305+
* @param \Illuminate\Support\Collection $observers
275306
* @return void
276307
*/
277-
protected function displayJson($class, $database, $table, $attributes, $relations)
308+
protected function displayJson($class, $database, $table, $attributes, $relations, $observers)
278309
{
279310
$this->output->writeln(
280311
collect([
@@ -283,6 +314,7 @@ protected function displayJson($class, $database, $table, $attributes, $relation
283314
'table' => $table,
284315
'attributes' => $attributes,
285316
'relations' => $relations,
317+
'observers' => $observers,
286318
])->toJson()
287319
);
288320
}
@@ -295,9 +327,10 @@ protected function displayJson($class, $database, $table, $attributes, $relation
295327
* @param string $table
296328
* @param \Illuminate\Support\Collection $attributes
297329
* @param \Illuminate\Support\Collection $relations
330+
* @param \Illuminate\Support\Collection $observers
298331
* @return void
299332
*/
300-
protected function displayCli($class, $database, $table, $attributes, $relations)
333+
protected function displayCli($class, $database, $table, $attributes, $relations, $observers)
301334
{
302335
$this->newLine();
303336

@@ -339,6 +372,15 @@ protected function displayCli($class, $database, $table, $attributes, $relations
339372

340373
$this->newLine();
341374

375+
$this->components->twoColumnDetail('<fg=green;options=bold>Eloquent Observers</>');
376+
377+
if ($observers->count()) foreach ($observers as $observer) {
378+
$this->components->twoColumnDetail(
379+
sprintf('%s', $observer['event']), implode(', ', $observer['observer']));
380+
}
381+
382+
$this->newLine();
383+
342384
$this->components->twoColumnDetail('<fg=green;options=bold>Relations</>');
343385

344386
foreach ($relations as $relation) {

0 commit comments

Comments
 (0)