TwigExtension.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Symfony\Bundle\TwigBundle\DependencyInjection;
  3. use Symfony\Component\DependencyInjection\Extension\Extension;
  4. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  5. use Symfony\Component\DependencyInjection\ContainerBuilder;
  6. use Symfony\Component\DependencyInjection\Reference;
  7. /*
  8. * This file is part of the Symfony framework.
  9. *
  10. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. /**
  16. * TwigExtension.
  17. *
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  19. */
  20. class TwigExtension extends Extension
  21. {
  22. /**
  23. * Loads the Twig configuration.
  24. *
  25. * @param array $config An array of configuration settings
  26. * @param ContainerBuilder $container A ContainerBuilder instance
  27. */
  28. public function configLoad(array $config, ContainerBuilder $container)
  29. {
  30. if (!$container->hasDefinition('twig')) {
  31. $loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
  32. $loader->load('twig.xml');
  33. }
  34. // form resources
  35. foreach (array('resources', 'resource') as $key) {
  36. if (isset($config['form'][$key])) {
  37. $resources = (array) $config['form'][$key];
  38. $container->setParameter('twig.form.resources', array_merge($container->getParameter('twig.form.resources'), $resources));
  39. unset($config['form'][$key]);
  40. }
  41. }
  42. // globals
  43. $def = $container->getDefinition('twig');
  44. $globals = $this->fixConfig($config, 'global');
  45. if (isset($globals[0])) {
  46. foreach ($globals as $global) {
  47. if (isset($global['type']) && 'service' === $global['type']) {
  48. $def->addMethodCall('addGlobal', array($global['key'], new Reference($global['id'])));
  49. } elseif (isset($global['value'])) {
  50. $def->addMethodCall('addGlobal', array($global['key'], $global['value']));
  51. } else {
  52. throw new \InvalidArgumentException(sprintf('Unable to understand global configuration (%s).', var_export($global, true)));
  53. }
  54. }
  55. } else {
  56. foreach ($globals as $key => $value) {
  57. if ('@' === substr($value, 0, 1)) {
  58. $def->addMethodCall('addGlobal', array($key, new Reference(substr($value, 1))));
  59. } else {
  60. $def->addMethodCall('addGlobal', array($key, $value));
  61. }
  62. }
  63. }
  64. unset($config['globals'], $config['global']);
  65. // extensions
  66. $extensions = $this->fixConfig($config, 'extension');
  67. if (isset($extensions[0]) && is_array($extensions[0])) {
  68. foreach ($extensions as $extension) {
  69. $container->getDefinition($extension['id'])->addTag('twig.extension');
  70. }
  71. } else {
  72. foreach ($extensions as $id) {
  73. $container->getDefinition($id)->addTag('twig.extension');
  74. }
  75. }
  76. unset($config['extensions'], $config['extension']);
  77. // convert - to _
  78. foreach ($config as $key => $value) {
  79. if (false !== strpos($key, '-')) {
  80. unset($config[$key]);
  81. $config[str_replace('-', '_', $key)] = $value;
  82. }
  83. }
  84. $container->setParameter('twig.options', array_replace($container->getParameter('twig.options'), $config));
  85. }
  86. /**
  87. * Returns the base path for the XSD files.
  88. *
  89. * @return string The XSD base path
  90. */
  91. public function getXsdValidationBasePath()
  92. {
  93. return __DIR__.'/../Resources/config/schema';
  94. }
  95. public function getNamespace()
  96. {
  97. return 'http://www.symfony-project.org/schema/dic/twig';
  98. }
  99. public function getAlias()
  100. {
  101. return 'twig';
  102. }
  103. protected function fixConfig($config, $key)
  104. {
  105. $values = array();
  106. if (isset($config[$key.'s'])) {
  107. $values = $config[$key.'s'];
  108. } elseif (isset($config[$key])) {
  109. if (is_string($config[$key]) || !is_int(key($config[$key]))) {
  110. // only one
  111. $values = array($config[$key]);
  112. } else {
  113. $values = $config[$key];
  114. }
  115. }
  116. return $values;
  117. }
  118. }