Skip to content

Commit

Permalink
Add "@isnot" blade directive (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniloBenevides authored and vinicius73 committed Jun 1, 2018
1 parent e793fd0 commit ec530a3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Defender/Providers/DefenderServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ protected function registerBladeExtensions()
$bladeCompiler->directive('endis', function ($expression) {
return '<?php endif; ?>';
});

/*
* add @isnot and @endisnot to blade compiler
*/
$bladeCompiler->directive('isnot', function ($expression) {
return "<?php if(!app('defender')->hasRoles({$this->stripParentheses($expression)})): ?>";
});

$bladeCompiler->directive('endisnot', function ($expression) {
return '<?php endif; ?>';
});
});
}

Expand Down
6 changes: 5 additions & 1 deletion tests/Defender/DefenderServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public function testShouldCompileDefenderTemplateHelpers()
$this->assertNotContains('@is', $compiled);
$this->assertNotContains('@endshield', $compiled);
$this->assertNotContains('@endis', $compiled);

$this->assertNotContains('@isnot', $compiled);
$this->assertNotContains('@endisnot', $compiled);
$this->assertStringEqualsFile($expected, $compiled);
}

Expand All @@ -110,6 +111,9 @@ public function testShouldNotCompileDefenderTemplateHelpers()
$this->assertContains('@is', $compiled);
$this->assertContains('@endshield', $compiled);
$this->assertContains('@endis', $compiled);
$this->assertContains('@isnot', $compiled);
$this->assertContains('@endisnot', $compiled);


$this->assertStringNotEqualsFile($expected, $compiled);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/stubs/views/defender.blade.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Yes, you are admin!
<?php endif; ?>

<?php if(!app('defender')->hasRoles('admin')): ?>
No, you are not an admin!
<?php endif; ?>

<?php if(app('defender')->canDo('do.this')): ?>
Yes, you can do this!
<?php endif; ?>
4 changes: 4 additions & 0 deletions tests/stubs/views/defender.blade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Yes, you are admin!
@endis

@isnot('admin')
No, you are not an admin!
@endisnot

@shield('do.this')
Yes, you can do this!
@endshield

0 comments on commit ec530a3

Please sign in to comment.