- Fixed a bug with {% use %} which was buggy whenever two uses were declared and one of them had a curly braces notation in it.
51 lines
1.8 KiB
PHP
51 lines
1.8 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace Picea;
|
|
|
|
class Builder
|
|
{
|
|
public const TEMPLATE_CLASSNAME_PREFIX = "PiceaTemplate_";
|
|
|
|
protected string $templatePath = "";
|
|
|
|
public function __construct(string $templatePath = "./Builder/ClassTemplate.php")
|
|
{
|
|
$this->templatePath = $templatePath;
|
|
}
|
|
|
|
public function build(Compiler\Context &$context, string $compiledSource) : Compiler\Context
|
|
{
|
|
$path = fn($p) => addslashes(str_replace('/', DIRECTORY_SEPARATOR, $p));
|
|
|
|
$context->className = static::generateClassName($context->viewPath);
|
|
|
|
$replace = [
|
|
'%NAMESPACE%' => $context->namespace,
|
|
'%USE%' => ( $uses = $context->renderUses() ) ? $uses : false,
|
|
'%CLASSNAME%' => $context->className,
|
|
'%PATHNAME%' => $path($context->viewPath),
|
|
'%FULLPATH%' => $path($context->filePath),
|
|
'%TEMPLATE%' => $path($this->templatePath),
|
|
'%EXTENDS%' => $context->extendFrom ? "extends " . static::TEMPLATE_CLASSNAME_PREFIX . static::generateClassUID($context->extendFrom) : '',
|
|
'%EXTENDS_TEMPLATE%' => $context->extendFrom,
|
|
'%CONTENT%' => $compiledSource,
|
|
'%FUNCTIONS%' => $context->functionStack ? $context->renderFunctions() : "",
|
|
'%PARENT_OUTPUT%' => $context->extendFrom ? "parent::output(\$variablesList);" : "",
|
|
];
|
|
|
|
$context->compiledSource = str_replace(array_keys($replace), array_values($replace), file_get_contents($this->templatePath));
|
|
|
|
return $context;
|
|
}
|
|
|
|
public static function generateClassName(string $filepath) : string
|
|
{
|
|
return static::TEMPLATE_CLASSNAME_PREFIX . static::generateClassUID($filepath);
|
|
}
|
|
|
|
public static function generateClassUID(string $filePath) : string
|
|
{
|
|
return md5($filePath);
|
|
}
|
|
}
|