TwigExtension.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  15. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  16. /**
  17. * TwigExtension.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. * @author Jeremy Mikola <jmikola@gmail.com>
  21. */
  22. class TwigExtension extends Extension
  23. {
  24. /**
  25. * Responds to the twig configuration parameter.
  26. *
  27. * @param array $configs
  28. * @param ContainerBuilder $container
  29. */
  30. public function load(array $configs, ContainerBuilder $container)
  31. {
  32. $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  33. $loader->load('twig.xml');
  34. foreach ($configs as &$config) {
  35. if (isset($config['globals'])) {
  36. foreach ($config['globals'] as $name => $value) {
  37. if (is_array($value) && isset($value['key'])) {
  38. $config['globals'][$name] = array(
  39. 'key' => $name,
  40. 'value' => $config['globals'][$name]
  41. );
  42. }
  43. }
  44. }
  45. }
  46. $configuration = new Configuration();
  47. $config = $this->processConfiguration($configuration, $configs);
  48. $container->setParameter('twig.exception_listener.controller', $config['exception_controller']);
  49. $container->setParameter('twig.form.resources', $config['form']['resources']);
  50. $reflClass = new \ReflectionClass('Symfony\Bridge\Twig\Extension\FormExtension');
  51. $container->getDefinition('twig.loader')->addMethodCall('addPath', array(dirname(dirname($reflClass->getFileName()))));
  52. if (!empty($config['globals'])) {
  53. $def = $container->getDefinition('twig');
  54. foreach ($config['globals'] as $key => $global) {
  55. if (isset($global['type']) && 'service' === $global['type']) {
  56. $def->addMethodCall('addGlobal', array($key, new Reference($global['id'])));
  57. } else {
  58. $def->addMethodCall('addGlobal', array($key, $global['value']));
  59. }
  60. }
  61. }
  62. unset(
  63. $config['form'],
  64. $config['globals'],
  65. $config['extensions']
  66. );
  67. $container->setParameter('twig.options', $config);
  68. $this->addClassesToCompile(array(
  69. 'Twig_Environment',
  70. 'Twig_ExtensionInterface',
  71. 'Twig_Extension',
  72. 'Twig_Extension_Core',
  73. 'Twig_Extension_Escaper',
  74. 'Twig_Extension_Optimizer',
  75. 'Twig_LoaderInterface',
  76. 'Twig_Markup',
  77. 'Twig_TemplateInterface',
  78. 'Twig_Template',
  79. ));
  80. }
  81. /**
  82. * Returns the base path for the XSD files.
  83. *
  84. * @return string The XSD base path
  85. */
  86. public function getXsdValidationBasePath()
  87. {
  88. return __DIR__.'/../Resources/config/schema';
  89. }
  90. public function getNamespace()
  91. {
  92. return 'http://symfony.com/schema/dic/twig';
  93. }
  94. }