-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generating policies (#614)
- Loading branch information
1 parent
e6f9cb1
commit 1a62e81
Showing
31 changed files
with
993 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Blueprint\Generators; | ||
|
||
use Blueprint\Concerns\HandlesImports; | ||
use Blueprint\Contracts\Generator; | ||
use Blueprint\Models\Policy; | ||
use Blueprint\Tree; | ||
use Illuminate\Support\Str; | ||
|
||
class PolicyGenerator extends AbstractClassGenerator implements Generator | ||
{ | ||
use HandlesImports; | ||
|
||
protected $types = ['policies']; | ||
|
||
public function output(Tree $tree): array | ||
{ | ||
$this->tree = $tree; | ||
|
||
$stub = $this->filesystem->stub('policy.class.stub'); | ||
|
||
/** @var \Blueprint\Models\Policy $policy */ | ||
foreach ($tree->policies() as $policy) { | ||
$this->addImport($policy, $policy->fullyQualifiedModelClassName()); | ||
|
||
$path = $this->getPath($policy); | ||
|
||
$this->create($path, $this->populateStub($stub, $policy)); | ||
} | ||
|
||
return $this->output; | ||
} | ||
|
||
protected function populateStub(string $stub, Policy $policy) | ||
{ | ||
$stub = str_replace('{{ namespace }}', $policy->fullyQualifiedNamespace(), $stub); | ||
$stub = str_replace('{{ class }}', $policy->className(), $stub); | ||
$stub = str_replace('{{ methods }}', $this->buildMethods($policy), $stub); | ||
$stub = str_replace('{{ imports }}', $this->buildImports($policy), $stub); | ||
|
||
return $stub; | ||
} | ||
|
||
protected function buildMethods(Policy $policy) | ||
{ | ||
$methods = ''; | ||
|
||
foreach ($policy->methods() as $name) { | ||
$methods .= str_replace( | ||
[ | ||
'{{ modelClass }}', | ||
'{{ modelVariable }}', | ||
], | ||
[ | ||
Str::studly($policy->name()), | ||
Str::camel($policy->name()), | ||
], | ||
$this->filesystem->stub('policy.method.' . $name . '.stub'), | ||
) . PHP_EOL; | ||
} | ||
|
||
return trim($methods); | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace Blueprint\Models; | ||
|
||
use Blueprint\Contracts\Model as BlueprintModel; | ||
use Illuminate\Support\Str; | ||
|
||
class Policy implements BlueprintModel | ||
{ | ||
/** @var array */ | ||
public static $supportedMethods = [ | ||
'viewAny', | ||
'view', | ||
'create', | ||
'update', | ||
'delete', | ||
'restore', | ||
'forceDelete', | ||
]; | ||
|
||
/** @var array */ | ||
public static $resourceAbilityMap = [ | ||
'index' => 'viewAny', | ||
'show' => 'view', | ||
'create' => 'create', | ||
'store' => 'create', | ||
'edit' => 'update', | ||
'update' => 'update', | ||
'destroy' => 'delete', | ||
]; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $namespace; | ||
|
||
/** | ||
* @var array<int, string> | ||
*/ | ||
private $methods; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $authorizeResource; | ||
|
||
/** | ||
* Controller constructor. | ||
*/ | ||
public function __construct(string $name, array $methods, bool $authorizeResource) | ||
{ | ||
$this->name = class_basename($name); | ||
$this->namespace = trim(implode('\\', array_slice(explode('\\', str_replace('/', '\\', $name)), 0, -1)), '\\'); | ||
$this->methods = $methods; | ||
$this->authorizeResource = $authorizeResource; | ||
} | ||
|
||
public function name(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function className(): string | ||
{ | ||
return $this->name() . (Str::endsWith($this->name(), 'Policy') ? '' : 'Policy'); | ||
} | ||
|
||
public function namespace() | ||
{ | ||
if (empty($this->namespace)) { | ||
return ''; | ||
} | ||
|
||
return $this->namespace; | ||
} | ||
|
||
public function fullyQualifiedNamespace() | ||
{ | ||
$fqn = config('blueprint.namespace'); | ||
|
||
if (config('blueprint.policy_namespace')) { | ||
$fqn .= '\\' . config('blueprint.policy_namespace'); | ||
} | ||
|
||
if ($this->namespace) { | ||
$fqn .= '\\' . $this->namespace; | ||
} | ||
|
||
return $fqn; | ||
} | ||
|
||
public function fullyQualifiedClassName() | ||
{ | ||
return $this->fullyQualifiedNamespace() . '\\' . $this->className(); | ||
} | ||
|
||
public function methods(): array | ||
{ | ||
return $this->methods; | ||
} | ||
|
||
public function authorizeResource(): bool | ||
{ | ||
return $this->authorizeResource; | ||
} | ||
|
||
public function fullyQualifiedModelClassName() | ||
{ | ||
$fqn = config('blueprint.namespace'); | ||
|
||
if (config('blueprint.models_namespace')) { | ||
$fqn .= '\\' . config('blueprint.models_namespace'); | ||
} | ||
|
||
if ($this->namespace) { | ||
$fqn .= '\\' . $this->namespace; | ||
} | ||
|
||
return $fqn . '\\' . $this->name; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
public function __construct() | ||
{ | ||
$this->authorizeResource({{ modelClass }}::class, '{{ modelVariable }}'); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace {{ namespace }}; | ||
|
||
use App\Models\User; | ||
{{ imports }} | ||
use Illuminate\Auth\Access\Response; | ||
|
||
class {{ class }} | ||
{ | ||
{{ methods }} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Determine whether the user can create models. | ||
*/ | ||
public function create(User $user): bool | ||
{ | ||
// | ||
} |
Oops, something went wrong.