|
1
|
|
|
<?php |
|
2
|
|
|
namespace Germania\TwigServiceProvider; |
|
3
|
|
|
|
|
4
|
|
|
use Pimple\Container; |
|
5
|
|
|
use Pimple\ServiceProviderInterface; |
|
6
|
|
|
|
|
7
|
|
|
use \Twig_Loader_Filesystem; |
|
8
|
|
|
use \Twig_Loader_Array; |
|
9
|
|
|
use \Twig_Loader_Chain; |
|
10
|
|
|
use \Twig_Environment; |
|
11
|
|
|
use \Twig_Extension_Debug; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class TwigServiceProvider implements ServiceProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
public $config = array( |
|
19
|
|
|
// For Twig Filesystem Loader |
|
20
|
|
|
// See https://twig.symfony.com/doc/2.x/api.html#twig-loader-filesystem |
|
21
|
|
|
'templates' => 'templates', |
|
22
|
|
|
|
|
23
|
|
|
// Twig Options |
|
24
|
|
|
// See https://twig.symfony.com/doc/2.x/api.html#environment-options |
|
25
|
|
|
'debug' => false, |
|
26
|
|
|
'cache' => 'cache', |
|
27
|
|
|
'auto_reload' => true, |
|
28
|
|
|
'autoescape' => false, |
|
29
|
|
|
'strict_variables' => false |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param array $config Configuration array. |
|
35
|
|
|
*/ |
|
36
|
65 |
|
public function __construct( array $config = array()) |
|
37
|
|
|
{ |
|
38
|
65 |
|
$this->config = array_merge($this->config, $config); |
|
39
|
65 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @implements ServiceProviderInterface |
|
44
|
|
|
*/ |
|
45
|
48 |
|
public function register(Container $dic) |
|
46
|
|
|
{ |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
7 |
|
$dic['Twig.Config'] = function( $dic ) { |
|
|
|
|
|
|
52
|
35 |
|
return $this->config; |
|
53
|
|
|
}; |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
3 |
|
$dic['Twig.CachePath'] = function( $dic ) { |
|
60
|
15 |
|
$template_config = $dic['Twig.Config']; |
|
61
|
15 |
|
return $template_config['cache']; |
|
62
|
|
|
}; |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
4 |
|
$dic['Twig.TemplatePaths'] = function( $dic ) { |
|
69
|
20 |
|
$template_config = $dic['Twig.Config']; |
|
70
|
20 |
|
$templates_paths = $template_config['templates']; |
|
71
|
|
|
|
|
72
|
20 |
|
if (is_string($templates_paths)) { |
|
73
|
20 |
|
$templates_paths = array( $templates_paths ); |
|
74
|
4 |
|
} |
|
75
|
|
|
|
|
76
|
20 |
|
return $templates_paths; |
|
77
|
|
|
}; |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
3 |
|
$dic['Twig.Loaders'] = function( $dic ) { |
|
84
|
15 |
|
$templates_paths = $dic['Twig.TemplatePaths']; |
|
85
|
|
|
|
|
86
|
|
|
return [ |
|
87
|
15 |
|
new Twig_Loader_Filesystem( $templates_paths ) |
|
88
|
3 |
|
]; |
|
89
|
|
|
}; |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
2 |
|
$dic['Twig.Options'] = function( $dic ) { |
|
98
|
10 |
|
$template_config = $dic['Twig.Config']; |
|
99
|
10 |
|
$cache_path = $dic['Twig.CachePath']; |
|
100
|
|
|
|
|
101
|
|
|
return [ |
|
102
|
10 |
|
'cache' => $cache_path, |
|
103
|
10 |
|
'auto_reload' => $template_config['auto_reload'], |
|
104
|
10 |
|
'autoescape' => $template_config['autoescape'], |
|
105
|
10 |
|
'debug' => $template_config['debug'], |
|
106
|
10 |
|
'strict_variables' => $template_config['strict_variables'] |
|
107
|
2 |
|
]; |
|
108
|
|
|
}; |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
2 |
|
$dic['Twig.Globals'] = function( $dic ) { |
|
|
|
|
|
|
116
|
10 |
|
return array(); |
|
117
|
|
|
}; |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
2 |
|
$dic['Twig.Filters'] = function( $dic ) { |
|
|
|
|
|
|
124
|
10 |
|
return array(); |
|
125
|
|
|
}; |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
2 |
|
$dic['Twig.Tests'] = function( $dic ) { |
|
|
|
|
|
|
132
|
10 |
|
return array(); |
|
133
|
|
|
}; |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
2 |
|
$dic['Twig.Functions'] = function( $dic ) { |
|
|
|
|
|
|
139
|
10 |
|
return array(); |
|
140
|
|
|
}; |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
2 |
|
$dic['Twig.Extensions'] = function($dic) { |
|
|
|
|
|
|
146
|
|
|
return [ |
|
147
|
8 |
|
new Twig_Extension_Debug |
|
148
|
2 |
|
]; |
|
149
|
|
|
}; |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return Twig_Loader_Chain |
|
158
|
|
|
*/ |
|
159
|
2 |
|
$dic['Twig.LoaderChain'] = function($dic) { |
|
160
|
10 |
|
$loaders = $dic['Twig.Loaders']; |
|
161
|
10 |
|
return new Twig_Loader_Chain( $loaders ); |
|
162
|
|
|
}; |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return Twig_Environment |
|
167
|
|
|
*/ |
|
168
|
2 |
|
$dic['Twig'] = function( $dic ) { |
|
169
|
|
|
|
|
170
|
|
|
// ---- 1. Instantiate Twig ----- |
|
171
|
5 |
|
$twig_loader_chain = $dic['Twig.LoaderChain']; |
|
172
|
5 |
|
$twig_options = $dic['Twig.Options']; |
|
173
|
5 |
|
$twig = new Twig_Environment($twig_loader_chain, $twig_options); |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
// ---- 2. Configure Extras ----- |
|
177
|
|
|
|
|
178
|
|
|
// Add Twig_Extensions |
|
179
|
5 |
|
$extensions = $dic['Twig.Extensions']; |
|
180
|
5 |
|
foreach( $extensions as $ext ): |
|
181
|
5 |
|
$twig->addExtension( $ext ); |
|
182
|
1 |
|
endforeach; |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
// Add Template Globals for Twig |
|
186
|
5 |
|
$globals = $dic['Twig.Globals']; |
|
187
|
5 |
|
foreach( $globals as $name => $value ): |
|
188
|
|
|
$twig->addGlobal( $name, $value ); |
|
189
|
1 |
|
endforeach; |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
// Add Twig_Filters |
|
193
|
5 |
|
$filters = $dic['Twig.Filters']; |
|
194
|
5 |
|
foreach( $filters as $filter ): |
|
195
|
|
|
$twig->addFilter( $filter ); |
|
196
|
1 |
|
endforeach; |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
// Add Tests |
|
200
|
5 |
|
$tests = $dic['Twig.Tests']; |
|
201
|
5 |
|
foreach( $tests as $test ): |
|
202
|
|
|
$twig->addTest( $test ); |
|
203
|
1 |
|
endforeach; |
|
204
|
|
|
|
|
205
|
|
|
|
|
206
|
|
|
// Add Functions |
|
207
|
5 |
|
$functions = $dic['Twig.Functions']; |
|
208
|
5 |
|
foreach( $functions as $fn ): |
|
209
|
|
|
$twig->addFunction( $fn ); |
|
210
|
1 |
|
endforeach; |
|
211
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
// ---- 3. Return Twig_Environment |
|
214
|
5 |
|
return $twig; |
|
215
|
|
|
}; |
|
216
|
|
|
|
|
217
|
60 |
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.