1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Blitz PHP framework. |
5
|
|
|
* |
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\View\Adapters; |
13
|
|
|
|
14
|
|
|
use Twig\Environment; |
|
|
|
|
15
|
|
|
use Twig\Loader\FilesystemLoader; |
|
|
|
|
16
|
|
|
use Twig\TwigFilter; |
|
|
|
|
17
|
|
|
use Twig\TwigFunction; |
|
|
|
|
18
|
|
|
|
19
|
|
|
class TwigAdapter extends AbstractAdapter |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
*/ |
24
|
|
|
protected string $ext = 'twig'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Instance Twig |
28
|
|
|
* |
29
|
|
|
* @var Environment |
30
|
|
|
*/ |
31
|
|
|
private $engine; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
*/ |
36
|
|
|
public function __construct(protected array $config, $viewPath = null, protected bool $debug = BLITZ_DEBUG) |
37
|
|
|
{ |
38
|
|
|
parent::__construct($config, $viewPath, $debug); |
39
|
|
|
|
40
|
|
|
$loader = new FilesystemLoader([ |
41
|
|
|
$this->viewPath, |
42
|
|
|
VIEW_PATH . 'partials', |
43
|
|
|
LAYOUT_PATH, |
44
|
|
|
]); |
45
|
|
|
$this->engine = new Environment($loader); |
46
|
|
|
|
47
|
|
|
$this->configure(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Rend une vue en associant le layout si celui ci est defini |
52
|
|
|
* |
53
|
|
|
* @param string|null $template |
54
|
|
|
* @param mixed $cache_id |
55
|
|
|
* @param mixed $compile_id |
56
|
|
|
* @param mixed $parent |
57
|
|
|
*/ |
58
|
|
|
public function render(string $view, ?array $options = null, ?bool $saveData = null): string |
59
|
|
|
{ |
60
|
|
|
$view = str_replace([$this->viewPath, ' '], '', $view); |
61
|
|
|
$pathinfo = pathinfo($view, PATHINFO_EXTENSION); |
62
|
|
|
if ($pathinfo === [] || $pathinfo === '' || $pathinfo === '0') { |
63
|
|
|
$view .= '.' . $this->ext; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->renderVars['start'] = microtime(true); |
67
|
|
|
|
68
|
|
|
$this->renderVars['view'] = $view; |
69
|
|
|
$this->renderVars['options'] = $options ?? []; |
70
|
|
|
|
71
|
|
|
$this->renderVars['file'] = $this->getRenderedFile($options, $this->renderVars['view'], $this->ext); |
72
|
|
|
|
73
|
|
|
$output = $this->engine->render($this->renderVars['view'], $this->data); |
74
|
|
|
|
75
|
|
|
$this->logPerformance($this->renderVars['start'], microtime(true), $this->renderVars['view']); |
|
|
|
|
76
|
|
|
|
77
|
|
|
return $this->decorate($output); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Configure le moteur de template |
82
|
|
|
*/ |
83
|
|
|
private function configure(): void |
84
|
|
|
{ |
85
|
|
|
if (isset($this->config['configure']) && is_callable($this->config['configure'])) { |
86
|
|
|
$newInstance = $this->config['configure']($this->engine); |
87
|
|
|
if ($newInstance instanceof Environment) { |
88
|
|
|
$this->engine = $newInstance; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$autoReload = $this->config['auto_reload'] ?: 'auto'; |
93
|
|
|
if ('auto' === $autoReload) { |
94
|
|
|
$autoReload = on_dev(); |
95
|
|
|
} |
96
|
|
|
if (true === $autoReload) { |
97
|
|
|
$this->engine->enableAutoReload(); |
98
|
|
|
} else { |
99
|
|
|
$this->engine->disableAutoReload(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$debug = $this->config['debug'] ?: 'auto'; |
103
|
|
|
if ('auto' === $debug) { |
104
|
|
|
$debug = on_dev(); |
105
|
|
|
} |
106
|
|
|
if (true === $debug) { |
107
|
|
|
$this->engine->enableDebug(); |
108
|
|
|
} else { |
109
|
|
|
$this->engine->disableDebug(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$strictVariables = $this->config['strict_variables'] ?: 'auto'; |
113
|
|
|
if ('auto' === $strictVariables) { |
114
|
|
|
$strictVariables = on_dev(); |
115
|
|
|
} |
116
|
|
|
if (true === $strictVariables) { |
117
|
|
|
$this->engine->enableStrictVariables(); |
118
|
|
|
} else { |
119
|
|
|
$this->engine->disableStrictVariables(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->engine->setCharset($this->config['charset'] ?: config('app.charset')); |
123
|
|
|
|
124
|
|
|
$this->engine->setCache($this->config['cache_dir'] ?: VIEW_CACHE_PATH . 'twig'); |
125
|
|
|
|
126
|
|
|
// Ajout des variables globals |
127
|
|
|
$globals = (array) ($this->config['globals'] ?? []); |
128
|
|
|
|
129
|
|
|
foreach ($globals as $name => $global) { |
130
|
|
|
$this->engine->addGlobal($name, $global); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Ajout des filtres |
134
|
|
|
$filters = (array) ($this->config['filters'] ?? []); |
135
|
|
|
|
136
|
|
|
foreach ($filters as $filter) { |
137
|
|
|
if ($filter instanceof TwigFilter) { |
138
|
|
|
$this->engine->addFilter($filter); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// Ajout des fonctions |
143
|
|
|
$functions = (array) ($this->config['functions'] ?? []); |
144
|
|
|
|
145
|
|
|
foreach ($functions as $function) { |
146
|
|
|
if ($function instanceof TwigFunction) { |
147
|
|
|
$this->engine->addFunction($function); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths