chore: complete update
This commit is contained in:
@@ -2,46 +2,38 @@
|
||||
|
||||
namespace App\Framework\View;
|
||||
|
||||
final class ComponentRenderer
|
||||
use App\Framework\Core\PathProvider;
|
||||
use App\Framework\Filesystem\FileStorage;
|
||||
use App\Framework\Meta\MetaData;
|
||||
use App\Framework\View\Loading\TemplateLoader;
|
||||
|
||||
final readonly class ComponentRenderer
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TemplateLoader $loader = new TemplateLoader(),
|
||||
private readonly Compiler $compiler = new Compiler(),
|
||||
private readonly TemplateProcessor $processor = new TemplateProcessor(),
|
||||
private string $cacheDir = __DIR__ . "/cache/components/"
|
||||
private TemplateProcessor $processor,
|
||||
private ComponentCache $cache,
|
||||
private TemplateLoader $loader,
|
||||
private FileStorage $storage = new FileStorage,
|
||||
) {}
|
||||
|
||||
public function render(string $componentName, array $data): string
|
||||
{
|
||||
$path = $this->loader->getComponentPath($componentName);
|
||||
if (!file_exists($path)) {
|
||||
|
||||
if(!$this->storage->exists($path)) {
|
||||
return "<!-- Komponente '$componentName' nicht gefunden -->";
|
||||
}
|
||||
|
||||
# Cache prüfen
|
||||
$hash = md5_file($path) . '_' . md5(serialize($data));
|
||||
$cacheFile = $this->cacheDir . "/{$componentName}_{$hash}.html";;
|
||||
|
||||
if(file_exists($cacheFile)) {
|
||||
return file_get_contents($cacheFile);
|
||||
if(!$cached = $this->cache->get($componentName, $data, $path)) {
|
||||
return $cached;
|
||||
}
|
||||
|
||||
$template = $this->storage->get($path);
|
||||
$context = new RenderContext(template: $componentName, metaData: new MetaData(''), data: $data);
|
||||
$output = $this->processor->render($context, $template);
|
||||
|
||||
$template = file_get_contents($path);
|
||||
$compiled = $this->compiler->compile($template)->saveHTML();
|
||||
$this->cache->set($componentName, $data, $path, $output);
|
||||
|
||||
$context = new RenderContext(
|
||||
template: $componentName,
|
||||
data: $data
|
||||
);
|
||||
|
||||
$output = $this->processor->render($context, $compiled);
|
||||
|
||||
if(!is_dir($this->cacheDir)) {
|
||||
mkdir($this->cacheDir, 0777, true);
|
||||
}
|
||||
|
||||
file_put_contents($cacheFile, $output);
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user