|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Limoncello\Templates; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2019 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use Limoncello\Contracts\Templates\TemplatesInterface; |
|
22
|
|
|
use Limoncello\Templates\Contracts\TemplatesCacheInterface; |
|
23
|
|
|
use Twig\Environment; |
|
24
|
|
|
use Twig\Error\LoaderError; |
|
25
|
|
|
use Twig\Error\RuntimeError; |
|
26
|
|
|
use Twig\Error\SyntaxError; |
|
27
|
|
|
use Twig\Loader\FilesystemLoader; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @package Limoncello\Templates |
|
31
|
|
|
*/ |
|
32
|
|
|
class TwigTemplates implements TemplatesInterface, TemplatesCacheInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Environment |
|
36
|
|
|
*/ |
|
37
|
|
|
private $twig; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $appRootFolder |
|
41
|
|
|
* @param string $templatesFolder |
|
42
|
|
|
* @param null|string $cacheFolder |
|
43
|
|
|
* @param bool $isDebug |
|
44
|
|
|
* @param bool $isAutoReload |
|
45
|
|
|
*/ |
|
46
|
5 |
|
public function __construct( |
|
47
|
|
|
string $appRootFolder, |
|
48
|
|
|
string $templatesFolder, |
|
49
|
|
|
?string $cacheFolder, |
|
50
|
|
|
bool $isDebug, |
|
51
|
|
|
bool $isAutoReload |
|
52
|
|
|
) { |
|
53
|
|
|
// For Twig options see http://twig.sensiolabs.org/doc/api.html |
|
54
|
|
|
$options = [ |
|
55
|
5 |
|
'debug' => $isDebug, |
|
56
|
5 |
|
'cache' => $cacheFolder === null ? false : $cacheFolder, |
|
57
|
5 |
|
'auto_reload' => $isAutoReload, |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
5 |
|
$this->twig = new Environment(new FilesystemLoader($templatesFolder, $appRootFolder), $options); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return Environment |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function getTwig(): Environment |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->twig; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
* @param array $context |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
* |
|
77
|
|
|
* @throws LoaderError |
|
78
|
|
|
* @throws SyntaxError |
|
79
|
|
|
* @throws RuntimeError |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function render(string $name, array $context = []): string |
|
82
|
|
|
{ |
|
83
|
1 |
|
return $this->getTwig()->render($name, $context); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $name |
|
88
|
|
|
* |
|
89
|
|
|
* @throws LoaderError |
|
90
|
|
|
* @throws SyntaxError |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function cache(string $name): void |
|
93
|
|
|
{ |
|
94
|
1 |
|
$this->getTwig()->resolveTemplate($name); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|