@@ -103,6 +103,7 @@ public function handle()
103
103
$ model ->getConnection ()->getTablePrefix ().$ model ->getTable (),
104
104
$ this ->getAttributes ($ model ),
105
105
$ this ->getRelations ($ model ),
106
+ $ this ->getObservers ($ model ),
106
107
);
107
108
}
108
109
@@ -223,6 +224,38 @@ protected function getRelations($model)
223
224
->values ();
224
225
}
225
226
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
+
226
259
/**
227
260
* Render the model information.
228
261
*
@@ -231,13 +264,14 @@ protected function getRelations($model)
231
264
* @param string $table
232
265
* @param \Illuminate\Support\Collection $attributes
233
266
* @param \Illuminate\Support\Collection $relations
267
+ * @param \Illuminate\Support\Collection $observers
234
268
* @return void
235
269
*/
236
- protected function display ($ class , $ database , $ table , $ attributes , $ relations )
270
+ protected function display ($ class , $ database , $ table , $ attributes , $ relations, $ observers )
237
271
{
238
272
$ 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 );
241
275
}
242
276
243
277
/**
@@ -248,9 +282,10 @@ protected function display($class, $database, $table, $attributes, $relations)
248
282
* @param string $table
249
283
* @param \Illuminate\Support\Collection $attributes
250
284
* @param \Illuminate\Support\Collection $relations
285
+ * @param \Illuminate\Support\Collection $observers
251
286
* @return void
252
287
*/
253
- protected function displayJson ($ class , $ database , $ table , $ attributes , $ relations )
288
+ protected function displayJson ($ class , $ database , $ table , $ attributes , $ relations, $ observers )
254
289
{
255
290
$ this ->output ->writeln (
256
291
collect ([
@@ -259,6 +294,7 @@ protected function displayJson($class, $database, $table, $attributes, $relation
259
294
'table ' => $ table ,
260
295
'attributes ' => $ attributes ,
261
296
'relations ' => $ relations ,
297
+ 'observers ' => $ observers ,
262
298
])->toJson ()
263
299
);
264
300
}
@@ -271,9 +307,10 @@ protected function displayJson($class, $database, $table, $attributes, $relation
271
307
* @param string $table
272
308
* @param \Illuminate\Support\Collection $attributes
273
309
* @param \Illuminate\Support\Collection $relations
310
+ * @param \Illuminate\Support\Collection $observers
274
311
* @return void
275
312
*/
276
- protected function displayCli ($ class , $ database , $ table , $ attributes , $ relations )
313
+ protected function displayCli ($ class , $ database , $ table , $ attributes , $ relations, $ observers )
277
314
{
278
315
$ this ->newLine ();
279
316
@@ -325,6 +362,17 @@ protected function displayCli($class, $database, $table, $attributes, $relations
325
362
}
326
363
327
364
$ 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 ();
328
376
}
329
377
330
378
/**
0 commit comments