master

laravel/framework

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

CompilesInjections.php

TLDR

The provided file, CompilesInjections.php, is a trait in the Illuminate\View\Compilers\Concerns namespace. It contains a single method, compileInject, which is used to compile inject statements into valid PHP code.

Methods

compileInject

The compileInject method takes a string parameter $expression and returns a string. This method is responsible for compiling inject statements into valid PHP code. It performs the following steps:

  1. It splits the $expression string into an array of segments by exploding it on commas and removing any parentheses.
  2. It trims the first segment to obtain the variable name.
  3. It trims the second segment to obtain the service name.
  4. It returns a PHP code string that assigns the instantiated service object to the variable name.

Classes

None

<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesInjections
{
    /**
     * Compile the inject statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileInject($expression)
    {
        $segments = explode(',', preg_replace("/[\(\)]/", '', $expression));

        $variable = trim($segments[0], " '\"");

        $service = trim($segments[1]);

        return "<?php \${$variable} = app({$service}); ?>";
    }
}