vendor/symfony/framework-bundle/FrameworkBundle.php line 90

Open in your IDE?
  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\FrameworkBundle;
  11. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
  12. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddDebugLogProcessorPass;
  13. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass;
  14. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AssetsContextPass;
  15. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
  16. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
  17. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ErrorLoggerCompilerPass;
  18. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
  19. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
  20. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RemoveUnusedSessionMarshallingHandlerPass;
  21. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SessionPass;
  22. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass;
  23. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass;
  24. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
  25. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\WorkflowGuardListenerPass;
  26. use Symfony\Component\Cache\Adapter\ApcuAdapter;
  27. use Symfony\Component\Cache\Adapter\ArrayAdapter;
  28. use Symfony\Component\Cache\Adapter\ChainAdapter;
  29. use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
  30. use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
  31. use Symfony\Component\Cache\DependencyInjection\CacheCollectorPass;
  32. use Symfony\Component\Cache\DependencyInjection\CachePoolClearerPass;
  33. use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
  34. use Symfony\Component\Cache\DependencyInjection\CachePoolPrunerPass;
  35. use Symfony\Component\Config\Resource\ClassExistenceResource;
  36. use Symfony\Component\Console\ConsoleEvents;
  37. use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
  38. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  39. use Symfony\Component\DependencyInjection\Compiler\RegisterReverseContainerPass;
  40. use Symfony\Component\DependencyInjection\ContainerBuilder;
  41. use Symfony\Component\Dotenv\Dotenv;
  42. use Symfony\Component\ErrorHandler\ErrorHandler;
  43. use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
  44. use Symfony\Component\Form\DependencyInjection\FormPass;
  45. use Symfony\Component\HttpClient\DependencyInjection\HttpClientPass;
  46. use Symfony\Component\HttpFoundation\Request;
  47. use Symfony\Component\HttpKernel\Bundle\Bundle;
  48. use Symfony\Component\HttpKernel\DependencyInjection\ControllerArgumentValueResolverPass;
  49. use Symfony\Component\HttpKernel\DependencyInjection\FragmentRendererPass;
  50. use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
  51. use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass;
  52. use Symfony\Component\HttpKernel\DependencyInjection\RegisterLocaleAwareServicesPass;
  53. use Symfony\Component\HttpKernel\DependencyInjection\RemoveEmptyControllerArgumentLocatorsPass;
  54. use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
  55. use Symfony\Component\HttpKernel\KernelEvents;
  56. use Symfony\Component\Messenger\DependencyInjection\MessengerPass;
  57. use Symfony\Component\Mime\DependencyInjection\AddMimeTypeGuesserPass;
  58. use Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoPass;
  59. use Symfony\Component\Routing\DependencyInjection\RoutingResolverPass;
  60. use Symfony\Component\Serializer\DependencyInjection\SerializerPass;
  61. use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass;
  62. use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
  63. use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
  64. use Symfony\Component\Translation\DependencyInjection\TranslatorPathsPass;
  65. use Symfony\Component\Validator\DependencyInjection\AddAutoMappingConfigurationPass;
  66. use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
  67. use Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass;
  68. use Symfony\Component\VarExporter\Internal\Hydrator;
  69. use Symfony\Component\VarExporter\Internal\Registry;
  70. // Help opcache.preload discover always-needed symbols
  71. class_exists(ApcuAdapter::class);
  72. class_exists(ArrayAdapter::class);
  73. class_exists(ChainAdapter::class);
  74. class_exists(PhpArrayAdapter::class);
  75. class_exists(PhpFilesAdapter::class);
  76. class_exists(Dotenv::class);
  77. class_exists(ErrorHandler::class);
  78. class_exists(Hydrator::class);
  79. class_exists(Registry::class);
  80. /**
  81. * Bundle.
  82. *
  83. * @author Fabien Potencier <fabien@symfony.com>
  84. */
  85. class FrameworkBundle extends Bundle
  86. {
  87. public function boot()
  88. {
  89. ErrorHandler::register(null, false)->throwAt($this->container->getParameter('debug.error_handler.throw_at'), true);
  90. if ($this->container->getParameter('kernel.http_method_override')) {
  91. Request::enableHttpMethodParameterOverride();
  92. }
  93. }
  94. public function build(ContainerBuilder $container)
  95. {
  96. parent::build($container);
  97. $registerListenersPass = new RegisterListenersPass();
  98. $registerListenersPass->setHotPathEvents([
  99. KernelEvents::REQUEST,
  100. KernelEvents::CONTROLLER,
  101. KernelEvents::CONTROLLER_ARGUMENTS,
  102. KernelEvents::RESPONSE,
  103. KernelEvents::FINISH_REQUEST,
  104. ]);
  105. if (class_exists(ConsoleEvents::class)) {
  106. $registerListenersPass->setNoPreloadEvents([
  107. ConsoleEvents::COMMAND,
  108. ConsoleEvents::TERMINATE,
  109. ConsoleEvents::ERROR,
  110. ]);
  111. }
  112. $container->addCompilerPass(new AssetsContextPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  113. $container->addCompilerPass(new LoggerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
  114. $container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());
  115. $container->addCompilerPass(new RemoveEmptyControllerArgumentLocatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);
  116. $container->addCompilerPass(new RoutingResolverPass());
  117. $container->addCompilerPass(new DataCollectorTranslatorPass());
  118. $container->addCompilerPass(new ProfilerPass());
  119. // must be registered before removing private services as some might be listeners/subscribers
  120. // but as late as possible to get resolved parameters
  121. $container->addCompilerPass($registerListenersPass, PassConfig::TYPE_BEFORE_REMOVING);
  122. $this->addCompilerPassIfExists($container, AddConstraintValidatorsPass::class);
  123. $container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_AFTER_REMOVING, -255);
  124. $this->addCompilerPassIfExists($container, AddValidatorInitializersPass::class);
  125. $this->addCompilerPassIfExists($container, AddConsoleCommandPass::class, PassConfig::TYPE_BEFORE_REMOVING);
  126. // must be registered as late as possible to get access to all Twig paths registered in
  127. // twig.template_iterator definition
  128. $this->addCompilerPassIfExists($container, TranslatorPass::class, PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
  129. $this->addCompilerPassIfExists($container, TranslatorPathsPass::class, PassConfig::TYPE_AFTER_REMOVING);
  130. $container->addCompilerPass(new LoggingTranslatorPass());
  131. $container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
  132. $this->addCompilerPassIfExists($container, TranslationExtractorPass::class);
  133. $this->addCompilerPassIfExists($container, TranslationDumperPass::class);
  134. $container->addCompilerPass(new FragmentRendererPass());
  135. $this->addCompilerPassIfExists($container, SerializerPass::class);
  136. $this->addCompilerPassIfExists($container, PropertyInfoPass::class);
  137. $container->addCompilerPass(new ControllerArgumentValueResolverPass());
  138. $container->addCompilerPass(new CachePoolPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 32);
  139. $container->addCompilerPass(new CachePoolClearerPass(), PassConfig::TYPE_AFTER_REMOVING);
  140. $container->addCompilerPass(new CachePoolPrunerPass(), PassConfig::TYPE_AFTER_REMOVING);
  141. $this->addCompilerPassIfExists($container, FormPass::class);
  142. $container->addCompilerPass(new WorkflowGuardListenerPass());
  143. $container->addCompilerPass(new ResettableServicePass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
  144. $container->addCompilerPass(new RegisterLocaleAwareServicesPass());
  145. $container->addCompilerPass(new TestServiceContainerWeakRefPass(), PassConfig::TYPE_BEFORE_REMOVING, -32);
  146. $container->addCompilerPass(new TestServiceContainerRealRefPass(), PassConfig::TYPE_AFTER_REMOVING);
  147. $this->addCompilerPassIfExists($container, AddMimeTypeGuesserPass::class);
  148. $this->addCompilerPassIfExists($container, MessengerPass::class);
  149. $this->addCompilerPassIfExists($container, HttpClientPass::class);
  150. $this->addCompilerPassIfExists($container, AddAutoMappingConfigurationPass::class);
  151. $container->addCompilerPass(new RegisterReverseContainerPass(true));
  152. $container->addCompilerPass(new RegisterReverseContainerPass(false), PassConfig::TYPE_AFTER_REMOVING);
  153. $container->addCompilerPass(new RemoveUnusedSessionMarshallingHandlerPass());
  154. $container->addCompilerPass(new SessionPass());
  155. // must be registered after MonologBundle's LoggerChannelPass
  156. $container->addCompilerPass(new ErrorLoggerCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
  157. if ($container->getParameter('kernel.debug')) {
  158. $container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 2);
  159. $container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
  160. $container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_BEFORE_REMOVING, -255);
  161. $container->addCompilerPass(new CacheCollectorPass(), PassConfig::TYPE_BEFORE_REMOVING);
  162. }
  163. }
  164. private function addCompilerPassIfExists(ContainerBuilder $container, string $class, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
  165. {
  166. $container->addResource(new ClassExistenceResource($class));
  167. if (class_exists($class)) {
  168. $container->addCompilerPass(new $class(), $type, $priority);
  169. }
  170. }
  171. }