TwigTemplateEngine   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 23
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 2 1
A __construct() 0 7 2
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