Browse Source

Update AuthServiceProvider class based on created model policy object

tags/0.1.0
Nafies Luthfi 8 years ago
parent
commit
eed7e6d075
  1. 50
      src/Generators/ModelPolicyGenerator.php
  2. 29
      src/stubs/AuthServiceProvider.stub
  3. 70
      tests/Generators/ModelPolicyGeneratorTest.php
  4. 1
      tests/TestCase.php

50
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;
}
}

29
src/stubs/AuthServiceProvider.stub

@ -0,0 +1,29 @@
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
//
}
}

70
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 = "<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected \$policies = [
'{$this->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 = "<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected \$policies = [
'{$this->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));
}
}

1
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'));

Loading…
Cancel
Save