Created
February 13, 2021 16:12
-
-
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`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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