Passed
Push — develop ( 3e8c8e...6a4b14 )
by Brent
02:58
created

TwigEngine::hasTemplateVariable()   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 1
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\Config;
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
18
    /**
19
     * An array of template variables available when rendering a template.
20
     *
21
     * @var array
22
     */
23
    private $variables = [];
24
25
    /**
26
     * Create a new Twig engine and add the Stitcher specific template functions.
27
     *
28
     * @param string         $templateDir
29
     * @param TemplatePlugin $templatePlugin
30
     */
31
    public function __construct(string $templateDir, TemplatePlugin $templatePlugin) {
32
        $loader = new Twig_Loader_Filesystem($templateDir);
33
34
        parent::__construct($loader, [
35
            'cache' => false,
36
        ]);
37
38
        $this->addFunction(new \Twig_SimpleFunction('meta', [$templatePlugin, 'meta'], ['is_safe' => ['html'],]));
39
        $this->addFunction(new \Twig_SimpleFunction('css', [$templatePlugin, 'css'], ['is_safe' => ['html'],]));
40
        $this->addFunction(new \Twig_SimpleFunction('js', [$templatePlugin, 'js'], ['is_safe' => ['html'],]));
41
        $this->addFunction(new \Twig_SimpleFunction('image', [$templatePlugin, 'image']));
42
        $this->addFunction(new \Twig_SimpleFunction('file', [$templatePlugin, 'file']));
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function renderTemplate(SplFileInfo $template) {
49
        return $this->render($template->getRelativePathname(), $this->variables);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function addTemplateVariables(array $variables) {
56
        $this->variables += $variables;
57
58
        return $this;
59
    }
60
61
    /**
62
     * Check whether a template variable is set
63
     *
64
     * @param string $name
65
     *
66
     * @return bool
67
     */
68
    public function hasTemplateVariable(string $name) : bool {
69
        return isset($this->variables[$name]);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function clearTemplateVariables() {
76
        $this->variables = [];
77
78
        return $this;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function addTemplateVariable($name, $value) {
85
        $this->variables[$name] = $value;
86
87
        return $this;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function clearTemplateVariable($variable) {
94
        unset($this->variables[$variable]);
95
96
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getTemplateExtension() {
103
        return 'html';
104
    }
105
}
106