TwigExtension.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\TwigBundle\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\Reference;
  15. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  16. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  17. /**
  18. * TwigExtension.
  19. *
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. * @author Jeremy Mikola <jmikola@gmail.com>
  22. */
  23. class TwigExtension extends Extension
  24. {
  25. /**
  26. * Responds to the twig configuration parameter.
  27. *
  28. * @param array $configs
  29. * @param ContainerBuilder $container
  30. */
  31. public function load(array $configs, ContainerBuilder $container)
  32. {
  33. $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  34. $loader->load('twig.xml');
  35. $processor = new Processor();
  36. $configuration = new Configuration();
  37. $config = $processor->process($configuration->getConfigTree(), $configs);
  38. $container->setParameter('twig.form.resources', $config['form']['resources']);
  39. if (!empty($config['globals'])) {
  40. $def = $container->getDefinition('twig');
  41. foreach ($config['globals'] as $key => $global) {
  42. if (isset($global['type']) && 'service' === $global['type']) {
  43. $def->addMethodCall('addGlobal', array($key, new Reference($global['id'])));
  44. } else {
  45. $def->addMethodCall('addGlobal', array($key, $global['value']));
  46. }
  47. }
  48. }
  49. if (!empty($config['extensions'])) {
  50. foreach ($config['extensions'] as $id) {
  51. $container->getDefinition($id)->addTag('twig.extension');
  52. }
  53. }
  54. if (!empty($config['cache_warmer'])) {
  55. $container->getDefinition('templating.cache_warmer.templates_cache')->addTag('kernel.cache_warmer');
  56. }
  57. unset(
  58. $config['form'],
  59. $config['globals'],
  60. $config['extensions'],
  61. $config['cache_warmer']
  62. );
  63. $container->setParameter('twig.options', $config);
  64. $this->addClassesToCompile(array(
  65. 'Twig_Environment',
  66. 'Twig_ExtensionInterface',
  67. 'Twig_Extension',
  68. 'Twig_Extension_Core',
  69. 'Twig_Extension_Escaper',
  70. 'Twig_Extension_Optimizer',
  71. 'Twig_LoaderInterface',
  72. 'Twig_Markup',
  73. 'Twig_TemplateInterface',
  74. 'Twig_Template',
  75. ));
  76. }
  77. /**
  78. * Returns the base path for the XSD files.
  79. *
  80. * @return string The XSD base path
  81. */
  82. public function getXsdValidationBasePath()
  83. {
  84. return __DIR__.'/../Resources/config/schema';
  85. }
  86. public function getNamespace()
  87. {
  88. return 'http://www.symfony-project.org/schema/dic/twig';
  89. }
  90. public function getAlias()
  91. {
  92. return 'twig';
  93. }
  94. }