AbstractRender::withLoader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\UI\Renders;
19
20
use Biurad\UI\Exceptions\RenderException;
21
use Biurad\UI\Interfaces\HtmlInterface;
22
use Biurad\UI\Interfaces\RenderInterface;
23
use Biurad\UI\Template;
24
25
/**
26
 * Render engine with ability to switch environment and loader.
27
 *
28
 * @author Divine Niiquaye Ibok <[email protected]>
29
 */
30
abstract class AbstractRender implements RenderInterface
31
{
32
    protected const EXTENSIONS = [];
33
34
    /** @var string[] */
35
    protected $extensions;
36
37
    /** @var Template|null */
38
    protected $loader;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 6
    public function withLoader(Template $loader): RenderInterface
44
    {
45 6
        $this->loader = $loader;
46
47 6
        return $this;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function withExtensions(string ...$extensions): void
54
    {
55
        foreach ($extensions as $extension) {
56
            $this->extensions[] = $extension;
57
        }
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 6
    public function getExtensions(): array
64
    {
65 6
        return \array_unique($this->extensions);
66
    }
67
68
    /**
69
     * Load a php html template file.
70
     */
71 6
    protected static function loadHtml(string $file): ?string
72
    {
73 6
        if (!\str_starts_with($file, 'html:')) {
74 5
            return null;
75
        }
76
77 1
        $level = \ob_get_level();
78 1
        \ob_start(function () {
79 1
            return '';
80 1
        });
81
82
        try {
83 1
            $templateContent = require($file = \substr($file, 5));
84 1
            \ob_end_clean();
85
86 1
            if (!$templateContent instanceof HtmlInterface) {
87 1
                throw new RenderException(\sprintf('Could not render template file "%s" as it does not return a "%s" instance.', $file, HtmlInterface::class));
88
            }
89
90 1
            $content = (string) $templateContent;
91 1
        } catch (\Throwable $e) {
92 1
            while (\ob_get_level() > $level) {
93
                \ob_end_clean();
94
            }
95
96 1
            throw $e;
97
        }
98
99 1
        return $content;
100
    }
101
}
102