From 31bc1bdf1ed7fdd59b07cfe7f82cda481b1b39ce Mon Sep 17 00:00:00 2001 From: Jared Lewis <17649602+jrd-lewis@users.noreply.github.com> Date: Fri, 15 Dec 2023 10:13:34 -0500 Subject: [PATCH] [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 --- .../View/Compilers/BladeCompiler.php | 1 + .../Compilers/Concerns/CompilesSessions.php | 37 +++++++++++++++++++ tests/View/Blade/BladeSessionTest.php | 27 ++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 src/Illuminate/View/Compilers/Concerns/CompilesSessions.php create mode 100644 tests/View/Blade/BladeSessionTest.php diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index 18aae67e14ba..c006bbd0f5c2 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -30,6 +30,7 @@ class BladeCompiler extends Compiler implements CompilerInterface Concerns\CompilesLayouts, Concerns\CompilesLoops, Concerns\CompilesRawPhp, + Concerns\CompilesSessions, Concerns\CompilesStacks, Concerns\CompilesStyles, Concerns\CompilesTranslations, diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesSessions.php b/src/Illuminate/View/Compilers/Concerns/CompilesSessions.php new file mode 100644 index 000000000000..0c375b406542 --- /dev/null +++ b/src/Illuminate/View/Compilers/Concerns/CompilesSessions.php @@ -0,0 +1,37 @@ +stripParentheses($expression); + + return '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 ''; + } +} diff --git a/tests/View/Blade/BladeSessionTest.php b/tests/View/Blade/BladeSessionTest.php new file mode 100644 index 000000000000..d3d553e0a7e9 --- /dev/null +++ b/tests/View/Blade/BladeSessionTest.php @@ -0,0 +1,27 @@ +{{ $value }} +@endsession'; + $expected = ' +has($__sessionArgs[0])) : +if (isset($value)) { $__sessionPrevious[] = $value; } +$value = session()->get($__sessionArgs[0]); ?> + +'; + + $this->assertEquals($expected, $this->compiler->compileString($string)); + } +}