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
|
|
|
/** @var string */ |
24
|
|
|
protected $templatesDir; |
25
|
|
|
/** @var \Twig\Profiler\Profile */ |
26
|
|
|
public $profile; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Builder $builder, $templatesPath) |
32
|
|
|
{ |
33
|
|
|
// load layouts |
34
|
|
|
$loader = new \Twig\Loader\FilesystemLoader($templatesPath); |
35
|
|
|
// Twig |
36
|
|
|
$loaderOptions = [ |
37
|
|
|
'debug' => getenv('CECIL_DEBUG') == 'true' ? true : false, |
38
|
|
|
'strict_variables' => true, |
39
|
|
|
'autoescape' => false, |
40
|
|
|
'cache' => false, |
41
|
|
|
'auto_reload' => true, |
42
|
|
|
]; |
43
|
|
|
// Trying to optimize Twig rendering time |
44
|
|
|
$performance = true; |
45
|
|
|
$cachePath = \Cecil\Util::joinFile($builder->getConfig()->getCachePath(), 'templates'); |
46
|
|
|
if ($performance) { |
|
|
|
|
47
|
|
|
$loaderOptions = array_replace($loaderOptions, ['cache' => $cachePath]); |
48
|
|
|
} |
49
|
|
|
$this->twig = new \Twig\Environment($loader, $loaderOptions); |
50
|
|
|
// set date format & timezone |
51
|
|
|
$this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
52
|
|
|
->setDateFormat($builder->getConfig()->get('date.format')); |
|
|
|
|
53
|
|
|
$this->twig->getExtension(\Twig\Extension\CoreExtension::class) |
54
|
|
|
->setTimezone($builder->getConfig()->get('date.timezone')); |
|
|
|
|
55
|
|
|
// adds extensions |
56
|
|
|
$this->twig->addExtension(new TwigExtension($builder)); |
57
|
|
|
$this->twig->addExtension(new \Twig\Extension\StringLoaderExtension()); |
58
|
|
|
// internationalisation |
59
|
|
|
if (extension_loaded('intl')) { |
60
|
|
|
$this->twig->addExtension(new \Twig_Extensions_Extension_Intl()); |
61
|
|
|
} |
62
|
|
|
if (extension_loaded('gettext')) { |
63
|
|
|
$this->twig->addExtension(new \Twig_Extensions_Extension_I18n()); |
64
|
|
|
} |
65
|
|
|
if (getenv('CECIL_DEBUG') == 'true') { |
66
|
|
|
// dump() |
67
|
|
|
$this->twig->addExtension(new \Twig\Extension\DebugExtension()); |
68
|
|
|
// profiler |
69
|
|
|
$this->profile = new \Twig\Profiler\Profile(); |
70
|
|
|
$this->twig->addExtension(new \Twig\Extension\ProfilerExtension($this->profile)); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function addGlobal(string $name, $value): void |
78
|
|
|
{ |
79
|
|
|
$this->twig->addGlobal($name, $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function render(string $template, array $variables): string |
86
|
|
|
{ |
87
|
|
|
return $this->twig->render($template, $variables); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|