Twig   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 43
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A viewFactory() 0 5 1
A addGlobals() 0 8 3
A addGlobal() 0 4 1
A render() 0 10 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Selami\View\Twig;
5
6
use Selami\View\ViewInterface;
7
use Psr\Container\ContainerInterface;
8
use \Twig\Environment as TwigEnvironment;
9
10
class Twig implements ViewInterface
11
{
12
    private $twig;
13
    private $config;
14
    public function __construct(TwigEnvironment $twig, array $config)
15
    {
16
        $this->config = $config;
17
        $this->twig = $twig;
18
        $this->addGlobals();
19
        new TwigExtensions($this->twig, $this->config);
20
    }
21
22
    public static function viewFactory(ContainerInterface $container, array $config) : ViewInterface
23
    {
24
        $twig = $container->get(TwigEnvironment::class);
25
        return new static($twig, $config);
26
    }
27
28
    private function addGlobals() : void
29
    {
30
        if (array_key_exists('runtime', $this->config)) {
31
            foreach ($this->config['runtime'] as $key => $value) {
32
                $this->twig->addGlobal($key, $value);
33
            }
34
        }
35
    }
36
37
    public function addGlobal(string $name, $value) : void
38
    {
39
        $this->twig->addGlobal($name, $value);
40
    }
41
42
    public function render(string $templateFile, array $parameters = []) : string
43
    {
44
        $output = $this->twig->render($templateFile, $parameters);
45
        preg_match('/{{(\s*)Widget\_/mi', $output, $matches);
46
        if (isset($matches[1])) {
47
            $template = $this->twig->createTemplate($output);
48
            $output = $template->render([]);
49
        }
50
        return $output;
51
    }
52
}
53