Completed
Push — master ( c686d8...1d0354 )
by Mehmet
05:00
created

Twig   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 45
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A extendAndAddGlobals() 0 6 1
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
8
class Twig implements ViewInterface
9
{
10
    private $twig;
11
    private $config = [
12
        'templates_dir' => '',
13
        'cache_dir'     => '',
14
        'debug'         => true,
15
        'auto_reload'   => true
16
17
    ];
18 9
    public function __construct(array $config, array $queryParams)
19
    {
20 9
        $this->config = array_merge($this->config,$config);
21 9
        $loader     = new \Twig_Loader_Filesystem($this->config['templates_dir']);
22 9
        $this->twig = new \Twig_Environment($loader, [
23 9
            'cache'         => $this->config['cache_dir'],
24 9
            'debug'         => $this->config['debug'],
25 9
            'auto_reload'   => $this->config['auto_reload']
26
        ]);
27 9
        $this->extendAndAddGlobals($queryParams);
28 9
    }
29
30 9
    private function extendAndAddGlobals(array $queryParams) {
31
32 9
        new TwigExtensions($this->twig, $this->config);
33 9
        $this->twig->addGlobal('_RC', $this->config); // Runtime Config values
34 9
        $this->twig->addGlobal('_QP', $queryParams); // Query Parameters ($_REQUEST)
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
35 9
    }
36
37 2
    public function addGlobal(string $name, $value)
38
    {
39 2
        $this->twig->addGlobal($name, $value);
40 2
    }
41
42 9
    public function render(string $templateFile, array $parameters=[])
43
    {
44 9
        $output = $this->twig->render($templateFile, $parameters);
45 8
        preg_match('/{{(\s*)Widget\_/msi', $output, $matches);
46 8
        if (isset($matches[1])) {
47 1
            $template = $this->twig->createTemplate($output);
48 1
            $output = $template->render([]);
49
        }
50 8
        return $output;
51
    }
52
}