|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace fkooman\RemoteStorage; |
|
20
|
|
|
|
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
use Twig_Environment; |
|
23
|
|
|
use Twig_Loader_Filesystem; |
|
24
|
|
|
|
|
25
|
|
|
class TwigTpl implements TplInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var Twig_Environment */ |
|
28
|
|
|
private $twig; |
|
29
|
|
|
|
|
30
|
|
|
/** @var array */ |
|
31
|
|
|
private $defaultVariables; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create TwigTemplateManager. |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $templateDirs template directories to look in where later |
|
37
|
|
|
* paths override the earlier paths |
|
38
|
|
|
* @param string $cacheDir the writable directory to store the cache |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(array $templateDirs, $cacheDir = null) |
|
41
|
|
|
{ |
|
42
|
|
|
$existingTemplateDirs = []; |
|
43
|
|
|
foreach ($templateDirs as $templateDir) { |
|
44
|
|
|
if (false !== is_dir($templateDir)) { |
|
45
|
|
|
$existingTemplateDirs[] = $templateDir; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
$existingTemplateDirs = array_reverse($existingTemplateDirs); |
|
49
|
|
|
|
|
50
|
|
|
$environmentOptions = [ |
|
51
|
|
|
'strict_variables' => true, |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
if (null !== $cacheDir) { |
|
55
|
|
|
if (false === is_dir($cacheDir)) { |
|
56
|
|
|
if (false === @mkdir($cacheDir, 0700, true)) { |
|
57
|
|
|
throw new RuntimeException('unable to create template cache directory'); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
$environmentOptions['cache'] = $cacheDir; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->twig = new Twig_Environment( |
|
64
|
|
|
new Twig_Loader_Filesystem( |
|
65
|
|
|
$existingTemplateDirs |
|
66
|
|
|
), |
|
67
|
|
|
$environmentOptions |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$this->defaultVariables = []; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function setDefault(array $templateVariables) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->defaultVariables = $templateVariables; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function addDefault(array $templateVariables) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->defaultVariables = array_merge( |
|
81
|
|
|
$this->defaultVariables, $templateVariables |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Render the template. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $templateName the name of the template |
|
89
|
|
|
* @param array $templateVariables the variables to be used in the |
|
90
|
|
|
* template |
|
91
|
|
|
* |
|
92
|
|
|
* @return string the rendered template |
|
93
|
|
|
*/ |
|
94
|
|
|
public function render($templateName, array $templateVariables) |
|
95
|
|
|
{ |
|
96
|
|
|
$templateVariables = array_merge($this->defaultVariables, $templateVariables); |
|
97
|
|
|
|
|
98
|
|
|
return $this->twig->render( |
|
99
|
|
|
sprintf( |
|
100
|
|
|
'%s.twig', |
|
101
|
|
|
$templateName |
|
102
|
|
|
), |
|
103
|
|
|
$templateVariables |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|