47 lines
1.6 KiB
PHP
47 lines
1.6 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace Picea;
|
|
|
|
class Builder
|
|
{
|
|
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
|
|
{
|
|
$context->className = static::generateClassName($context->viewPath);
|
|
|
|
$replace = [
|
|
'%NAMESPACE%' => $context->namespace,
|
|
'%USE%' => ( $uses = $context->renderUses() ) ? "use $uses;" : false,
|
|
'%CLASSNAME%' => $context->className,
|
|
'%PATHNAME%' => $context->viewPath,
|
|
'%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);
|
|
}
|
|
}
|