diff --git a/src/Generators/ModelPolicyGenerator.php b/src/Generators/ModelPolicyGenerator.php index 2a60307..68c93c6 100644 --- a/src/Generators/ModelPolicyGenerator.php +++ b/src/Generators/ModelPolicyGenerator.php @@ -24,6 +24,8 @@ class ModelPolicyGenerator extends BaseGenerator ); $this->command->info($this->modelNames['model_name'].' model policy generated.'); + + $this->updateAuthServiceProviderClass(); } /** @@ -51,4 +53,52 @@ class ModelPolicyGenerator extends BaseGenerator return $policyFileContent; } + + /** + * Update AuthServiceProviderClass based on created model policy object + * + * @return void + */ + public function updateAuthServiceProviderClass() + { + $modelName = $this->modelNames['model_name']; + $fullModelName = $this->modelNames['full_model_name']; + $authSPPath = $this->makeAuthServiceProvilderFile(app_path('Providers'), 'AuthServiceProvider.php'); + + $authSPContent = $this->files->get($authSPPath); + + if (! is_null($parentName = $this->command->option('parent'))) { + $modelName = $parentName.'\\'.$modelName; + } + + $authSPContent = str_replace( + " protected \$policies = [\n", + " protected \$policies = [\n '{$fullModelName}' => 'App\Policies\\{$modelName}Policy',\n", + $authSPContent + ); + + $this->generateFile($authSPPath, $authSPContent); + + $this->command->info('AuthServiceProvider class has been updated.'); + } + + /** + * Create AuthServiceProvider class if not exists + * @param string $routeDirPath Absolute directory path + * @param string $filename File name to be created + * @return string Absolute path of create route file + */ + protected function makeAuthServiceProvilderFile($routeDirPath, $filename) + { + $routeDirPath = $this->makeDirectory($routeDirPath); + + if (! $this->files->exists($routeDirPath.'/'.$filename)) { + $this->generateFile( + $routeDirPath.'/'.$filename, + $this->files->get(__DIR__.'/../stubs/AuthServiceProvider.stub') + ); + } + + return $routeDirPath.'/'.$filename; + } } diff --git a/src/stubs/AuthServiceProvider.stub b/src/stubs/AuthServiceProvider.stub new file mode 100644 index 0000000..9e68caa --- /dev/null +++ b/src/stubs/AuthServiceProvider.stub @@ -0,0 +1,29 @@ + 'App\Policies\ModelPolicy', + ]; + + /** + * Register any authentication / authorization services. + * + * @return void + */ + public function boot() + { + $this->registerPolicies(); + + // + } +} diff --git a/tests/Generators/ModelPolicyGeneratorTest.php b/tests/Generators/ModelPolicyGeneratorTest.php index e4030d9..2c54d1f 100644 --- a/tests/Generators/ModelPolicyGeneratorTest.php +++ b/tests/Generators/ModelPolicyGeneratorTest.php @@ -81,6 +81,41 @@ class {$this->model_name}Policy } "; $this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath)); + + $authSPPath = app_path('Providers/AuthServiceProvider.php'); + $this->assertFileExists($authSPPath); + $authSPContent = "full_model_name}' => 'App\Policies\\{$this->model_name}Policy', + 'App\Model' => 'App\Policies\ModelPolicy', + ]; + + /** + * Register any authentication / authorization services. + * + * @return void + */ + public function boot() + { + \$this->registerPolicies(); + + // + } +} +"; + $this->assertEquals($authSPContent, file_get_contents($authSPPath)); } /** @test */ @@ -158,5 +193,40 @@ class {$this->model_name}Policy } "; $this->assertEquals($modelPolicyContent, file_get_contents($modelPolicyPath)); + + $authSPPath = app_path('Providers/AuthServiceProvider.php'); + $this->assertFileExists($authSPPath); + $authSPContent = "full_model_name}' => 'App\Policies\Projects\\{$this->model_name}Policy', + 'App\Model' => 'App\Policies\ModelPolicy', + ]; + + /** + * Register any authentication / authorization services. + * + * @return void + */ + public function boot() + { + \$this->registerPolicies(); + + // + } +} +"; + $this->assertEquals($authSPContent, file_get_contents($authSPPath)); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index ae0603d..5614f02 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -46,6 +46,7 @@ abstract class TestCase extends BaseTestCase $this->removeFileOrDir(resource_path("lang/en/{$this->lang_name}.php")); $this->removeFileOrDir(base_path('routes')); $this->removeFileOrDir(app_path('Policies')); + $this->removeFileOrDir(app_path('Providers')); $this->removeFileOrDir(base_path('tests/BrowserKitTest.php')); $this->removeFileOrDir(base_path('tests/Feature')); $this->removeFileOrDir(base_path('tests/Unit'));