1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Charcoal\View\Twig; |
6
|
|
|
|
7
|
|
|
// From Twig |
8
|
|
|
use Twig_Environment; |
9
|
|
|
|
10
|
|
|
// From 'charcoal-view' |
11
|
|
|
use Charcoal\View\AbstractEngine; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class TwigEngine extends AbstractEngine |
17
|
|
|
{ |
18
|
|
|
const DEFAULT_CACHE_PATH = '../cache/twig'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Twig_Environment $twig |
22
|
|
|
*/ |
23
|
|
|
private $twig; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function type(): string |
29
|
|
|
{ |
30
|
|
|
return 'twig'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return Twig_Environment |
35
|
|
|
*/ |
36
|
|
|
public function twig(): Twig_Environment |
37
|
|
|
{ |
38
|
|
|
if ($this->twig === null) { |
39
|
|
|
$this->twig = $this->createTwig(); |
40
|
|
|
} |
41
|
|
|
return $this->twig; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $templateIdent The template identifier to load and render. |
46
|
|
|
* @param mixed $context The rendering context. |
47
|
|
|
* @return string The rendered template string. |
48
|
|
|
*/ |
49
|
|
|
public function render(string $templateIdent, $context): string |
50
|
|
|
{ |
51
|
|
|
$arrayContext = json_decode(json_encode($context), true); |
52
|
|
|
return $this->twig()->render($templateIdent, $arrayContext); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $templateString The template string to render. |
57
|
|
|
* @param mixed $context The rendering context. |
58
|
|
|
* @return string The rendered template string. |
59
|
|
|
*/ |
60
|
|
|
public function renderTemplate(string $templateString, $context): string |
61
|
|
|
{ |
62
|
|
|
$template = $this->twig()->createTemplate($templateString); |
63
|
|
|
$arrayContext = json_decode(json_encode($context), true); |
64
|
|
|
return $template->render($arrayContext); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return Twig_Environment |
69
|
|
|
*/ |
70
|
|
|
protected function createTwig(): Twig_Environment |
71
|
|
|
{ |
72
|
|
|
$twig = new Twig_Environment($this->loader(), [ |
|
|
|
|
73
|
|
|
'cache' => $this->cache(), |
74
|
|
|
'charset' => 'utf-8', |
75
|
|
|
'debug' => false |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
return $twig; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set the engine's cache implementation. |
83
|
|
|
* |
84
|
|
|
* @param mixed $cache A Twig cache option. |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function setCache($cache): void |
88
|
|
|
{ |
89
|
|
|
/** |
90
|
|
|
* If NULL is specified, the value is converted to FALSE |
91
|
|
|
* because Twig internally requires FALSE to disable the cache. |
92
|
|
|
*/ |
93
|
|
|
if ($cache === null) { |
94
|
|
|
$cache = false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
parent::setCache($cache); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: