|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony framework. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * This source file is subject to the MIT license that is bundled |
|
9 * with this source code in the file LICENSE. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Bundle\AsseticBundle\DependencyInjection; |
|
13 |
|
14 use Symfony\Component\Config\FileLocator; |
|
15 use Symfony\Component\Config\Definition\Processor; |
|
16 use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17 use Symfony\Component\DependencyInjection\DefinitionDecorator; |
|
18 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
19 use Symfony\Component\DependencyInjection\Reference; |
|
20 use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
21 |
|
22 /** |
|
23 * Semantic asset configuration. |
|
24 * |
|
25 * @author Kris Wallsmith <kris@symfony.com> |
|
26 */ |
|
27 class AsseticExtension extends Extension |
|
28 { |
|
29 /** |
|
30 * Loads the configuration. |
|
31 * |
|
32 * @param array $configs An array of configuration settings |
|
33 * @param ContainerBuilder $container A ContainerBuilder instance |
|
34 */ |
|
35 public function load(array $configs, ContainerBuilder $container) |
|
36 { |
|
37 $bundles = $container->getParameter('kernel.bundles'); |
|
38 |
|
39 $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
40 $loader->load('assetic.xml'); |
|
41 $loader->load('templating_twig.xml'); |
|
42 $loader->load('templating_php.xml'); |
|
43 |
|
44 $processor = new Processor(); |
|
45 $configuration = new MainConfiguration(array_keys($bundles)); |
|
46 $config = $processor->processConfiguration($configuration, $configs); |
|
47 |
|
48 $container->setParameter('assetic.debug', $config['debug']); |
|
49 $container->setParameter('assetic.use_controller', $config['use_controller']); |
|
50 $container->setParameter('assetic.read_from', $config['read_from']); |
|
51 $container->setParameter('assetic.write_to', $config['write_to']); |
|
52 |
|
53 $container->setParameter('assetic.java.bin', $config['java']); |
|
54 $container->setParameter('assetic.node.bin', $config['node']); |
|
55 $container->setParameter('assetic.sass.bin', $config['sass']); |
|
56 |
|
57 // register formulae |
|
58 $formulae = array(); |
|
59 foreach ($config['assets'] as $name => $formula) { |
|
60 $formulae[$name] = array($formula['inputs'], $formula['filters'], $formula['options']); |
|
61 } |
|
62 |
|
63 if ($formulae) { |
|
64 $container->getDefinition('assetic.config_resource')->replaceArgument(0, $formulae); |
|
65 } else { |
|
66 $container->removeDefinition('assetic.config_loader'); |
|
67 $container->removeDefinition('assetic.config_resource'); |
|
68 } |
|
69 |
|
70 // register filters |
|
71 foreach ($config['filters'] as $name => $filter) { |
|
72 if (isset($filter['resource'])) { |
|
73 $loader->load($container->getParameterBag()->resolveValue($filter['resource'])); |
|
74 unset($filter['resource']); |
|
75 } else { |
|
76 $loader->load('filters/'.$name.'.xml'); |
|
77 } |
|
78 |
|
79 if (isset($filter['file'])) { |
|
80 $container->getDefinition('assetic.filter.'.$name)->setFile($filter['file']); |
|
81 unset($filter['file']); |
|
82 } |
|
83 |
|
84 if (isset($filter['apply_to'])) { |
|
85 if (!is_array($filter['apply_to'])) { |
|
86 $filter['apply_to'] = array($filter['apply_to']); |
|
87 } |
|
88 |
|
89 foreach ($filter['apply_to'] as $i => $pattern) { |
|
90 $worker = new DefinitionDecorator('assetic.worker.ensure_filter'); |
|
91 $worker->replaceArgument(0, '/'.$pattern.'/'); |
|
92 $worker->replaceArgument(1, new Reference('assetic.filter.'.$name)); |
|
93 $worker->addTag('assetic.factory_worker'); |
|
94 |
|
95 $container->setDefinition('assetic.filter.'.$name.'.worker'.$i, $worker); |
|
96 } |
|
97 |
|
98 unset($filter['apply_to']); |
|
99 } |
|
100 |
|
101 foreach ($filter as $key => $value) { |
|
102 $container->setParameter('assetic.filter.'.$name.'.'.$key, $value); |
|
103 } |
|
104 } |
|
105 |
|
106 // twig functions |
|
107 $container->setParameter('assetic.twig_extension.functions', $config['twig']['functions']); |
|
108 |
|
109 // choose dynamic or static |
|
110 if ($container->getParameterBag()->resolveValue($container->getParameterBag()->get('assetic.use_controller'))) { |
|
111 $loader->load('controller.xml'); |
|
112 $container->getDefinition('assetic.helper.dynamic')->addTag('templating.helper', array('alias' => 'assetic')); |
|
113 $container->removeDefinition('assetic.helper.static'); |
|
114 } else { |
|
115 $loader->load('asset_writer.xml'); |
|
116 $container->getDefinition('assetic.helper.static')->addTag('templating.helper', array('alias' => 'assetic')); |
|
117 $container->removeDefinition('assetic.helper.dynamic'); |
|
118 } |
|
119 |
|
120 // bundle and kernel resources |
|
121 foreach ($container->getParameterBag()->resolveValue($config['bundles']) as $bundle) { |
|
122 $rc = new \ReflectionClass($bundles[$bundle]); |
|
123 foreach (array('twig', 'php') as $engine) { |
|
124 $container->setDefinition( |
|
125 'assetic.'.$engine.'_directory_resource.'.$bundle, |
|
126 new DirectoryResourceDefinition($bundle, $engine, array( |
|
127 $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views', |
|
128 dirname($rc->getFileName()).'/Resources/views', |
|
129 )) |
|
130 ); |
|
131 } |
|
132 } |
|
133 foreach (array('twig', 'php') as $engine) { |
|
134 $container->setDefinition( |
|
135 'assetic.'.$engine.'_directory_resource.kernel', |
|
136 new DirectoryResourceDefinition('', $engine, array($container->getParameter('kernel.root_dir').'/Resources/views')) |
|
137 ); |
|
138 } |
|
139 } |
|
140 |
|
141 /** |
|
142 * Returns the base path for the XSD files. |
|
143 * |
|
144 * @return string The XSD base path |
|
145 */ |
|
146 public function getXsdValidationBasePath() |
|
147 { |
|
148 return __DIR__ . '/../Resources/config/schema'; |
|
149 } |
|
150 |
|
151 public function getNamespace() |
|
152 { |
|
153 return 'http://symfony.com/schema/dic/assetic'; |
|
154 } |
|
155 } |