twig.php 7.2 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\Component\DependencyInjection\Loader\Configurator;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Bridge\Twig\AppVariable;
  13. use Symfony\Bridge\Twig\DataCollector\TwigDataCollector;
  14. use Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer;
  15. use Symfony\Bridge\Twig\Extension\AssetExtension;
  16. use Symfony\Bridge\Twig\Extension\CodeExtension;
  17. use Symfony\Bridge\Twig\Extension\ExpressionExtension;
  18. use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
  19. use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
  20. use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
  21. use Symfony\Bridge\Twig\Extension\ProfilerExtension;
  22. use Symfony\Bridge\Twig\Extension\RoutingExtension;
  23. use Symfony\Bridge\Twig\Extension\StopwatchExtension;
  24. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  25. use Symfony\Bridge\Twig\Extension\WebLinkExtension;
  26. use Symfony\Bridge\Twig\Extension\WorkflowExtension;
  27. use Symfony\Bridge\Twig\Extension\YamlExtension;
  28. use Symfony\Bridge\Twig\Translation\TwigExtractor;
  29. use Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer;
  30. use Symfony\Bundle\TwigBundle\DependencyInjection\Configurator\EnvironmentConfigurator;
  31. use Symfony\Bundle\TwigBundle\TemplateIterator;
  32. use Twig\Cache\FilesystemCache;
  33. use Twig\Environment;
  34. use Twig\Extension\CoreExtension;
  35. use Twig\Extension\DebugExtension;
  36. use Twig\Extension\EscaperExtension;
  37. use Twig\Extension\OptimizerExtension;
  38. use Twig\Extension\StagingExtension;
  39. use Twig\ExtensionSet;
  40. use Twig\Loader\ChainLoader;
  41. use Twig\Loader\FilesystemLoader;
  42. use Twig\Profiler\Profile;
  43. use Twig\RuntimeLoader\ContainerRuntimeLoader;
  44. use Twig\Template;
  45. use Twig\TemplateWrapper;
  46. return static function (ContainerConfigurator $container) {
  47. $container->services()
  48. ->set('twig', Environment::class)
  49. ->public()
  50. ->args([service('twig.loader'), abstract_arg('Twig options')])
  51. ->call('addGlobal', ['app', service('twig.app_variable')])
  52. ->call('addRuntimeLoader', [service('twig.runtime_loader')])
  53. ->configurator([service('twig.configurator.environment'), 'configure'])
  54. ->tag('container.preload', ['class' => FilesystemCache::class])
  55. ->tag('container.preload', ['class' => CoreExtension::class])
  56. ->tag('container.preload', ['class' => EscaperExtension::class])
  57. ->tag('container.preload', ['class' => OptimizerExtension::class])
  58. ->tag('container.preload', ['class' => StagingExtension::class])
  59. ->tag('container.preload', ['class' => ExtensionSet::class])
  60. ->tag('container.preload', ['class' => Template::class])
  61. ->tag('container.preload', ['class' => TemplateWrapper::class])
  62. ->tag('container.private', ['package' => 'symfony/twig-bundle', 'version' => '5.2'])
  63. ->alias('Twig_Environment', 'twig')
  64. ->alias(Environment::class, 'twig')
  65. ->set('twig.app_variable', AppVariable::class)
  66. ->call('setEnvironment', [param('kernel.environment')])
  67. ->call('setDebug', [param('kernel.debug')])
  68. ->call('setTokenStorage', [service('security.token_storage')->ignoreOnInvalid()])
  69. ->call('setRequestStack', [service('request_stack')->ignoreOnInvalid()])
  70. ->set('twig.template_iterator', TemplateIterator::class)
  71. ->args([service('kernel'), abstract_arg('Twig paths'), param('twig.default_path')])
  72. ->set('twig.template_cache_warmer', TemplateCacheWarmer::class)
  73. ->args([service(ContainerInterface::class), service('twig.template_iterator')])
  74. ->tag('kernel.cache_warmer')
  75. ->tag('container.service_subscriber', ['id' => 'twig'])
  76. ->set('twig.loader.native_filesystem', FilesystemLoader::class)
  77. ->args([[], param('kernel.project_dir')])
  78. ->tag('twig.loader')
  79. ->set('twig.loader.chain', ChainLoader::class)
  80. ->set('twig.extension.profiler', ProfilerExtension::class)
  81. ->args([service('twig.profile'), service('debug.stopwatch')->ignoreOnInvalid()])
  82. ->set('twig.profile', Profile::class)
  83. ->set('data_collector.twig', TwigDataCollector::class)
  84. ->args([service('twig.profile'), service('twig')])
  85. ->tag('data_collector', ['template' => '@WebProfiler/Collector/twig.html.twig', 'id' => 'twig', 'priority' => 257])
  86. ->set('twig.extension.trans', TranslationExtension::class)
  87. ->args([service('translator')->nullOnInvalid()])
  88. ->tag('twig.extension')
  89. ->set('twig.extension.assets', AssetExtension::class)
  90. ->args([service('assets.packages')])
  91. ->set('twig.extension.code', CodeExtension::class)
  92. ->args([service('debug.file_link_formatter')->ignoreOnInvalid(), param('kernel.project_dir'), param('kernel.charset')])
  93. ->tag('twig.extension')
  94. ->set('twig.extension.routing', RoutingExtension::class)
  95. ->args([service('router')])
  96. ->set('twig.extension.yaml', YamlExtension::class)
  97. ->set('twig.extension.debug.stopwatch', StopwatchExtension::class)
  98. ->args([service('debug.stopwatch')->ignoreOnInvalid(), param('kernel.debug')])
  99. ->set('twig.extension.expression', ExpressionExtension::class)
  100. ->set('twig.extension.httpkernel', HttpKernelExtension::class)
  101. ->set('twig.runtime.httpkernel', HttpKernelRuntime::class)
  102. ->args([service('fragment.handler')])
  103. ->set('twig.extension.httpfoundation', HttpFoundationExtension::class)
  104. ->args([service('url_helper')])
  105. ->set('twig.extension.debug', DebugExtension::class)
  106. ->set('twig.extension.weblink', WebLinkExtension::class)
  107. ->args([service('request_stack')])
  108. ->set('twig.translation.extractor', TwigExtractor::class)
  109. ->args([service('twig')])
  110. ->tag('translation.extractor', ['alias' => 'twig'])
  111. ->set('workflow.twig_extension', WorkflowExtension::class)
  112. ->args([service('workflow.registry')])
  113. ->set('twig.configurator.environment', EnvironmentConfigurator::class)
  114. ->args([
  115. abstract_arg('date format, set in TwigExtension'),
  116. abstract_arg('interval format, set in TwigExtension'),
  117. abstract_arg('timezone, set in TwigExtension'),
  118. abstract_arg('decimals, set in TwigExtension'),
  119. abstract_arg('decimal point, set in TwigExtension'),
  120. abstract_arg('thousands separator, set in TwigExtension'),
  121. ])
  122. ->set('twig.runtime_loader', ContainerRuntimeLoader::class)
  123. ->args([abstract_arg('runtime locator')])
  124. ->set('twig.error_renderer.html', TwigErrorRenderer::class)
  125. ->decorate('error_renderer.html')
  126. ->args([
  127. service('twig'),
  128. service('twig.error_renderer.html.inner'),
  129. inline_service(TwigErrorRenderer::class)
  130. ->factory([TwigErrorRenderer::class, 'isDebug'])
  131. ->args([service('request_stack'), param('kernel.debug')]),
  132. ])
  133. ;
  134. };