chore: lots of changes

This commit is contained in:
2025-05-24 07:09:22 +02:00
parent 77ee769d5e
commit 899227b0a4
178 changed files with 5145 additions and 53 deletions

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Framework\View\Processors;
use App\Framework\View\DomProcessor;
final class ForProcessor implements DomProcessor
{
/**
* @inheritDoc
*/
public function process(\DOMDocument $dom, RenderContext $context): void
{
$xpath = new \DOMXPath($dom);
foreach ($xpath->query('//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) {
$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));
}
$output .= $dom->saveHTML($fragment);
}
}
$replacement = $dom->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);
}
}
}
}