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