Test Failed
Push — master ( a58c94...1bcec0 )
by Brent
03:26
created

TwigEngine::addTemplateVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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