Skip to content

Commit

Permalink
[feat] simplify the definition of multiple Blade "if" directives (#2628)
Browse files Browse the repository at this point in the history
* [feat] simplify the definition of a Blade directives

---------

Co-authored-by: erikn69 <erikn_69@hotmail.com>
  • Loading branch information
alissn and erikn69 committed Mar 1, 2024
1 parent 29b16ce commit 5243ff9
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,20 @@ public static function bladeMethodWrapper($method, $role, $guard = null): bool
return auth($guard)->check() && auth($guard)->user()->{$method}($role);
}

protected function registerBladeExtensions($bladeCompiler): void
protected function registerBladeExtensions(BladeCompiler $bladeCompiler): void
{
$bladeMethodWrapper = '\\Spatie\\Permission\\PermissionServiceProvider::bladeMethodWrapper';

$bladeCompiler->directive('role', fn ($args) => "<?php if({$bladeMethodWrapper}('hasRole', {$args})): ?>");
$bladeCompiler->directive('elserole', fn ($args) => "<?php elseif({$bladeMethodWrapper}('hasRole', {$args})): ?>");
$bladeCompiler->directive('endrole', fn () => '<?php endif; ?>');
// permission checks
$bladeCompiler->if('haspermission', fn () => $bladeMethodWrapper('checkPermissionTo', ...func_get_args()));

$bladeCompiler->directive('haspermission', fn ($args) => "<?php if({$bladeMethodWrapper}('checkPermissionTo', {$args})): ?>");
$bladeCompiler->directive('elsehaspermission', fn ($args) => "<?php elseif({$bladeMethodWrapper}('checkPermissionTo', {$args})): ?>");
$bladeCompiler->directive('endhaspermission', fn () => '<?php endif; ?>');

$bladeCompiler->directive('hasrole', fn ($args) => "<?php if({$bladeMethodWrapper}('hasRole', {$args})): ?>");
$bladeCompiler->directive('endhasrole', fn () => '<?php endif; ?>');

$bladeCompiler->directive('hasanyrole', fn ($args) => "<?php if({$bladeMethodWrapper}('hasAnyRole', {$args})): ?>");
$bladeCompiler->directive('endhasanyrole', fn () => '<?php endif; ?>');

$bladeCompiler->directive('hasallroles', fn ($args) => "<?php if({$bladeMethodWrapper}('hasAllRoles', {$args})): ?>");
$bladeCompiler->directive('endhasallroles', fn () => '<?php endif; ?>');

$bladeCompiler->directive('unlessrole', fn ($args) => "<?php if(! {$bladeMethodWrapper}('hasRole', {$args})): ?>");
// role checks
$bladeCompiler->if('role', fn () => $bladeMethodWrapper('hasRole', ...func_get_args()));
$bladeCompiler->if('hasrole', fn () => $bladeMethodWrapper('hasRole', ...func_get_args()));
$bladeCompiler->if('hasanyrole', fn () => $bladeMethodWrapper('hasAnyRole', ...func_get_args()));
$bladeCompiler->if('hasallroles', fn () => $bladeMethodWrapper('hasAllRoles', ...func_get_args()));
$bladeCompiler->if('hasexactroles', fn () => $bladeMethodWrapper('hasExactRoles', ...func_get_args()));
$bladeCompiler->directive('endunlessrole', fn () => '<?php endif; ?>');

$bladeCompiler->directive('hasexactroles', fn ($args) => "<?php if({$bladeMethodWrapper}('hasExactRoles', {$args})): ?>");
$bladeCompiler->directive('endhasexactroles', fn () => '<?php endif; ?>');
}

protected function registerMacroHelpers(): void
Expand Down

0 comments on commit 5243ff9

Please sign in to comment.