Template::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Tleckie\Template;
4
5
use RuntimeException;
6
use Tleckie\Template\Compiler\Compiler;
7
use Tleckie\Template\Compiler\CompilerInterface;
8
use Tleckie\Template\Compiler\Path;
9
use Tleckie\Template\Compiler\PathInterface;
10
use function extract;
11
use function ob_get_clean;
12
use function ob_start;
13
14
/**
15
 * Class Template
16
 *
17
 * @package Tleckie\Template
18
 * @author  Teodoro Leckie Westberg <[email protected]>
19
 */
20
class Template implements TemplateInterface
21
{
22
    /** @var PathInterface */
23
    private PathInterface $templatePath;
24
25
    /** @var PathInterface */
26
    private PathInterface $compiledPath;
27
28
    /** @var CompilerInterface */
29
    private CompilerInterface $compiler;
30
31
    /** @var array */
32
    private array $helpers = [];
33
34
    /** @var bool */
35
    private bool $developmentMode;
36
37
    /**
38
     * Template constructor.
39
     *
40
     * @param string                 $templatePath
41
     * @param string                 $compilePath
42
     * @param CompilerInterface|null $compiler
43
     * @param bool                   $developmentMode
44
     */
45
    public function __construct(
46
        string $templatePath,
47
        string $compilePath,
48
        CompilerInterface $compiler = null,
49
        bool $developmentMode = false
50
    ) {
51
        $this->templatePath = new Path($templatePath);
52
        $this->compiledPath = new Path($compilePath);
53
        $this->compiler = $compiler ?? new Compiler();
54
        $this->developmentMode = $developmentMode;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function developmentMode(): bool
61
    {
62
        return $this->developmentMode;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function compiler(): CompilerInterface
69
    {
70
        return $this->compiler;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function setTemplatePath(PathInterface $path): TemplateInterface
77
    {
78
        $this->templatePath = $path;
79
80
        return $this;
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function setCompiledPath(PathInterface $path): TemplateInterface
87
    {
88
        $this->compiledPath = $path;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96
    public function registerHelpers(array $helpers): TemplateInterface
97
    {
98
        foreach ($helpers as $alias => $helper) {
99
            $this->registerHelper($alias, $helper);
100
        }
101
102
        return $this;
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function registerHelper(string $alias, object $helper): TemplateInterface
109
    {
110
        $this->helpers[$alias] = $helper;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function helpers(): array
119
    {
120
        return $this->helpers;
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public function __get(string $helperAlias): object
127
    {
128
        if (!isset($this->helpers[$helperAlias])) {
129
            throw new RuntimeException(sprintf('Helper [%s] not found.', $helperAlias));
130
        }
131
132
        return $this->helpers[$helperAlias];
133
    }
134
135
    /**
136
     * @inheritdoc
137
     */
138
    public function render(string $template, array $context = []): string
139
    {
140
        $this->checkCompiler($template);
141
142
        ob_start();
143
144
        $this->managerTemplate($context);
145
146
        return ob_get_clean();
147
    }
148
149
    /**
150
     * @param string $template
151
     * @throws RuntimeException
152
     */
153
    private function checkCompiler(string $template): void
154
    {
155
        $this->compiler
156
            ->setTemplatePath($this->templatePath->withFile($template))
157
            ->setCompiledPath($this->compiledPath->withFile(sprintf("%s.compiled", $template)));
158
159
        if (($this->developmentMode || $this->compiler->needsCompile()) && !$this->compiler->compile()) {
160
            throw new RuntimeException('Compiler failed');
161
        }
162
    }
163
164
    /**
165
     * @param array $context
166
     */
167
    private function managerTemplate(array $context): void
168
    {
169
        extract($context);
170
171
        include $this->compiler->getCompiledPath()->getFullPath();
172
    }
173
174
    /**
175
     * @inheritdoc
176
     */
177
    public function flushCompiled(): bool
178
    {
179
        return $this->compiledPath->deleteDir();
180
    }
181
}
182