-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateRuntimeController.php
More file actions
83 lines (71 loc) · 3.08 KB
/
Copy pathTemplateRuntimeController.php
File metadata and controls
83 lines (71 loc) · 3.08 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
<?php declare(strict_types=1);
namespace Computator\FrameworkUtils\PHPTemplate;
use Computator\FrameworkUtils\PHPTemplate\RenderObjects;
use Computator\FrameworkUtils\PHPTemplate\Exceptions;
use Computator\FrameworkUtils\PHPTemplate\UserApi;
use ReflectionMethod;
class TemplateRuntimeController implements UserApi\TemplateClient {
public static function getConstructorTestArgs(\PHPUnit\Framework\TestCase $tc): array {
$tc_createStub = new ReflectionMethod($tc::class, 'createStub');
return [
'renderer' => $tc_createStub->invoke($tc, UserApi\RenderManager::class),
'template' => $tc_createStub->invoke($tc, Templates\Base::class),
];
}
public function __construct(
public readonly UserApi\RenderManager $renderer,
protected readonly Templates\Base $template,
) {}
public function tpl(string $template): UserApi\ResolvedTemplateClient {
try {
return $this->renderer->getTemplateAsProxy($template);
} catch (Exceptions\TemplateNotFoundException $e) {
return new RenderObjects\Error($this->renderer, $e);
}
}
public function inherit(string $parent_template): void {
try {
$this->renderer->setParentForTemplate($this->template, $parent_template);
} catch (Exceptions\RendererStateException $e) {
throw new Exceptions\TemplateLogicException("inherit can not be called more than once", previous: $e);
} catch (Exceptions\RendererException $e) {
throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e);
}
}
public function define(string $block_name): void {
try {
$this->renderer->startRenderingBlock($this->template, $block_name);
} catch (Exceptions\RendererStateException $e) {
throw new Exceptions\TemplateLogicException("can't start a new block definition: $e", previous: $e);
} catch (Exceptions\RendererException $e) {
throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e);
}
}
public function define_end(): void {
try {
$this->renderer->endRenderingBlock($this->template);
} catch (Exceptions\RendererStateException $e) {
throw new Exceptions\TemplateLogicException("can't end the current block: $e", previous: $e);
} catch (Exceptions\RendererException $e) {
throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e);
}
}
public function block(string $block_name): bool {
try {
return $this->renderer->renderChildBlock($this->template, $block_name);
} catch (Exceptions\RendererStateException $e) {
throw new Exceptions\TemplateLogicException("only parent templates can render child template blocks: $e", previous: $e);
} catch (Exceptions\RendererException $e) {
throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e);
}
}
public function primary(): void {
try {
$this->renderer->renderChildContent($this->template);
} catch (Exceptions\RendererStateException $e) {
throw new Exceptions\TemplateLogicException("only parent templates can render child content: $e", previous: $e);
} catch (Exceptions\RendererException $e) {
throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e);
}
}
}