Passed
Push — master ( 55ec05...cfe8d0 )
by Brent
04:42 queued 02:05
created

TwigEngine::getTemplateExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Template\Twig;
4
5
use Brendt\Stitcher\Lib\Browser;
6
use Brendt\Stitcher\Template\TemplateEngine;
7
use Brendt\Stitcher\Template\TemplatePlugin;
8
use Symfony\Component\Finder\SplFileInfo;
9
use Twig_Environment;
10
use Twig_Loader_Filesystem;
11
12
/**
13
 * The Twig template engine.
14
 */
15
class TwigEngine extends Twig_Environment implements TemplateEngine
16
{
17
    private $variables = [];
18
19
    public function __construct(Browser $browser, TemplatePlugin $templatePlugin) {
20
        $loader = new Twig_Loader_Filesystem($browser->getTemplateDir());
21
22
        parent::__construct($loader, [
23
            'cache' => false,
24
        ]);
25
26
        $this->addFunction(new \Twig_SimpleFunction('meta', [$templatePlugin, 'meta'], ['is_safe' => ['html'],]));
27
        $this->addFunction(new \Twig_SimpleFunction('css', [$templatePlugin, 'css'], ['is_safe' => ['html'],]));
28
        $this->addFunction(new \Twig_SimpleFunction('js', [$templatePlugin, 'js'], ['is_safe' => ['html'],]));
29
        $this->addFunction(new \Twig_SimpleFunction('image', [$templatePlugin, 'image']));
30
        $this->addFunction(new \Twig_SimpleFunction('file', [$templatePlugin, 'file']));
31
    }
32
33
    public function renderTemplate(SplFileInfo $template) {
34
        return $this->render($template->getRelativePathname(), $this->variables);
35
    }
36
37
    public function addTemplateVariables(array $variables) {
38
        $this->variables += $variables;
39
40
        return $this;
41
    }
42
43
    public function hasTemplateVariable(string $name) : bool {
44
        return isset($this->variables[$name]);
45
    }
46
47
    public function clearTemplateVariables() {
48
        $this->variables = [];
49
50
        return $this;
51
    }
52
53
    public function addTemplateVariable($name, $value) {
54
        $this->variables[$name] = $value;
55
56
        return $this;
57
    }
58
59
    public function clearTemplateVariable($variable) {
60
        unset($this->variables[$variable]);
61
62
        return $this;
63
    }
64
65
    public function getTemplateExtensions(): array {
66
        return ['html', 'twig'];
67
    }
68
}
69