| 1 | <?php |
| 2 | |
| 3 | final class TemplateRenderer |
| 4 | { |
| 5 | private string $templatesDir; |
| 6 | |
| 7 | public function __construct(?string $templatesDir = null) |
| 8 | { |
| 9 | $this->templatesDir = $templatesDir ?? (__DIR__ . DIRECTORY_SEPARATOR . 'templates'); |
| 10 | } |
| 11 | |
| 12 | public function renderFromJsonFile(string $jsonPath): string |
| 13 | { |
| 14 | if (!str_contains($jsonPath, __DIR__)) |
| 15 | $jsonPath = __DIR__ . $jsonPath; |
| 16 | |
| 17 | if (!is_file($jsonPath)) { |
| 18 | throw new RuntimeException("JSON file not found: {$jsonPath}"); |
| 19 | } |
| 20 | $raw = file_get_contents($jsonPath); |
| 21 | if ($raw === false) { |
| 22 | throw new RuntimeException("Failed to read JSON file: {$jsonPath}"); |
| 23 | } |
| 24 | $data = json_decode($raw, true, 512, JSON_THROW_ON_ERROR); |
| 25 | $data['wikiPath'] = substr($jsonPath, strlen(__DIR__)); |
| 26 | return $this->render($data); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param array<string, mixed> $data |
| 31 | */ |
| 32 | public function render(array $data): string |
| 33 | { |
| 34 | $type = $data['type'] ?? null; |
| 35 | if (!is_string($type) || $type === '') { |
| 36 | throw new RuntimeException("JSON object is missing a valid 'type' field."); |
| 37 | } |
| 38 | |
| 39 | $template = $this->loadTemplate($type); |
| 40 | return $this->renderTemplateString($template, $data); |
| 41 | } |
| 42 | |
| 43 | public function renderAll(array $data): string |
| 44 | { |
| 45 | $string = ""; |
| 46 | foreach ($data as $item) { |
| 47 | $array = json_decode(json_encode($item), true); |
| 48 | $string .= $this->render($array); |
| 49 | } |
| 50 | |
| 51 | return $string; |
| 52 | } |
| 53 | |
| 54 | private function loadTemplate(string $name): string |
| 55 | { |
| 56 | $path = $this->templatesDir . DIRECTORY_SEPARATOR . $name . '.template'; |
| 57 | if (!is_file($path)) { |
| 58 | throw new RuntimeException("Template not found: {$path}"); |
| 59 | } |
| 60 | $tpl = file_get_contents($path); |
| 61 | if ($tpl === false) { |
| 62 | throw new RuntimeException("Failed to read template: {$path}"); |
| 63 | } |
| 64 | return $tpl; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Main rendering pipeline: |
| 69 | * 1) resolve child-template tags [[path:tplName]] |
| 70 | * 2) resolve raw {{{path}}} |
| 71 | * 3) resolve escaped {{path}} |
| 72 | */ |
| 73 | private function renderTemplateString(string $template, array $data): string |
| 74 | { |
| 75 | // 1) Child-template tags: [[moves:proficiency.move]] |
| 76 | $template = preg_replace_callback( |
| 77 | '/\[\[\s*([a-zA-Z0-9_.-]+)\s*:\s*([a-zA-Z0-9_.-]+)\s*\]\]/', |
| 78 | function ($m) use ($data) { |
| 79 | $path = $m[1]; |
| 80 | $childTplName = $m[2]; |
| 81 | |
| 82 | $value = $this->getByPath($data, $path); |
| 83 | if ($value === null) |
| 84 | return ''; |
| 85 | |
| 86 | $childTpl = $this->loadTemplate($childTplName); |
| 87 | |
| 88 | // Render array => multiple children |
| 89 | if (is_array($value) && $this->isList($value)) { |
| 90 | $out = ''; |
| 91 | foreach ($value as $childItem) { |
| 92 | if (is_array($childItem)) { |
| 93 | // Object/assoc child |
| 94 | $childCtx = $childItem; |
| 95 | } else { |
| 96 | // Scalar child (string, int, etc.) |
| 97 | $childCtx = [ |
| 98 | 'value' => $childItem |
| 99 | ]; |
| 100 | } |
| 101 | |
| 102 | $childCtx['_parent'] = $data; |
| 103 | $out .= $this->renderTemplateString($childTpl, $childCtx); |
| 104 | } |
| 105 | return $out; |
| 106 | } |
| 107 | |
| 108 | // Render object/assoc => single child |
| 109 | if (is_array($value) && !$this->isList($value)) { |
| 110 | $childCtx = $value; |
| 111 | $childCtx['_parent'] = $data; |
| 112 | return $this->renderTemplateString($childTpl, $childCtx); |
| 113 | } |
| 114 | |
| 115 | // Scalars: nothing (you could also return scalar string if desired) |
| 116 | return ''; |
| 117 | }, |
| 118 | $template |
| 119 | ); |
| 120 | |
| 121 | // 2) Raw placeholders {{{path}}} |
| 122 | $template = preg_replace_callback( |
| 123 | '/\{\{\{\s*([a-zA-Z0-9_.-]+)\s*\}\}\}/', |
| 124 | fn($m) => $this->toString($this->getByPath($data, $m[1]), false), |
| 125 | $template |
| 126 | ); |
| 127 | |
| 128 | // 3) Escaped placeholders {{path}} |
| 129 | $template = preg_replace_callback( |
| 130 | '/\{\{\s*([a-zA-Z0-9_.-]+)\s*\}\}/', |
| 131 | fn($m) => $this->toString($this->getByPath($data, $m[1]), true), |
| 132 | $template |
| 133 | ); |
| 134 | |
| 135 | if (isset($data['wikiPath'])) |
| 136 | $template = str_replace("<<url>>", "?path=" . $data['wikiPath'], $template); |
| 137 | |
| 138 | return $template ?? ''; |
| 139 | } |
| 140 | |
| 141 | private function getByPath(mixed $data, string $path): mixed |
| 142 | { |
| 143 | $parts = explode('.', $path); |
| 144 | $cur = $data; |
| 145 | |
| 146 | foreach ($parts as $part) { |
| 147 | if (!is_array($cur)) |
| 148 | return null; |
| 149 | |
| 150 | if (ctype_digit($part)) { |
| 151 | $idx = (int) $part; |
| 152 | $cur = $cur[$idx] ?? null; |
| 153 | } else { |
| 154 | $cur = $cur[$part] ?? null; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return $cur; |
| 159 | } |
| 160 | |
| 161 | private function toString(mixed $value, bool $escape): string |
| 162 | { |
| 163 | if ($value === null) |
| 164 | return ''; |
| 165 | |
| 166 | if (is_bool($value)) { |
| 167 | $str = $value ? 'true' : 'false'; |
| 168 | } elseif (is_int($value) || is_float($value) || is_string($value)) { |
| 169 | $str = (string) $value; |
| 170 | } elseif (is_array($value)) { |
| 171 | $str = json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); |
| 172 | } else { |
| 173 | $str = (string) $value; |
| 174 | } |
| 175 | |
| 176 | return str_replace("\n", "<br/>", $escape |
| 177 | ? htmlspecialchars($str, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') |
| 178 | : $str); |
| 179 | } |
| 180 | |
| 181 | private function isList(array $arr): bool |
| 182 | { |
| 183 | // PHP 8.1+: array_is_list($arr) |
| 184 | $i = 0; |
| 185 | foreach ($arr as $k => $_) { |
| 186 | if ($k !== $i++) |
| 187 | return false; |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | } |