Skip to content

Commit

Permalink
[5.2] Add @elsecan and @elsecannot blade directives (#13256)
Browse files Browse the repository at this point in the history
*        add elsecan and elsecannot blade directives

*         use container resolving of the Gate class
  • Loading branch information
themsaid authored and taylorotwell committed Apr 25, 2016
1 parent ba82ed4 commit d679a3d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,17 @@ protected function compileCan($expression)
return "<?php if (app('Illuminate\\Contracts\\Auth\\Access\\Gate')->check{$expression}): ?>";
}

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

/**
* Compile the cannot statements into valid PHP.
*
Expand All @@ -639,6 +650,17 @@ protected function compileCannot($expression)
return "<?php if (app('Illuminate\\Contracts\\Auth\\Access\\Gate')->denies{$expression}): ?>";
}

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

/**
* Compile the if statements into valid PHP.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/View/ViewBladeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,13 @@ public function testCanStatementsAreCompiled()
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@can (\'update\', [$post])
breeze
@elsecan(\'delete\', [$post])
sneeze
@endcan';
$expected = '<?php if (app(\'Illuminate\\Contracts\\Auth\\Access\\Gate\')->check(\'update\', [$post])): ?>
breeze
<?php elseif (app(\'Illuminate\\Contracts\\Auth\\Access\\Gate\')->check(\'delete\', [$post])): ?>
sneeze
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
Expand All @@ -269,9 +273,13 @@ public function testCannotStatementsAreCompiled()
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@cannot (\'update\', [$post])
breeze
@elsecannot(\'delete\', [$post])
sneeze
@endcannot';
$expected = '<?php if (app(\'Illuminate\\Contracts\\Auth\\Access\\Gate\')->denies(\'update\', [$post])): ?>
breeze
<?php elseif (app(\'Illuminate\\Contracts\\Auth\\Access\\Gate\')->denies(\'delete\', [$post])): ?>
sneeze
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
Expand Down

0 comments on commit d679a3d

Please sign in to comment.