Convert include code into a method of Template class

This commit is contained in:
Kijin Sung 2023-10-18 00:01:11 +09:00
parent 012dbb9ab7
commit e044e11c5f
4 changed files with 77 additions and 114 deletions

View file

@ -457,6 +457,63 @@ class Template
* =================== HELPER FUNCTIONS FOR TEMPLATE v2 ===================
*/
/**
* Include another template from v2 @include directive.
*
* Blade has several variations of the @include directive, and we need
* access to the actual PHP args in order to process them accurately.
* So we do this in the Template class, not in the converter.
*
* @param ...$args
* @return string
*/
protected function _v2_include(...$args): string
{
// Set some basic information.
$directive = $args[0];
$extension = $this->extension === 'blade.php' ? 'blade.php' : null;
$isConditional = in_array($directive, ['includeWhen', 'includeUnless']);
$basedir = $this->relative_dirname;
$cond = $isConditional ? $args[1] : null;
$path = $isConditional ? $args[2] : $args[1];
$vars = $isConditional ? ($args[3] ?? null) : ($args[2] ?? null);
// Handle paths relative to the Rhymix installation directory.
if (preg_match('#^\^/?(\w.+)$#s', $path, $match))
{
$basedir = str_contains($match[1], '/') ? dirname($match[1]) : \RX_BASEDIR;
$path = basename($match[1]);
}
// If the conditions are not met, return.
if ($isConditional && $directive === 'includeWhen' && !$cond)
{
return '';
}
if ($isConditional && $directive === 'includeUnless' && $cond)
{
return '';
}
// Create a new instance of TemplateHandler.
$template = new self($basedir, $path, $extension);
// If the directive is @includeIf and the template file does not exist, return.
if ($directive === 'includeIf' && !$template->exists())
{
return '';
}
// Set variables.
if ($vars !== null)
{
$template->setVars($vars);
}
// Compile and return.
return $template->compile();
}
/**
* Load a resource from v2 @load directive.
*