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:
- It splits the
$expression
string into an array of segments by exploding it on commas and removing any parentheses. - It trims the first segment to obtain the variable name.
- It trims the second segment to obtain the service name.
- 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}); ?>";
}
}