CompilesUseStatements.php
TLDR
This file contains a PHP trait called CompilesUseStatements
. It provides a method called compileUse
which compiles the use statements into valid PHP.
Methods
compileUse
This method takes a string $expression
as a parameter and returns a compiled PHP code. It performs the following steps:
- It splits the input
$expression
using commas and removes any parentheses. - It trims the first segment from leading and trailing single quotes, double quotes, and spaces. It also removes leading backslashes.
- It checks if the second segment exists. If it does, it trims it from leading and trailing single quotes, double quotes, and spaces.
- It returns a compiled PHP code that uses the trimmed first segment as the namespace and the second segment (if exists) as the alias. The namespace and alias are preceded by the
use
keyword and wrapped in<?php ?>
tags.
END
<?php
namespace Illuminate\View\Compilers\Concerns;
trait CompilesUseStatements
{
/**
* Compile the use statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileUse($expression)
{
$segments = explode(',', preg_replace("/[\(\)]/", '', $expression));
$use = ltrim(trim($segments[0], " '\""), '\\');
$as = isset($segments[1]) ? ' as '.trim($segments[1], " '\"") : '';
return "<?php use \\{$use}{$as}; ?>";
}
}