Skip to content

Commit

Permalink
Add @canany and @elsecanany blade directive (#24137)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxu authored and taylorotwell committed May 17, 2018
1 parent 370dbcf commit 97edff8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesAuthorizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ protected function compileCannot($expression)
return "<?php if (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->denies{$expression}): ?>";
}

/**
* Compile the canany statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileCanany($expression)
{
return "<?php if (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->any{$expression}): ?>";
}

/**
* Compile the else-can statements into valid PHP.
*
Expand All @@ -48,6 +59,17 @@ protected function compileElsecannot($expression)
return "<?php elseif (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->denies{$expression}): ?>";
}

/**
* Compile the else-canany statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileElsecanany($expression)
{
return "<?php elseif (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->any{$expression}): ?>";
}

/**
* Compile the end-can statements into valid PHP.
*
Expand All @@ -67,4 +89,14 @@ protected function compileEndcannot()
{
return '<?php endif; ?>';
}

/**
* Compile the end-canany statements into valid PHP.
*
* @return string
*/
protected function compileEndcanany()
{
return '<?php endif; ?>';
}
}
21 changes: 21 additions & 0 deletions tests/View/Blade/BladeCananyStatementsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Illuminate\Tests\View\Blade;

class BladeCananyStatementsTest extends AbstractBladeTestCase
{
public function testCananyStatementsAreCompiled()
{
$string = '@canany ([\'create\', \'update\'], [$post])
breeze
@elsecanany([\'delete\', \'approve\'], [$post])
sneeze
@endcan';
$expected = '<?php if (app(\\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->any([\'create\', \'update\'], [$post])): ?>
breeze
<?php elseif (app(\\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->any([\'delete\', \'approve\'], [$post])): ?>
sneeze
<?php endif; ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
}

0 comments on commit 97edff8

Please sign in to comment.