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) |
|
|
|
|
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
|
|
|
} |
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.