-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.php
More file actions
293 lines (245 loc) · 9.57 KB
/
Copy pathRenderer.php
File metadata and controls
293 lines (245 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php declare(strict_types=1);
namespace Computator\FrameworkUtils\PHPTemplate;
use Computator\FrameworkUtils\PHPTemplate\Exceptions;
use Computator\FrameworkUtils\PHPTemplate\RenderTree;
use Computator\FrameworkUtils\PHPTemplate\Templates;
use Computator\FrameworkUtils\PHPTemplate\UserApi;
use ArrayAccess;
use SplObjectStorage;
use Throwable;
use ValueError;
use function array_key_exists;
use function is_string;
use function ob_get_level,ob_end_flush;
class Renderer implements UserApi\RenderManager, UserApi\RenderClient {
protected readonly Templates\Base $root_template;
protected readonly TemplateResolver $resolver;
/** @var ArrayAccess<Templates\Base,RendererTemplateState> $tpl_state_map */
protected SplObjectStorage $tpl_state_map;
protected array $tpl_proxy_map = [];
protected readonly bool $using_tree;
protected RenderTree\Tree $rendertree;
protected ?RenderTree\Buffer $current_buff = null;
protected bool $rendering_to_string;
public static function create(Templates\Base $template, TemplateResolver $resolver = new TemplateResolver()): UserApi\RenderClient {
return new static($template, $resolver);
}
final private function __construct(Templates\Base $template, TemplateResolver $resolver) {
$this->root_template = $template;
$this->resolver = $resolver;
$this->tpl_state_map = new SplObjectStorage();
// TODO: make using a tree optional once we can detect if
// there is any out of order rendering, or always use the tree
// if we can render the tree without any buffering
$this->initialize_tree();
}
public function render(): void {
$this->rendering_to_string = false;
// use try-finally since we want to send output as well
// as throw any errors
try {
$this->render_with_inherit($this->root_template, []);
} finally {
if ($this->using_tree)
$this->rendertree->render();
}
}
public function renderToString(): string {
$this->rendering_to_string = true;
ob_start();
$this->render_with_inherit($this->root_template, []);
if ($this->using_tree)
$this->rendertree->render();
return (string) ob_get_clean();
}
protected function tpl_state(Templates\Base $tpl): RendererTemplateState {
return $this->tpl_state_map[$tpl] ??= new RendererTemplateState();
}
protected function initialize_tree(): void {
$this->using_tree = true;
$this->rendertree = new RenderTree\Tree(
RenderTree\Node::withValue(null),
);
}
protected function swap_to_new_buffer(): bool {
if ($was_buffering = $this->current_buff !== null)
$this->current_buff->append((string) ob_get_clean());
$this->current_buff = new RenderTree\Buffer();
$this->rendertree->addValue($this->current_buff);
ob_start();
return $was_buffering;
}
protected function complete_buffer(): void {
assert($this->current_buff !== null);
$this->current_buff->append((string) ob_get_clean());
$this->current_buff = null;
}
/** @param array<string,mixed> $context */
protected function do_render(Templates\Base $tpl, array $context): void {
$was_buffering = false;
if ($this->using_tree) {
$was_buffering = $this->swap_to_new_buffer();
}
$start_ob_level = ob_get_level();
$error = null;
try {
// TODO: handle execute return values
$tpl->execute(
$context,
renderer: $this,
template: $tpl,
);
} catch (Throwable $t) {
$error = new Exceptions\TemplateRenderException(
"error while rendering template: {$t->getMessage()}",
previous: $t,
);
}
if (ob_get_level() > $start_ob_level) {
do {
ob_end_flush();
} while (ob_get_level() > $start_ob_level);
if (!$error)
$error = new Exceptions\TemplateRenderException("template did not close it's output buffers!");
}
if ($this->using_tree) {
$this->complete_buffer();
if ($was_buffering)
$this->swap_to_new_buffer();
}
if ($error)
throw $error;
}
protected function render_parent(Templates\Base $child_tpl): void {
$parent_tpl = $this->tpl_state($child_tpl)->parent;
assert($parent_tpl !== null);
assert($this->tpl_state($parent_tpl)->child === $child_tpl);
$target = $this->tpl_state($parent_tpl)->parent_render_target;
assert($target !== null);
$this->rendertree->setCurrentNode($target);
$this->do_render($parent_tpl, []);
// if the parent has it's own parent, render that
if ($this->tpl_state($parent_tpl)->parent !== null)
$this->render_parent($parent_tpl);
}
/** @param array<string,mixed> $context */
protected function render_with_inherit(Templates\Base $tpl, array $context): void {
$this->do_render($tpl, $context);
if ($this->tpl_state($tpl)->parent !== null)
$this->render_parent($tpl);
}
public function getTemplateAsProxy(string $template): RenderObjects\TemplateRenderProxy {
$tpl = $this->resolver->resolve($template);
$proxy = new RenderObjects\TemplateRenderProxy(
$this,
$tpl,
);
$this->tpl_proxy_map[$proxy->id] = $tpl;
return $proxy;
}
public function getTemplateInstanceAsProxyById(int $orig_proxy_id): ?RenderObjects\TemplateRenderProxy {
if (!array_key_exists($orig_proxy_id, $this->tpl_proxy_map))
return null;
$proxy = new RenderObjects\TemplateRenderProxy(
$this,
$this->tpl_proxy_map[$orig_proxy_id],
);
$this->tpl_proxy_map[$proxy->id] = $this->tpl_proxy_map[$orig_proxy_id];
return $proxy;
}
/** @param array<string,mixed> $context */
public function renderProxiedTemplate(RenderObjects\TemplateRenderProxy $proxy, array $context): void {
$prev = $this->rendertree->getCurrentNode();
$new_node = $this->rendertree->addNode();
$this->rendertree->setCurrentNode($new_node);
$this->render_with_inherit($this->tpl_proxy_map[$proxy->id], $context);
$this->rendertree->setCurrentNode($prev);
// if rendering is nested the final buffer node
// needs to be shifted back to the parent node
if ($this->current_buff !== null) {
$prev->appendChildren($new_node->popChild());
}
}
public function renderError(Templates\PHPString|Templates\Text|string $error): void {
if (is_string($error))
$error = new Templates\Text($error);
$this->do_render($error, []);
}
public function setParentForTemplate(Templates\Base $tpl, string $parent_template): void {
if (isset($this->tpl_state($tpl)->parent))
throw new Exceptions\RendererStateException("template already has an associated parent");
$new_parent_tpl = $this->resolver->resolve($parent_template);
$this->tpl_state($tpl)->parent = $new_parent_tpl;
$this->tpl_state($new_parent_tpl)->child = $tpl;
$curr = $this->rendertree->getCurrentNode();
$new_child = $curr->isLeaf()
? RenderTree\IgnoredNode::withValue($curr->getValue())
: RenderTree\IgnoredNode::withChildren(...$curr);
$curr->replaceChildren(
$new_child,
);
$this->rendertree->setCurrentNode($new_child);
$this->tpl_state($new_parent_tpl)->parent_render_target = $curr;
$this->tpl_state($tpl)->child_render_root = $new_child;
}
public function startRenderingBlock(Templates\Base $tpl, string $block_name): void {
if ($block_name == '')
throw new ValueError("'\$block_name' can not be empty");
if ($this->tpl_state($tpl)->parent === null)
throw new Exceptions\RendererStateException("can not define a block until a parent template has been set");
if ($this->tpl_state($tpl)->current_block !== null)
throw new Exceptions\RendererStateException("current block '{$this->tpl_state($tpl)->current_block}' has not been closed");
if (isset($this->tpl_state($tpl)->blocks[$block_name]))
throw new Exceptions\RendererStateException("can not redefine block '{$block_name}' for template");
// swap to new IgnoredNode for block
assert($this->tpl_state($tpl)->current_block_prev_node === null);
$this->tpl_state($tpl)->current_block_prev_node = $this->rendertree->getCurrentNode();
$node = $this->rendertree->addNode(RenderTree\IgnoredNode::withValue(null));
$this->rendertree->setCurrentNode($node);
// store current block
$this->tpl_state($tpl)->blocks[$block_name] = $node;
$this->tpl_state($tpl)->current_block = $block_name;
// swap to buffer in new node
$was_buffering = $this->swap_to_new_buffer();
assert($was_buffering == true);
}
public function endRenderingBlock(Templates\Base $tpl): void {
if ($this->tpl_state($tpl)->current_block === null)
throw new Exceptions\RendererStateException("not currently rendering a block");
// complete buffer for block
$this->complete_buffer();
// swap back to the node active before the block
$prev_node = $this->tpl_state($tpl)->current_block_prev_node;
assert($prev_node !== null);
$this->rendertree->setCurrentNode($prev_node);
$this->tpl_state($tpl)->current_block_prev_node = null;
// swap to buffer in original node
$this->swap_to_new_buffer();
$this->tpl_state($tpl)->current_block = null;
}
public function renderChildBlock(Templates\Base $tpl, string $block_name): bool {
$child_tpl = $this->tpl_state($tpl)->child;
if ($child_tpl === null)
throw new Exceptions\RendererStateException("template does not have a child set");
if (!array_key_exists($block_name, $this->tpl_state($child_tpl)->blocks))
return false;
$this->complete_buffer();
$this->rendertree->addNode(
RenderTree\Node::fromNode($this->tpl_state($child_tpl)->blocks[$block_name]),
);
$this->swap_to_new_buffer();
return true;
}
public function renderChildContent(Templates\Base $tpl): void {
$child_tpl = $this->tpl_state($tpl)->child;
if ($child_tpl === null)
throw new Exceptions\RendererStateException("template does not have a child set");
$child_root = $this->tpl_state($child_tpl)->child_render_root;
assert($child_root !== null);
$this->complete_buffer();
$this->rendertree->addNode(
RenderTree\Node::fromNode($child_root),
);
$this->swap_to_new_buffer();
}
}