chore: complete update
This commit is contained in:
@@ -2,58 +2,71 @@
|
||||
|
||||
namespace App\Framework\View\Processors;
|
||||
|
||||
use App\Framework\DI\Container;
|
||||
use App\Framework\Meta\MetaData;
|
||||
use App\Framework\View\DomProcessor;
|
||||
use App\Framework\View\DomWrapper;
|
||||
use App\Framework\View\RenderContext;
|
||||
use App\Framework\View\TemplateProcessor;
|
||||
|
||||
final class ForProcessor implements DomProcessor
|
||||
{
|
||||
public function __construct(
|
||||
private Container $container,
|
||||
private ?TemplateProcessor $templateProcessor = null,
|
||||
|
||||
) {
|
||||
// Falls kein TemplateProcessor übergeben wird, erstellen wir einen mit den Standard-Prozessoren
|
||||
if ($this->templateProcessor === null) {
|
||||
#$this->templateProcessor = new TemplateProcessor([],[PlaceholderReplacer::class], $this->container);
|
||||
#$this->templateProcessor->register(PlaceholderReplacer::class);
|
||||
$this->templateProcessor = $this->container->get(TemplateProcessor::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function process(\DOMDocument $dom, RenderContext $context): void
|
||||
public function process(DomWrapper $dom, RenderContext $context): DomWrapper
|
||||
{
|
||||
$xpath = new \DOMXPath($dom);
|
||||
|
||||
foreach ($xpath->query('//for[@var][@in]') as $node) {
|
||||
foreach ($dom->document->querySelectorAll('for[var][in]') as $node) {
|
||||
$var = $node->getAttribute('var');
|
||||
$in = $node->getAttribute('in');
|
||||
|
||||
$output = '';
|
||||
|
||||
if (isset($context->data[$in]) && is_iterable($context->data[$in])) {
|
||||
foreach ($context->data[$in] as $item) {
|
||||
$items = $context->data[$in] ?? null;
|
||||
if (isset($context->model->{$in}) && $items === null) {
|
||||
$items = $context->model->{$in};
|
||||
}
|
||||
|
||||
if (is_iterable($items)) {
|
||||
foreach ($items as $item) {
|
||||
$clone = $node->cloneNode(true);
|
||||
foreach ($clone->childNodes as $child) {
|
||||
$this->replacePlaceholdersRecursive($child, [$var => $item] + $context->data);
|
||||
}
|
||||
|
||||
$fragment = $dom->createDocumentFragment();
|
||||
foreach ($clone->childNodes as $child) {
|
||||
$fragment->appendChild($child->cloneNode(true));
|
||||
}
|
||||
// Neuen Kontext für die Schleifenvariable erstellen
|
||||
$loopContext = new RenderContext(
|
||||
template: $context->template,
|
||||
metaData: new MetaData('', ''),
|
||||
data: array_merge($context->data, [$var => $item]),
|
||||
#model: $context->model,
|
||||
controllerClass: $context->controllerClass
|
||||
);
|
||||
|
||||
$output .= $dom->saveHTML($fragment);
|
||||
// Den Inhalt der Schleife mit den bestehenden Prozessoren verarbeiten
|
||||
$innerHTML = $clone->innerHTML;
|
||||
|
||||
$processedContent = $this->templateProcessor->render($loopContext, $innerHTML, true);
|
||||
|
||||
$output .= $processedContent;
|
||||
}
|
||||
}
|
||||
|
||||
$replacement = $dom->createDocumentFragment();
|
||||
$replacement = $dom->document->createDocumentFragment();
|
||||
@$replacement->appendXML($output);
|
||||
$node->parentNode?->replaceChild($replacement, $node);
|
||||
}
|
||||
}
|
||||
|
||||
private function replacePlaceholdersRecursive(\DOMNode $node, array $data): void
|
||||
{
|
||||
if ($node->nodeType === XML_TEXT_NODE) {
|
||||
$node->nodeValue = preg_replace_callback('/{{\s*(\w+)\s*}}/', function ($matches) use ($data) {
|
||||
return htmlspecialchars((string)($data[$matches[1]] ?? $matches[0]), ENT_QUOTES | ENT_HTML5);
|
||||
}, $node->nodeValue);
|
||||
}
|
||||
|
||||
if ($node->hasChildNodes()) {
|
||||
foreach ($node->childNodes as $child) {
|
||||
$this->replacePlaceholdersRecursive($child, $data);
|
||||
}
|
||||
}
|
||||
return $dom;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user