Skip to content

Instantly share code, notes, and snippets.

@AnandPilania
Created February 13, 2021 16:12
Show Gist options
  • Save AnandPilania/80d985b9ff0d10b22df6e2a3b9eced1f to your computer and use it in GitHub Desktop.
Save AnandPilania/80d985b9ff0d10b22df6e2a3b9eced1f to your computer and use it in GitHub Desktop.
Extends Laravel's default `make:policy` command that auto add related mapping to `AuthServiceProvider`
<?php
namespace App\Console\Commands;
use App\Traits\InsertLineTrait; // https://gist.github.com/AnandPilania/da4ddbc407b97c9ce89ff130a5dc9c1e#file-insertlinetrait-php
use Illuminate\Foundation\Console\PolicyMakeCommand as Command;
use Symfony\Component\Console\Input\InputOption;
class PolicyMakeCommand extends Command
{
use InsertLineTrait;
public function handle()
{
parent::handle();
$policyName = $this->argument('name');
$modelName = $this->option('model') ?? null;
if ($this->option('add') && $modelName) {
$this->insertLineAfter(
app_path('Providers/AuthServiceProvider.php'),
"protected \$policies = [",
"\t\t" . trim("'App\Models\\" . $modelName . "' => 'App\Policies\\" . $policyName . "',")
);
}
}
protected function getOptions()
{
return array_merge([
['add', 'a', InputOption::VALUE_NONE, 'Auto add Model => Policy mapping to provider.']
], parent::getOptions());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment