master

laravel/framework

Last updated at: 29/12/2023 09:25

CompilesSessions.php

TLDR

This file is a trait called CompilesSessions that is used in the Illuminate\View\Compilers\Concerns namespace. It contains two methods, compileSession and compileEndsession, which are responsible for compiling session statements into valid PHP code.

Methods

compileSession

This method takes a string parameter called $expression and compiles session statements into valid PHP code. It removes any parentheses from the expression, assigns the session value to a variable, and stores the previous value if it exists.

compileEndsession

This method takes a string parameter called $expression and compiles endsession statements into valid PHP code. It unsets the current session value and restores the previous value if it exists.

<?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); ?>';
    }
}