chore: lots of changes
This commit is contained in:
41
src/Framework/View/Processors/IncludeProcessor.php
Normal file
41
src/Framework/View/Processors/IncludeProcessor.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Framework\View\Processors;
|
||||
|
||||
use App\Framework\View\DomProcessor;
|
||||
use App\Framework\View\DomTemplateParser;
|
||||
use App\Framework\View\RenderContext;
|
||||
use App\Framework\View\TemplateLoader;
|
||||
|
||||
final readonly class IncludeProcessor implements DomProcessor
|
||||
{
|
||||
public function __construct(
|
||||
private TemplateLoader $loader,
|
||||
private DomTemplateParser $parser = new DomTemplateParser()
|
||||
) {}
|
||||
|
||||
public function process(\DOMDocument $dom, RenderContext $context): void
|
||||
{
|
||||
$xpath = new \DOMXPath($dom);
|
||||
|
||||
foreach ($xpath->query('//include[@file]') as $includeNode) {
|
||||
$file = $includeNode->getAttribute('file');
|
||||
|
||||
try {
|
||||
$html = $this->loader->load($file);
|
||||
$includedDom = $this->parser->parse($html);
|
||||
|
||||
$fragment = $dom->createDocumentFragment();
|
||||
foreach ($includedDom->documentElement->childNodes as $child) {
|
||||
$fragment->appendChild($dom->importNode($child, true));
|
||||
}
|
||||
|
||||
$includeNode->parentNode?->replaceChild($fragment, $includeNode);
|
||||
} catch (\Throwable $e) {
|
||||
// Optional: Fehlerkommentar ins Template schreiben
|
||||
$error = $dom->createComment("Fehler beim Laden von '$file': " . $e->getMessage());
|
||||
$includeNode->parentNode?->replaceChild($error, $includeNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user