1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cecil/Cecil package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cecil\Renderer; |
12
|
|
|
|
13
|
|
|
use Cecil\Builder; |
14
|
|
|
use Cecil\Renderer\Twig\Extension as TwigExtension; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Twig. |
18
|
|
|
*/ |
19
|
|
|
class Twig implements RendererInterface |
20
|
|
|
{ |
21
|
|
|
/** @var \Twig\Environment */ |
22
|
|
|
protected $twig; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $templatesDir; |
26
|
|
|
|
27
|
|
|
/** @var \Twig\Profiler\Profile */ |
28
|
|
|
public $profile; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Builder $builder, $templatesPath) |
34
|
|
|
{ |
35
|
|
|
// load layouts |
36
|
|
|
$loader = new \Twig\Loader\FilesystemLoader($templatesPath); |
37
|
|
|
// default options |
38
|
|
|
$loaderOptions = [ |
39
|
|
|
'debug' => $builder->isDebug(), |
40
|
|
|
'strict_variables' => true, |
41
|
|
|
'autoescape' => false, |
42
|
|
|
'auto_reload' => true, |
43
|
|
|
'cache' => false, |
44
|
|
|
]; |
45
|
|
|
// use Twig cache? |
46
|
|
|
if ($builder->getConfig()->get('cache.templates.enabled')) { |
47
|
|
|
$templatesCachePath = \Cecil\Util::joinFile( |
48
|
|
|
$builder->getConfig()->getCachePath(), |
49
|
|
|
(string) $builder->getConfig()->get('cache.templates.dir') |
50
|
|
|
); |
51
|
|
|
$loaderOptions = array_replace($loaderOptions, ['cache' => $templatesCachePath]); |
52
|
|
|
} |
53
|
|
|
// create the Twig instance |
54
|
|
|
$this->twig = new \Twig\Environment($loader, $loaderOptions); |
55
|
|
|
// set date format |
56
|
|
|
$this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
57
|
|
|
->setDateFormat($builder->getConfig()->get('date.format')); |
58
|
|
|
// set timezone |
59
|
|
|
if ($builder->getConfig()->has('date.timezone')) { |
60
|
|
|
$this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
61
|
|
|
->setTimezone($builder->getConfig()->get('date.timezone')); |
62
|
|
|
} |
63
|
|
|
// adds extensions |
64
|
|
|
$this->twig->addExtension(new TwigExtension($builder)); |
65
|
|
|
$this->twig->addExtension(new \Twig\Extension\StringLoaderExtension()); |
66
|
|
|
// internationalisation |
67
|
|
|
if (extension_loaded('intl')) { |
68
|
|
|
$this->twig->addExtension(new \Twig\Extensions\IntlExtension()); |
69
|
|
|
$builder->getLogger()->debug('Intl extension is loaded'); |
70
|
|
|
} |
71
|
|
|
if (extension_loaded('gettext')) { |
72
|
|
|
$this->twig->addExtension(new \Twig\Extensions\I18nExtension()); |
73
|
|
|
$builder->getLogger()->debug('Gettext extension is loaded'); |
74
|
|
|
} |
75
|
|
|
if ($builder->isDebug()) { |
76
|
|
|
// dump() |
77
|
|
|
$this->twig->addExtension(new \Twig\Extension\DebugExtension()); |
78
|
|
|
// profiler |
79
|
|
|
$this->profile = new \Twig\Profiler\Profile(); |
80
|
|
|
$this->twig->addExtension(new \Twig\Extension\ProfilerExtension($this->profile)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function addGlobal(string $name, $value): void |
88
|
|
|
{ |
89
|
|
|
$this->twig->addGlobal($name, $value); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function render(string $template, array $variables): string |
96
|
|
|
{ |
97
|
|
|
return $this->twig->render($template, $variables); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|