-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
TwigExtension.php
142 lines (126 loc) · 4.83 KB
/
TwigExtension.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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\TwigBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Config\FileLocator;
/**
* TwigExtension.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class TwigExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('twig.xml');
$this->addClassesToCompile(array(
'Twig_Environment',
'Twig_ExtensionInterface',
'Twig_Extension',
'Twig_Extension_Core',
'Twig_Extension_Escaper',
'Twig_Extension_Optimizer',
'Twig_LoaderInterface',
'Twig_Markup',
'Twig_TemplateInterface',
'Twig_Template',
));
foreach ($configs as $config) {
$this->doConfigLoad($config, $container);
}
}
/**
* Loads the Twig configuration.
*
* @param array $config An array of configuration settings
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function doConfigLoad(array $config, ContainerBuilder $container)
{
// form resources
foreach (array('resources', 'resource') as $key) {
if (isset($config['form'][$key])) {
$resources = (array) $config['form'][$key];
$container->setParameter('twig.form.resources', array_merge($container->getParameter('twig.form.resources'), $resources));
unset($config['form'][$key]);
}
}
// globals
$def = $container->getDefinition('twig');
$globals = $this->normalizeConfig($config, 'global');
if (isset($globals[0])) {
foreach ($globals as $global) {
if (isset($global['type']) && 'service' === $global['type']) {
$def->addMethodCall('addGlobal', array($global['key'], new Reference($global['id'])));
} elseif (isset($global['value'])) {
$def->addMethodCall('addGlobal', array($global['key'], $global['value']));
} else {
throw new \InvalidArgumentException(sprintf('Unable to understand global configuration (%s).', var_export($global, true)));
}
}
} else {
foreach ($globals as $key => $value) {
if (is_string($value) && '@' === substr($value, 0, 1)) {
$def->addMethodCall('addGlobal', array($key, new Reference(substr($value, 1))));
} else {
$def->addMethodCall('addGlobal', array($key, $value));
}
}
}
unset($config['globals'], $config['global']);
// extensions
$extensions = $this->normalizeConfig($config, 'extension');
if (isset($extensions[0]) && is_array($extensions[0])) {
foreach ($extensions as $extension) {
$container->getDefinition($extension['id'])->addTag('twig.extension');
}
} else {
foreach ($extensions as $id) {
$container->getDefinition($id)->addTag('twig.extension');
}
}
unset($config['extensions'], $config['extension']);
// convert - to _
foreach ($config as $key => $value) {
if (false !== strpos($key, '-')) {
unset($config[$key]);
$config[str_replace('-', '_', $key)] = $value;
}
}
if (isset($config['cache-warmer'])) {
$config['cache_warmer'] = $config['cache-warmer'];
}
if (isset($config['cache_warmer']) && $config['cache_warmer']) {
$container->getDefinition('templating.cache_warmer.templates_cache')->addTag('kernel.cache_warmer');
}
$container->setParameter('twig.options', array_replace($container->getParameter('twig.options'), $config));
}
/**
* Returns the base path for the XSD files.
*
* @return string The XSD base path
*/
public function getXsdValidationBasePath()
{
return __DIR__.'/../Resources/config/schema';
}
public function getNamespace()
{
return 'http://www.symfony-project.org/schema/dic/twig';
}
public function getAlias()
{
return 'twig';
}
}