TwigExtension.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Config\Resource\FileExistenceResource;
  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@symfony.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. foreach ($configs as $key => $config) {
  36. if (isset($config['globals'])) {
  37. foreach ($config['globals'] as $name => $value) {
  38. if (is_array($value) && isset($value['key'])) {
  39. $configs[$key]['globals'][$name] = array(
  40. 'key' => $name,
  41. 'value' => $value,
  42. );
  43. }
  44. }
  45. }
  46. }
  47. $configuration = $this->getConfiguration($configs, $container);
  48. $config = $this->processConfiguration($configuration, $configs);
  49. $container->setParameter('twig.exception_listener.controller', $config['exception_controller']);
  50. $container->setParameter('twig.form.resources', $config['form_themes']);
  51. $envConfiguratorDefinition = $container->getDefinition('twig.configurator.environment');
  52. $envConfiguratorDefinition->replaceArgument(0, $config['date']['format']);
  53. $envConfiguratorDefinition->replaceArgument(1, $config['date']['interval_format']);
  54. $envConfiguratorDefinition->replaceArgument(2, $config['date']['timezone']);
  55. $envConfiguratorDefinition->replaceArgument(3, $config['number_format']['decimals']);
  56. $envConfiguratorDefinition->replaceArgument(4, $config['number_format']['decimal_point']);
  57. $envConfiguratorDefinition->replaceArgument(5, $config['number_format']['thousands_separator']);
  58. $twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem');
  59. // register user-configured paths
  60. foreach ($config['paths'] as $path => $namespace) {
  61. if (!$namespace) {
  62. $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path));
  63. } else {
  64. $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
  65. }
  66. }
  67. $container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']);
  68. $container->getDefinition('twig.template_iterator')->replaceArgument(2, $config['paths']);
  69. // register bundles as Twig namespaces
  70. foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
  71. $dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views';
  72. if (is_dir($dir)) {
  73. $this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
  74. }
  75. $container->addResource(new FileExistenceResource($dir));
  76. $reflection = new \ReflectionClass($class);
  77. $dir = dirname($reflection->getFileName()).'/Resources/views';
  78. if (is_dir($dir)) {
  79. $this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
  80. }
  81. $container->addResource(new FileExistenceResource($dir));
  82. }
  83. $dir = $container->getParameter('kernel.root_dir').'/Resources/views';
  84. if (is_dir($dir)) {
  85. $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
  86. }
  87. $container->addResource(new FileExistenceResource($dir));
  88. if (!empty($config['globals'])) {
  89. $def = $container->getDefinition('twig');
  90. foreach ($config['globals'] as $key => $global) {
  91. if (isset($global['type']) && 'service' === $global['type']) {
  92. $def->addMethodCall('addGlobal', array($key, new Reference($global['id'])));
  93. } else {
  94. $def->addMethodCall('addGlobal', array($key, $global['value']));
  95. }
  96. }
  97. }
  98. unset(
  99. $config['form'],
  100. $config['globals'],
  101. $config['extensions']
  102. );
  103. if (isset($config['autoescape_service']) && isset($config['autoescape_service_method'])) {
  104. $config['autoescape'] = array(new Reference($config['autoescape_service']), $config['autoescape_service_method']);
  105. }
  106. unset($config['autoescape_service'], $config['autoescape_service_method']);
  107. $container->getDefinition('twig')->replaceArgument(1, $config);
  108. $this->addClassesToCompile(array(
  109. 'Twig_Environment',
  110. 'Twig_Extension',
  111. 'Twig_Extension_Core',
  112. 'Twig_Extension_Escaper',
  113. 'Twig_Extension_Optimizer',
  114. 'Twig_LoaderInterface',
  115. 'Twig_Markup',
  116. 'Twig_Template',
  117. ));
  118. }
  119. private function addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle)
  120. {
  121. $name = $bundle;
  122. if ('Bundle' === substr($name, -6)) {
  123. $name = substr($name, 0, -6);
  124. }
  125. $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir, $name));
  126. }
  127. /**
  128. * Returns the base path for the XSD files.
  129. *
  130. * @return string The XSD base path
  131. */
  132. public function getXsdValidationBasePath()
  133. {
  134. return __DIR__.'/../Resources/config/schema';
  135. }
  136. public function getNamespace()
  137. {
  138. return 'http://symfony.com/schema/dic/twig';
  139. }
  140. }