AsseticExtension.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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']);
  43. $container->setParameter('assetic.read_from', $config['read_from']);
  44. $container->setParameter('assetic.write_to', $config['write_to']);
  45. $container->setParameter('assetic.java.bin', $config['java']);
  46. $container->setParameter('assetic.node.bin', $config['node']);
  47. $container->setParameter('assetic.sass.bin', $config['sass']);
  48. // register formulae
  49. $formulae = array();
  50. foreach ($config['assets'] as $name => $formula) {
  51. $formulae[$name] = array($formula['inputs'], $formula['filters'], $formula['options']);
  52. }
  53. if ($formulae) {
  54. $container->getDefinition('assetic.config_resource')->replaceArgument(0, $formulae);
  55. } else {
  56. $container->removeDefinition('assetic.config_loader');
  57. $container->removeDefinition('assetic.config_resource');
  58. }
  59. // register filters
  60. foreach ($config['filters'] as $name => $filter) {
  61. if (isset($filter['resource'])) {
  62. $loader->load($container->getParameterBag()->resolveValue($filter['resource']));
  63. unset($filter['resource']);
  64. } else {
  65. $loader->load('filters/'.$name.'.xml');
  66. }
  67. if (isset($filter['file'])) {
  68. $container->getDefinition('assetic.filter.'.$name)->setFile($filter['file']);
  69. unset($filter['file']);
  70. }
  71. if (isset($filter['apply_to'])) {
  72. if (!is_array($filter['apply_to'])) {
  73. $filter['apply_to'] = array($filter['apply_to']);
  74. }
  75. foreach ($filter['apply_to'] as $i => $pattern) {
  76. $worker = new DefinitionDecorator('assetic.worker.ensure_filter');
  77. $worker->replaceArgument(0, '/'.$pattern.'/');
  78. $worker->replaceArgument(1, new Reference('assetic.filter.'.$name));
  79. $worker->addTag('assetic.factory_worker');
  80. $container->setDefinition('assetic.filter.'.$name.'.worker'.$i, $worker);
  81. }
  82. unset($filter['apply_to']);
  83. }
  84. foreach ($filter as $key => $value) {
  85. $container->setParameter('assetic.filter.'.$name.'.'.$key, $value);
  86. }
  87. }
  88. // twig functions
  89. $container->setParameter('assetic.twig_extension.functions', $config['twig']['functions']);
  90. // choose dynamic or static
  91. if ($container->getParameterBag()->resolveValue($container->getParameterBag()->get('assetic.use_controller'))) {
  92. $loader->load('controller.xml');
  93. $container->getDefinition('assetic.helper.dynamic')->addTag('templating.helper', array('alias' => 'assetic'));
  94. $container->removeDefinition('assetic.helper.static');
  95. } else {
  96. $loader->load('asset_writer.xml');
  97. $container->getDefinition('assetic.helper.static')->addTag('templating.helper', array('alias' => 'assetic'));
  98. $container->removeDefinition('assetic.helper.dynamic');
  99. }
  100. // bundle and kernel resources
  101. foreach ($container->getParameterBag()->resolveValue($config['bundles']) as $bundle) {
  102. $rc = new \ReflectionClass($bundles[$bundle]);
  103. foreach (array('twig', 'php') as $engine) {
  104. $container->setDefinition(
  105. 'assetic.'.$engine.'_directory_resource.'.$bundle,
  106. new DirectoryResourceDefinition($bundle, $engine, array(
  107. $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views',
  108. dirname($rc->getFileName()).'/Resources/views',
  109. ))
  110. );
  111. }
  112. }
  113. foreach (array('twig', 'php') as $engine) {
  114. $container->setDefinition(
  115. 'assetic.'.$engine.'_directory_resource.kernel',
  116. new DirectoryResourceDefinition('', $engine, array($container->getParameter('kernel.root_dir').'/Resources/views'))
  117. );
  118. }
  119. }
  120. /**
  121. * Returns the base path for the XSD files.
  122. *
  123. * @return string The XSD base path
  124. */
  125. public function getXsdValidationBasePath()
  126. {
  127. return __DIR__ . '/../Resources/config/schema';
  128. }
  129. public function getNamespace()
  130. {
  131. return 'http://symfony.com/schema/dic/assetic';
  132. }
  133. }