/
AsseticExtension.php
160 lines (136 loc) · 5.82 KB
/
AsseticExtension.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
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* Semantic asset configuration.
*
* @author Kris Wallsmith <kris.wallsmith@symfony.com>
*/
class AsseticExtension extends Extension
{
/**
* Loads the configuration.
*
* @param array $configs An array of configuration settings
* @param ContainerBuilder $container A ContainerBuilder instance
*/
public function load(array $configs, ContainerBuilder $container)
{
$parameterBag = $container->getParameterBag();
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('assetic.xml');
$loader->load('templating_twig.xml');
$loader->load('templating_php.xml');
$config = self::processConfigs($configs, $container->getParameter('kernel.debug'), array_keys($container->getParameter('kernel.bundles')));
$container->setParameter('assetic.debug', $config['debug']);
$container->setParameter('assetic.use_controller', $config['use_controller']);
$container->setParameter('assetic.read_from', $config['read_from']);
$container->setParameter('assetic.write_to', $config['write_to']);
$container->setParameter('assetic.java.bin', $config['java']);
$container->setParameter('assetic.node.bin', $config['node']);
$container->setParameter('assetic.sass.bin', $config['sass']);
// register filters
foreach ($config['filters'] as $name => $filter) {
if (isset($filter['resource'])) {
$loader->load($parameterBag->resolveValue($filter['resource']));
unset($filter['resource']);
} else {
$loader->load('filters/'.$name.'.xml');
}
foreach ($filter as $key => $value) {
$container->setParameter('assetic.filter.'.$name.'.'.$key, $value);
}
}
// choose dynamic or static
if ($parameterBag->resolveValue($parameterBag->get('assetic.use_controller'))) {
$loader->load('controller.xml');
$container->setParameter('assetic.twig_extension.class', '%assetic.twig_extension.dynamic.class%');
} else {
$loader->load('asset_writer.xml');
$container->setParameter('assetic.twig_extension.class', '%assetic.twig_extension.static.class%');
}
// register config resources
self::registerFormulaResources($container, $parameterBag->resolveValue($config['bundles']));
}
static protected function processConfigs(array $configs, $debug, array $bundles)
{
$configuration = new Configuration();
$tree = $configuration->getConfigTree($debug, $bundles);
$processor = new Processor();
return $processor->process($tree, $configs);
}
static protected function registerFormulaResources(ContainerBuilder $container, array $bundles)
{
$map = $container->getParameter('kernel.bundles');
if ($diff = array_diff($bundles, array_keys($map))) {
throw new \InvalidArgumentException(sprintf('The following bundles are not registered: "%s"', implode('", "', $diff)));
}
$am = $container->getDefinition('assetic.asset_manager');
// bundle views/ directories
foreach ($bundles as $name) {
$rc = new \ReflectionClass($map[$name]);
if (is_dir($dir = dirname($rc->getFileName()).'/Resources/views')) {
// FIXME: must also look for templates in %kernel.root_dir%/Resources/%bundle%/views
foreach (array('twig', 'php') as $engine) {
$container->setDefinition(
'assetic.'.$engine.'_directory_resource.'.$name,
self::createDirectoryResourceDefinition($name, $dir, $engine)
);
}
}
}
// kernel views/ directory
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) {
foreach (array('twig', 'php') as $engine) {
$container->setDefinition(
'assetic.'.$engine.'_directory_resource.kernel',
self::createDirectoryResourceDefinition('', $dir, $engine)
);
}
}
}
/**
* @todo decorate an abstract xml definition
*/
static protected function createDirectoryResourceDefinition($bundle, $dir, $engine)
{
$definition = new Definition('%assetic.directory_resource.class%');
$definition
->addArgument(new Reference('templating.loader'))
->addArgument($bundle)
->addArgument($dir)
->addArgument('/^[^.]+\.[^.]+\.'.$engine.'$/')
->addTag('assetic.templating.'.$engine)
->addTag('assetic.formula_resource', array('loader' => $engine))
->setPublic(false)
;
return $definition;
}
/**
* 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://symfony.com/schema/dic/assetic';
}
}