-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[10.x] Add Blade
@session
Directive (#49339)
* Add Blade `@session` Directive * Fix test * Refactor to `$__sessionPrevious` array for nesting * Unset `$__sessionPrevious` if it is set and empty. * use value --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
c14b82a
commit 31bc1bd
Showing
3 changed files
with
65 additions
and
0 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
37 changes: 37 additions & 0 deletions
37
src/Illuminate/View/Compilers/Concerns/CompilesSessions.php
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,37 @@ | ||
<?php | ||
|
||
namespace Illuminate\View\Compilers\Concerns; | ||
|
||
trait CompilesSessions | ||
{ | ||
/** | ||
* Compile the session statements into valid PHP. | ||
* | ||
* @param string $expression | ||
* @return string | ||
*/ | ||
protected function compileSession($expression) | ||
{ | ||
$expression = $this->stripParentheses($expression); | ||
|
||
return '<?php $__sessionArgs = ['.$expression.']; | ||
if (session()->has($__sessionArgs[0])) : | ||
if (isset($value)) { $__sessionPrevious[] = $value; } | ||
$value = session()->get($__sessionArgs[0]); ?>'; | ||
} | ||
|
||
/** | ||
* Compile the endsession statements into valid PHP. | ||
* | ||
* @param string $expression | ||
* @return string | ||
*/ | ||
protected function compileEndsession($expression) | ||
{ | ||
return '<?php unset($value); | ||
if (isset($__sessionPrevious) && !empty($__sessionPrevious)) { $value = array_pop($__sessionPrevious); } | ||
if (isset($__sessionPrevious) && empty($__sessionPrevious)) { unset($__sessionPrevious); } | ||
endif; | ||
unset($__sessionArgs); ?>'; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\View\Blade; | ||
|
||
class BladeSessionTest extends AbstractBladeTestCase | ||
{ | ||
public function testSessionsAreCompiled() | ||
{ | ||
$string = ' | ||
@session(\'status\') | ||
<span>{{ $value }}</span> | ||
@endsession'; | ||
$expected = ' | ||
<?php $__sessionArgs = [\'status\']; | ||
if (session()->has($__sessionArgs[0])) : | ||
if (isset($value)) { $__sessionPrevious[] = $value; } | ||
$value = session()->get($__sessionArgs[0]); ?> | ||
<span><?php echo e($value); ?></span> | ||
<?php unset($value); | ||
if (isset($__sessionPrevious) && !empty($__sessionPrevious)) { $value = array_pop($__sessionPrevious); } | ||
if (isset($__sessionPrevious) && empty($__sessionPrevious)) { unset($__sessionPrevious); } | ||
endif; | ||
unset($__sessionArgs); ?>'; | ||
|
||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
} | ||
} |