AsseticExtension.php 6.3KB

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