master

laravel/framework

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

CompilesStacks.php

TLDR

The CompilesStacks trait provides methods for compiling stack statements used in the Laravel views. It includes methods for compiling stack statements, push statements, push-once statements, end-push statements, end-push-once statements, prepend statements, prepend-once statements, end-prepend statements, and end-prepend-once statements.

Methods

compileStack($expression)

This method compiles the stack statements into the content by echoing the yieldPushContent method on the environment variable.

compilePush($expression)

This method compiles the push statements into valid PHP by starting a push block on the environment variable.

compilePushOnce($expression)

This method compiles the push-once statements into valid PHP. It generates a unique identifier if no identifier is provided and checks if the push block has already been rendered before starting the push block.

compileEndpush()

This method compiles the end-push statements into valid PHP by stopping the current push block on the environment variable.

compileEndpushOnce()

This method compiles the end-push-once statements into valid PHP. It stops the current push block and checks if the push block has been rendered once to end the conditional block.

compilePrepend($expression)

This method compiles the prepend statements into valid PHP by starting a prepend block on the environment variable.

compilePrependOnce($expression)

This method compiles the prepend-once statements into valid PHP. It generates a unique identifier if no identifier is provided and checks if the prepend block has already been rendered before starting the prepend block.

compileEndprepend()

This method compiles the end-prepend statements into valid PHP by stopping the current prepend block on the environment variable.

compileEndprependOnce()

This method compiles the end-prepend-once statements into valid PHP. It stops the current prepend block and checks if the prepend block has been rendered once to end the conditional block.

END

<?php

namespace Illuminate\View\Compilers\Concerns;

use Illuminate\Support\Str;

trait CompilesStacks
{
    /**
     * Compile the stack statements into the content.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileStack($expression)
    {
        return "<?php echo \$__env->yieldPushContent{$expression}; ?>";
    }

    /**
     * Compile the push statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compilePush($expression)
    {
        return "<?php \$__env->startPush{$expression}; ?>";
    }

    /**
     * Compile the push-once statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compilePushOnce($expression)
    {
        $parts = explode(',', $this->stripParentheses($expression), 2);

        [$stack, $id] = [$parts[0], $parts[1] ?? ''];

        $id = trim($id) ?: "'".(string) Str::uuid()."'";

        return '<?php if (! $__env->hasRenderedOnce('.$id.')): $__env->markAsRenderedOnce('.$id.');
$__env->startPush('.$stack.'); ?>';
    }

    /**
     * Compile the end-push statements into valid PHP.
     *
     * @return string
     */
    protected function compileEndpush()
    {
        return '<?php $__env->stopPush(); ?>';
    }

    /**
     * Compile the end-push-once statements into valid PHP.
     *
     * @return string
     */
    protected function compileEndpushOnce()
    {
        return '<?php $__env->stopPush(); endif; ?>';
    }

    /**
     * Compile the prepend statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compilePrepend($expression)
    {
        return "<?php \$__env->startPrepend{$expression}; ?>";
    }

    /**
     * Compile the prepend-once statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compilePrependOnce($expression)
    {
        $parts = explode(',', $this->stripParentheses($expression), 2);

        [$stack, $id] = [$parts[0], $parts[1] ?? ''];

        $id = trim($id) ?: "'".(string) Str::uuid()."'";

        return '<?php if (! $__env->hasRenderedOnce('.$id.')): $__env->markAsRenderedOnce('.$id.');
$__env->startPrepend('.$stack.'); ?>';
    }

    /**
     * Compile the end-prepend statements into valid PHP.
     *
     * @return string
     */
    protected function compileEndprepend()
    {
        return '<?php $__env->stopPrepend(); ?>';
    }

    /**
     * Compile the end-prepend-once statements into valid PHP.
     *
     * @return string
     */
    protected function compileEndprependOnce()
    {
        return '<?php $__env->stopPrepend(); endif; ?>';
    }
}