Skip to content

Commit a9bf589

Browse files
[9.x] Add Policies to Model Show Command (#45153)
* Add Policies to Model Show Command * Formatting on Policy Line * show first policy Co-authored-by: Taylor Otwell <[email protected]>
1 parent d961975 commit a9bf589

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/Illuminate/Foundation/Console/ShowModelCommand.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Contracts\Container\BindingResolutionException;
1111
use Illuminate\Database\Console\DatabaseInspectionCommand;
1212
use Illuminate\Database\Eloquent\Relations\Relation;
13+
use Illuminate\Support\Facades\Gate;
1314
use Illuminate\Support\Str;
1415
use ReflectionClass;
1516
use ReflectionMethod;
@@ -103,12 +104,27 @@ public function handle()
103104
$class,
104105
$model->getConnection()->getName(),
105106
$model->getConnection()->getTablePrefix().$model->getTable(),
107+
$this->getPolicy($model),
106108
$this->getAttributes($model),
107109
$this->getRelations($model),
108110
$this->getObservers($model),
109111
);
110112
}
111113

114+
/**
115+
* Get the first policy associated with this model.
116+
*
117+
* @param \Illuminate\Database\Eloquent\Model $model
118+
* @return Illuminate\Support\Collection
119+
*/
120+
protected function getPolicy($model)
121+
{
122+
return collect(Gate::policies())
123+
->filter(fn ($policy, $modelClass) => $modelClass === get_class($model))
124+
->values()
125+
->first();
126+
}
127+
112128
/**
113129
* Get the column attributes for the given model.
114130
*
@@ -266,16 +282,17 @@ protected function getObservers($model)
266282
* @param string $class
267283
* @param string $database
268284
* @param string $table
285+
* @param string $policy
269286
* @param \Illuminate\Support\Collection $attributes
270287
* @param \Illuminate\Support\Collection $relations
271288
* @param \Illuminate\Support\Collection $observers
272289
* @return void
273290
*/
274-
protected function display($class, $database, $table, $attributes, $relations, $observers)
291+
protected function display($class, $database, $table, $policy, $attributes, $relations, $observers)
275292
{
276293
$this->option('json')
277-
? $this->displayJson($class, $database, $table, $attributes, $relations, $observers)
278-
: $this->displayCli($class, $database, $table, $attributes, $relations, $observers);
294+
? $this->displayJson($class, $database, $table, $policy, $attributes, $relations, $observers)
295+
: $this->displayCli($class, $database, $table, $policy, $attributes, $relations, $observers);
279296
}
280297

281298
/**
@@ -284,18 +301,20 @@ protected function display($class, $database, $table, $attributes, $relations, $
284301
* @param string $class
285302
* @param string $database
286303
* @param string $table
304+
* @param string $policy
287305
* @param \Illuminate\Support\Collection $attributes
288306
* @param \Illuminate\Support\Collection $relations
289307
* @param \Illuminate\Support\Collection $observers
290308
* @return void
291309
*/
292-
protected function displayJson($class, $database, $table, $attributes, $relations, $observers)
310+
protected function displayJson($class, $database, $table, $policy, $attributes, $relations, $observers)
293311
{
294312
$this->output->writeln(
295313
collect([
296314
'class' => $class,
297315
'database' => $database,
298316
'table' => $table,
317+
'policy' => $policy,
299318
'attributes' => $attributes,
300319
'relations' => $relations,
301320
'observers' => $observers,
@@ -309,19 +328,24 @@ protected function displayJson($class, $database, $table, $attributes, $relation
309328
* @param string $class
310329
* @param string $database
311330
* @param string $table
331+
* @param string $policy
312332
* @param \Illuminate\Support\Collection $attributes
313333
* @param \Illuminate\Support\Collection $relations
314334
* @param \Illuminate\Support\Collection $observers
315335
* @return void
316336
*/
317-
protected function displayCli($class, $database, $table, $attributes, $relations, $observers)
337+
protected function displayCli($class, $database, $table, $policy, $attributes, $relations, $observers)
318338
{
319339
$this->newLine();
320340

321341
$this->components->twoColumnDetail('<fg=green;options=bold>'.$class.'</>');
322342
$this->components->twoColumnDetail('Database', $database);
323343
$this->components->twoColumnDetail('Table', $table);
324344

345+
if ($policy) {
346+
$this->components->twoColumnDetail('Policy', $policy);
347+
}
348+
325349
$this->newLine();
326350

327351
$this->components->twoColumnDetail(
@@ -372,7 +396,9 @@ protected function displayCli($class, $database, $table, $attributes, $relations
372396
if ($observers->count()) {
373397
foreach ($observers as $observer) {
374398
$this->components->twoColumnDetail(
375-
sprintf('%s', $observer['event']), implode(', ', $observer['observer']));
399+
sprintf('%s', $observer['event']),
400+
implode(', ', $observer['observer'])
401+
);
376402
}
377403
}
378404

0 commit comments

Comments
 (0)