Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
49 / 49 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| TemplateRuntimeController | |
100.00% |
49 / 49 |
|
100.00% |
11 / 11 |
28 | |
100.00% |
1 / 1 |
| getConstructorTestArgs | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| tpl | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| inherit | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| define | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| define_end | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| block | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| primary | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| insert_set | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| insert | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| insert_end | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Computator\FrameworkUtils\PHPTemplate; |
| 4 | |
| 5 | use Computator\FrameworkUtils\PHPTemplate\RenderObjects; |
| 6 | use Computator\FrameworkUtils\PHPTemplate\Exceptions; |
| 7 | use Computator\FrameworkUtils\PHPTemplate\UserApi; |
| 8 | |
| 9 | use ReflectionMethod; |
| 10 | |
| 11 | class TemplateRuntimeController implements UserApi\TemplateClient { |
| 12 | |
| 13 | public static function getConstructorTestArgs(\PHPUnit\Framework\TestCase $tc): array { |
| 14 | $tc_createStub = new ReflectionMethod($tc::class, 'createStub'); |
| 15 | return [ |
| 16 | 'renderer' => $tc_createStub->invoke($tc, UserApi\RenderManager::class), |
| 17 | 'template' => $tc_createStub->invoke($tc, Templates\Base::class), |
| 18 | ]; |
| 19 | } |
| 20 | |
| 21 | public function __construct( |
| 22 | public readonly UserApi\RenderManager $renderer, |
| 23 | protected readonly Templates\Base $template, |
| 24 | ) {} |
| 25 | |
| 26 | public function tpl(string $template): UserApi\ResolvedTemplateClient { |
| 27 | try { |
| 28 | return $this->renderer->getTemplateAsProxy($template); |
| 29 | } catch (Exceptions\TemplateNotFoundException $e) { |
| 30 | return new RenderObjects\Error($this->renderer, $e); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public function inherit(string $parent_template): void { |
| 35 | try { |
| 36 | $this->renderer->setParentForTemplate($this->template, $parent_template); |
| 37 | } catch (Exceptions\RendererStateException $e) { |
| 38 | throw new Exceptions\TemplateLogicException("inherit can not be called more than once", previous: $e); |
| 39 | } catch (Exceptions\RendererException $e) { |
| 40 | throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public function define(string $block_name): void { |
| 45 | try { |
| 46 | $this->renderer->startRenderingBlock($this->template, $block_name); |
| 47 | } catch (Exceptions\RendererStateException $e) { |
| 48 | throw new Exceptions\TemplateLogicException("can't start a new block definition: $e", previous: $e); |
| 49 | } catch (Exceptions\RendererException $e) { |
| 50 | throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public function define_end(): void { |
| 55 | try { |
| 56 | $this->renderer->endRenderingBlock($this->template); |
| 57 | } catch (Exceptions\RendererStateException $e) { |
| 58 | throw new Exceptions\TemplateLogicException("can't end the current block: $e", previous: $e); |
| 59 | } catch (Exceptions\RendererException $e) { |
| 60 | throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public function block(string $block_name): bool { |
| 65 | try { |
| 66 | return $this->renderer->renderChildBlock($this->template, $block_name); |
| 67 | } catch (Exceptions\RendererStateException $e) { |
| 68 | throw new Exceptions\TemplateLogicException("only parent templates can render child template blocks: $e", previous: $e); |
| 69 | } catch (Exceptions\RendererException $e) { |
| 70 | throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public function primary(): void { |
| 75 | try { |
| 76 | $this->renderer->renderChildContent($this->template); |
| 77 | } catch (Exceptions\RendererStateException $e) { |
| 78 | throw new Exceptions\TemplateLogicException("only parent templates can render child content: $e", previous: $e); |
| 79 | } catch (Exceptions\RendererException $e) { |
| 80 | throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public function insert_set(string $insert_name): void { |
| 85 | try { |
| 86 | $this->renderer->setInsertionPoint($insert_name); |
| 87 | } catch (Exceptions\RendererStateException $e) { |
| 88 | throw new Exceptions\TemplateLogicException("insertion point names must be unique: $e", previous: $e); |
| 89 | } catch (Exceptions\RendererException $e) { |
| 90 | throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public function insert(string $insert_name): void { |
| 95 | try { |
| 96 | $this->renderer->startAppendingToInsertionPoint($insert_name); |
| 97 | } catch (Exceptions\RendererStateException $e) { |
| 98 | throw new Exceptions\TemplateLogicException("can't start appending a content block: $e", previous: $e); |
| 99 | } catch (Exceptions\RendererException $e) { |
| 100 | throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public function insert_end(): void { |
| 105 | try { |
| 106 | $this->renderer->stopAppendingToInsertionPoint(); |
| 107 | } catch (Exceptions\RendererStateException $e) { |
| 108 | throw new Exceptions\TemplateLogicException("can't stop appending a content block: $e", previous: $e); |
| 109 | } catch (Exceptions\RendererException $e) { |
| 110 | throw new Exceptions\TemplateRenderException("renderer error: $e", previous: $e); |
| 111 | } |
| 112 | } |
| 113 | } |