PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

https://gitlab.com/mohamedchiheb.bida/workshopFOS
PHP | 153 lines | 102 code | 24 blank | 27 comment | 16 complexity | ebc192fb780d2bfbc8ab299c2f4d0c4f MD5 | raw file
  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 $key => $config) {
  35. if (isset($config['globals'])) {
  36. foreach ($config['globals'] as $name => $value) {
  37. if (is_array($value) && isset($value['key'])) {
  38. $configs[$key]['globals'][$name] = array(
  39. 'key' => $name,
  40. 'value' => $value,
  41. );
  42. }
  43. }
  44. }
  45. }
  46. $configuration = $this->getConfiguration($configs, $container);
  47. $config = $this->processConfiguration($configuration, $configs);
  48. $container->setParameter('twig.exception_listener.controller', $config['exception_controller']);
  49. $container->setParameter('twig.form.resources', $config['form_themes']);
  50. $twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.filesystem');
  51. // register user-configured paths
  52. foreach ($config['paths'] as $path => $namespace) {
  53. if (!$namespace) {
  54. $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path));
  55. } else {
  56. $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
  57. }
  58. }
  59. // register bundles as Twig namespaces
  60. foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
  61. if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views')) {
  62. $this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
  63. }
  64. $reflection = new \ReflectionClass($class);
  65. if (is_dir($dir = dirname($reflection->getFileName()).'/Resources/views')) {
  66. $this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
  67. }
  68. }
  69. if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) {
  70. $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
  71. }
  72. if (!empty($config['globals'])) {
  73. $def = $container->getDefinition('twig');
  74. foreach ($config['globals'] as $key => $global) {
  75. if (isset($global['type']) && 'service' === $global['type']) {
  76. $def->addMethodCall('addGlobal', array($key, new Reference($global['id'])));
  77. } else {
  78. $def->addMethodCall('addGlobal', array($key, $global['value']));
  79. }
  80. }
  81. }
  82. unset(
  83. $config['form'],
  84. $config['globals'],
  85. $config['extensions']
  86. );
  87. if ($container->getParameter('kernel.debug')) {
  88. $loader->load('debug.xml');
  89. $container->setDefinition('templating.engine.twig', $container->findDefinition('debug.templating.engine.twig'));
  90. $container->setAlias('debug.templating.engine.twig', 'templating.engine.twig');
  91. }
  92. if (isset($config['autoescape_service']) && isset($config['autoescape_service_method'])) {
  93. $config['autoescape'] = array(new Reference($config['autoescape_service']), $config['autoescape_service_method']);
  94. }
  95. unset($config['autoescape_service'], $config['autoescape_service_method']);
  96. $container->setParameter('twig.options', $config);
  97. $this->addClassesToCompile(array(
  98. 'Twig_Environment',
  99. 'Twig_Extension',
  100. 'Twig_Extension_Core',
  101. 'Twig_Extension_Escaper',
  102. 'Twig_Extension_Optimizer',
  103. 'Twig_LoaderInterface',
  104. 'Twig_Markup',
  105. 'Twig_Template',
  106. ));
  107. }
  108. private function addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle)
  109. {
  110. $name = $bundle;
  111. if ('Bundle' === substr($name, -6)) {
  112. $name = substr($name, 0, -6);
  113. }
  114. $twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir, $name));
  115. }
  116. /**
  117. * Returns the base path for the XSD files.
  118. *
  119. * @return string The XSD base path
  120. */
  121. public function getXsdValidationBasePath()
  122. {
  123. return __DIR__.'/../Resources/config/schema';
  124. }
  125. public function getNamespace()
  126. {
  127. return 'http://symfony.com/schema/dic/twig';
  128. }
  129. }