TwigTemplateEngine::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 7
rs 10
1
<?php declare(strict_types=1);
2
namespace html_go\templating;
3
4
use Twig\Environment;
5
use Twig\Loader\FilesystemLoader;
6
7
final class TwigTemplateEngine implements TemplateEngine
8
{
9
    private Environment $engine;
10
    private string $ext;
11
12
    /**
13
     * TwigTemplateEngine Constructor.
14
     * @param array<mixed> $templateDirs
15
     * @param array<mixed> $options
16
     * @param string $ext the template file extension. Default is <code>twig</code>.
17
     * @param array<mixed> $globals
18
     */
19
    public function __construct(array $templateDirs, array $options, string $ext = 'twig', array $globals = []) {
20
        $loader = new FilesystemLoader($templateDirs);
21
        $this->engine = new Environment($loader, $options);
22
        foreach ($globals as $name => $value) {
23
            $this->engine->addGlobal($name, $value);
24
        }
25
        $this->ext = $ext;
26
    }
27
28
    public function render($template, array $vars): string {
29
        return $this->engine->render($template.'.'.$this->ext, $vars);
30
    }
31
}
32