This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTwigServiceProvider.php
189 lines (152 loc) · 7.12 KB
/
TwigServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Provider\Twig\RuntimeLoader;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Bridge\Twig\Extension\AssetExtension;
use Symfony\Bridge\Twig\Extension\DumpExtension;
use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Extension\SecurityExtension;
use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
use Symfony\Bridge\Twig\Extension\WebLinkExtension;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
use Symfony\Component\Form\FormRenderer;
/**
* Twig integration for Silex.
*
* @author Fabien Potencier <[email protected]>
*/
class TwigServiceProvider implements ServiceProviderInterface
{
public function register(Container $app)
{
$app['twig.options'] = [];
$app['twig.form.templates'] = ['form_div_layout.html.twig'];
$app['twig.path'] = [];
$app['twig.templates'] = [];
$app['twig.date.format'] = 'F j, Y H:i';
$app['twig.date.interval_format'] = '%d days';
$app['twig.date.timezone'] = null;
$app['twig.number_format.decimals'] = 0;
$app['twig.number_format.decimal_point'] = '.';
$app['twig.number_format.thousands_separator'] = ',';
$app['twig'] = function ($app) {
$twig = $app['twig.environment_factory']($app);
// registered for BC, but should not be used anymore
// deprecated and should probably be removed in Silex 3.0
$twig->addGlobal('app', $app);
$coreExtension = $twig->getExtension('Twig_Extension_Core');
$coreExtension->setDateFormat($app['twig.date.format'], $app['twig.date.interval_format']);
if (null !== $app['twig.date.timezone']) {
$coreExtension->setTimezone($app['twig.date.timezone']);
}
$coreExtension->setNumberFormat($app['twig.number_format.decimals'], $app['twig.number_format.decimal_point'], $app['twig.number_format.thousands_separator']);
if ($app['debug']) {
$twig->addExtension(new \Twig_Extension_Debug());
}
if (class_exists('Symfony\Bridge\Twig\Extension\RoutingExtension')) {
$app['twig.app_variable'] = function ($app) {
$var = new AppVariable();
if (isset($app['security.token_storage'])) {
$var->setTokenStorage($app['security.token_storage']);
}
if (isset($app['request_stack'])) {
$var->setRequestStack($app['request_stack']);
}
$var->setDebug($app['debug']);
return $var;
};
$twig->addGlobal('global', $app['twig.app_variable']);
if (isset($app['request_stack'])) {
$twig->addExtension(new HttpFoundationExtension($app['request_stack']));
$twig->addExtension(new RoutingExtension($app['url_generator']));
$twig->addExtension(new WebLinkExtension($app['request_stack']));
}
if (isset($app['translator'])) {
$twig->addExtension(new TranslationExtension($app['translator']));
}
if (isset($app['security.authorization_checker'])) {
$twig->addExtension(new SecurityExtension($app['security.authorization_checker']));
}
if (isset($app['fragment.handler'])) {
$app['fragment.renderer.hinclude']->setTemplating($twig);
$twig->addExtension(new HttpKernelExtension($app['fragment.handler']));
}
if (isset($app['assets.packages'])) {
$twig->addExtension(new AssetExtension($app['assets.packages']));
}
if (isset($app['form.factory'])) {
$app['twig.form.engine'] = function ($app) use ($twig) {
return new TwigRendererEngine($app['twig.form.templates'], $twig);
};
$app['twig.form.renderer'] = function ($app) {
$csrfTokenManager = isset($app['csrf.token_manager']) ? $app['csrf.token_manager'] : null;
return new FormRenderer($app['twig.form.engine'], $csrfTokenManager);
};
$twig->addExtension(new FormExtension());
// add loader for Symfony built-in form templates
$reflected = new \ReflectionClass('Symfony\Bridge\Twig\Extension\FormExtension');
$path = dirname($reflected->getFileName()).'/../Resources/views/Form';
$app['twig.loader']->addLoader(new \Twig_Loader_Filesystem($path));
}
if (isset($app['var_dumper.cloner'])) {
$twig->addExtension(new DumpExtension($app['var_dumper.cloner']));
}
$twig->addRuntimeLoader($app['twig.runtime_loader']);
}
return $twig;
};
$app['twig.loader.filesystem'] = function ($app) {
$loader = new \Twig_Loader_Filesystem();
foreach (is_array($app['twig.path']) ? $app['twig.path'] : [$app['twig.path']] as $key => $val) {
if (is_string($key)) {
$loader->addPath($key, $val);
} else {
$loader->addPath($val);
}
}
return $loader;
};
$app['twig.loader.array'] = function ($app) {
return new \Twig_Loader_Array($app['twig.templates']);
};
$app['twig.loader'] = function ($app) {
return new \Twig_Loader_Chain([
$app['twig.loader.array'],
$app['twig.loader.filesystem'],
]);
};
$app['twig.environment_factory'] = $app->protect(function ($app) {
return new \Twig_Environment($app['twig.loader'], array_replace([
'charset' => $app['charset'],
'debug' => $app['debug'],
'strict_variables' => $app['debug'],
], $app['twig.options']));
});
$app['twig.runtime.httpkernel'] = function ($app) {
return new HttpKernelRuntime($app['fragment.handler']);
};
$app['twig.runtimes'] = function ($app) {
return [
HttpKernelRuntime::class => 'twig.runtime.httpkernel',
FormRenderer::class => 'twig.form.renderer',
];
};
$app['twig.runtime_loader'] = function ($app) {
return new RuntimeLoader($app, $app['twig.runtimes']);
};
}
}