master

laravel/framework

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

CompilesRawPhp.php

TLDR

This file contains a trait called CompilesRawPhp which provides two methods for compiling raw PHP statements: compilePhp and compileUnset.

Methods

compilePhp

This method takes a PHP expression as a parameter and compiles it into valid PHP code. If the expression is not empty, it wraps the expression inside <?php and ?> tags. If the expression is empty, it returns the string @php.

compileUnset

This method takes a PHP expression as a parameter and compiles it into a valid PHP unset statement. It wraps the expression inside <?php unset and ?> tags.

<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesRawPhp
{
    /**
     * Compile the raw PHP statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compilePhp($expression)
    {
        if ($expression) {
            return "<?php {$expression}; ?>";
        }

        return '@php';
    }

    /**
     * Compile the unset statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileUnset($expression)
    {
        return "<?php unset{$expression}; ?>";
    }
}