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

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <[email protected]>
  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\Runtime\SymfonyRuntime;
  61. use Symfony\Component\Serializer\DependencyInjection\SerializerPass;
  62. use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass;
  63. use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
  64. use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
  65. use Symfony\Component\Translation\DependencyInjection\TranslatorPathsPass;
  66. use Symfony\Component\Validator\DependencyInjection\AddAutoMappingConfigurationPass;
  67. use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
  68. use Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass;
  69. use Symfony\Component\VarExporter\Internal\Hydrator;
  70. use Symfony\Component\VarExporter\Internal\Registry;
  71. // Help opcache.preload discover always-needed symbols
  72. class_exists(ApcuAdapter::class);
  73. class_exists(ArrayAdapter::class);
  74. class_exists(ChainAdapter::class);
  75. class_exists(PhpArrayAdapter::class);
  76. class_exists(PhpFilesAdapter::class);
  77. class_exists(Dotenv::class);
  78. class_exists(ErrorHandler::class);
  79. class_exists(Hydrator::class);
  80. class_exists(Registry::class);
  81. /**
  82. * Bundle.
  83. *
  84. * @author Fabien Potencier <[email protected]>
  85. */
  86. class FrameworkBundle extends Bundle
  87. {
  88. public function boot()
  89. {
  90. if (class_exists(SymfonyRuntime::class)) {
  91. $handler = set_error_handler('var_dump');
  92. restore_error_handler();
  93. } else {
  94. $handler = [ErrorHandler::register(null, false)];
  95. }
  96. if (\is_array($handler) && $handler[0] instanceof ErrorHandler) {
  97. $handler[0]->throwAt($this->container->getParameter('debug.error_handler.throw_at'), true);
  98. }
  99. if ($this->container->getParameter('kernel.http_method_override')) {
  100. Request::enableHttpMethodParameterOverride();
  101. }
  102. }
  103. public function build(ContainerBuilder $container)
  104. {
  105. parent::build($container);
  106. $registerListenersPass = new RegisterListenersPass();
  107. $registerListenersPass->setHotPathEvents([
  108. KernelEvents::REQUEST,
  109. KernelEvents::CONTROLLER,
  110. KernelEvents::CONTROLLER_ARGUMENTS,
  111. KernelEvents::RESPONSE,
  112. KernelEvents::FINISH_REQUEST,
  113. ]);
  114. if (class_exists(ConsoleEvents::class)) {
  115. $registerListenersPass->setNoPreloadEvents([
  116. ConsoleEvents::COMMAND,
  117. ConsoleEvents::TERMINATE,
  118. ConsoleEvents::ERROR,
  119. ]);
  120. }
  121. $container->addCompilerPass(new AssetsContextPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  122. $container->addCompilerPass(new LoggerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
  123. $container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());
  124. $container->addCompilerPass(new RemoveEmptyControllerArgumentLocatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);
  125. $container->addCompilerPass(new RoutingResolverPass());
  126. $container->addCompilerPass(new DataCollectorTranslatorPass());
  127. $container->addCompilerPass(new ProfilerPass());
  128. // must be registered before removing private services as some might be listeners/subscribers
  129. // but as late as possible to get resolved parameters
  130. $container->addCompilerPass($registerListenersPass, PassConfig::TYPE_BEFORE_REMOVING);
  131. $this->addCompilerPassIfExists($container, AddConstraintValidatorsPass::class);
  132. $container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_AFTER_REMOVING, -255);
  133. $this->addCompilerPassIfExists($container, AddValidatorInitializersPass::class);
  134. $this->addCompilerPassIfExists($container, AddConsoleCommandPass::class, PassConfig::TYPE_BEFORE_REMOVING);
  135. // must be registered as late as possible to get access to all Twig paths registered in
  136. // twig.template_iterator definition
  137. $this->addCompilerPassIfExists($container, TranslatorPass::class, PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
  138. $this->addCompilerPassIfExists($container, TranslatorPathsPass::class, PassConfig::TYPE_AFTER_REMOVING);
  139. $container->addCompilerPass(new LoggingTranslatorPass());
  140. $container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
  141. $this->addCompilerPassIfExists($container, TranslationExtractorPass::class);
  142. $this->addCompilerPassIfExists($container, TranslationDumperPass::class);
  143. $container->addCompilerPass(new FragmentRendererPass());
  144. $this->addCompilerPassIfExists($container, SerializerPass::class);
  145. $this->addCompilerPassIfExists($container, PropertyInfoPass::class);
  146. $container->addCompilerPass(new ControllerArgumentValueResolverPass());
  147. $container->addCompilerPass(new CachePoolPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 32);
  148. $container->addCompilerPass(new CachePoolClearerPass(), PassConfig::TYPE_AFTER_REMOVING);
  149. $container->addCompilerPass(new CachePoolPrunerPass(), PassConfig::TYPE_AFTER_REMOVING);
  150. $this->addCompilerPassIfExists($container, FormPass::class);
  151. $container->addCompilerPass(new WorkflowGuardListenerPass());
  152. $container->addCompilerPass(new ResettableServicePass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
  153. $container->addCompilerPass(new RegisterLocaleAwareServicesPass());
  154. $container->addCompilerPass(new TestServiceContainerWeakRefPass(), PassConfig::TYPE_BEFORE_REMOVING, -32);
  155. $container->addCompilerPass(new TestServiceContainerRealRefPass(), PassConfig::TYPE_AFTER_REMOVING);
  156. $this->addCompilerPassIfExists($container, AddMimeTypeGuesserPass::class);
  157. $this->addCompilerPassIfExists($container, MessengerPass::class);
  158. $this->addCompilerPassIfExists($container, HttpClientPass::class);
  159. $this->addCompilerPassIfExists($container, AddAutoMappingConfigurationPass::class);
  160. $container->addCompilerPass(new RegisterReverseContainerPass(true));
  161. $container->addCompilerPass(new RegisterReverseContainerPass(false), PassConfig::TYPE_AFTER_REMOVING);
  162. $container->addCompilerPass(new RemoveUnusedSessionMarshallingHandlerPass());
  163. $container->addCompilerPass(new SessionPass());
  164. // must be registered after MonologBundle's LoggerChannelPass
  165. $container->addCompilerPass(new ErrorLoggerCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
  166. if ($container->getParameter('kernel.debug')) {
  167. $container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 2);
  168. $container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
  169. $container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_BEFORE_REMOVING, -255);
  170. $container->addCompilerPass(new CacheCollectorPass(), PassConfig::TYPE_BEFORE_REMOVING);
  171. }
  172. }
  173. private function addCompilerPassIfExists(ContainerBuilder $container, string $class, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
  174. {
  175. $container->addResource(new ClassExistenceResource($class));
  176. if (class_exists($class)) {
  177. $container->addCompilerPass(new $class(), $type, $priority);
  178. }
  179. }
  180. }