FrameworkBundle.php 10 KB

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