master

laravel/framework

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

CompilesIncludes.php

TLDR

The CompilesIncludes.php file in the Illuminate\View\Compilers\Concerns namespace contains a trait called CompilesIncludes. This trait provides several methods for compiling different include statements into valid PHP code.

Methods

compileEach($expression)

This method compiles the each statements into valid PHP code. It takes an expression as a parameter and returns the compiled PHP code.

compileInclude($expression)

This method compiles the include statements into valid PHP code. It takes an expression as a parameter and returns the compiled PHP code.

compileIncludeIf($expression)

This method compiles the include-if statements into valid PHP code. It takes an expression as a parameter and returns the compiled PHP code.

compileIncludeWhen($expression)

This method compiles the include-when statements into valid PHP code. It takes an expression as a parameter and returns the compiled PHP code.

compileIncludeUnless($expression)

This method compiles the include-unless statements into valid PHP code. It takes an expression as a parameter and returns the compiled PHP code.

compileIncludeFirst($expression)

This method compiles the include-first statements into valid PHP code. It takes an expression as a parameter and returns the compiled PHP code.

<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesIncludes
{
    /**
     * Compile the each statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileEach($expression)
    {
        return "<?php echo \$__env->renderEach{$expression}; ?>";
    }

    /**
     * Compile the include statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileInclude($expression)
    {
        $expression = $this->stripParentheses($expression);

        return "<?php echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";
    }

    /**
     * Compile the include-if statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileIncludeIf($expression)
    {
        $expression = $this->stripParentheses($expression);

        return "<?php if (\$__env->exists({$expression})) echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";
    }

    /**
     * Compile the include-when statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileIncludeWhen($expression)
    {
        $expression = $this->stripParentheses($expression);

        return "<?php echo \$__env->renderWhen($expression, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path'])); ?>";
    }

    /**
     * Compile the include-unless statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileIncludeUnless($expression)
    {
        $expression = $this->stripParentheses($expression);

        return "<?php echo \$__env->renderUnless($expression, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path'])); ?>";
    }

    /**
     * Compile the include-first statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileIncludeFirst($expression)
    {
        $expression = $this->stripParentheses($expression);

        return "<?php echo \$__env->first({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";
    }
}