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

  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\EnableLoggerDebugModePass;
  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\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\BinaryFileResponse;
  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(nullfalse)->throwAt($this->container->getParameter('debug.error_handler.throw_at'), true);
  90.         if ($this->container->getParameter('kernel.http_method_override')) {
  91.             Request::enableHttpMethodParameterOverride();
  92.         }
  93.         if ($this->container->hasParameter('kernel.trust_x_sendfile_type_header') && $this->container->getParameter('kernel.trust_x_sendfile_type_header')) {
  94.             BinaryFileResponse::trustXSendfileTypeHeader();
  95.         }
  96.     }
  97.     public function build(ContainerBuilder $container)
  98.     {
  99.         parent::build($container);
  100.         $registerListenersPass = new RegisterListenersPass();
  101.         $registerListenersPass->setHotPathEvents([
  102.             KernelEvents::REQUEST,
  103.             KernelEvents::CONTROLLER,
  104.             KernelEvents::CONTROLLER_ARGUMENTS,
  105.             KernelEvents::RESPONSE,
  106.             KernelEvents::FINISH_REQUEST,
  107.         ]);
  108.         if (class_exists(ConsoleEvents::class)) {
  109.             $registerListenersPass->setNoPreloadEvents([
  110.                 ConsoleEvents::COMMAND,
  111.                 ConsoleEvents::TERMINATE,
  112.                 ConsoleEvents::ERROR,
  113.             ]);
  114.         }
  115.         $container->addCompilerPass(new AssetsContextPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  116.         $container->addCompilerPass(new LoggerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
  117.         $container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());
  118.         $container->addCompilerPass(new RemoveEmptyControllerArgumentLocatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);
  119.         $container->addCompilerPass(new RoutingResolverPass());
  120.         $container->addCompilerPass(new DataCollectorTranslatorPass());
  121.         $container->addCompilerPass(new ProfilerPass());
  122.         // must be registered before removing private services as some might be listeners/subscribers
  123.         // but as late as possible to get resolved parameters
  124.         $container->addCompilerPass($registerListenersPassPassConfig::TYPE_BEFORE_REMOVING);
  125.         $this->addCompilerPassIfExists($containerAddConstraintValidatorsPass::class);
  126.         $container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_AFTER_REMOVING, -255);
  127.         $this->addCompilerPassIfExists($containerAddValidatorInitializersPass::class);
  128.         $this->addCompilerPassIfExists($containerAddConsoleCommandPass::class, PassConfig::TYPE_BEFORE_REMOVING);
  129.         // must be registered as late as possible to get access to all Twig paths registered in
  130.         // twig.template_iterator definition
  131.         $this->addCompilerPassIfExists($containerTranslatorPass::class, PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
  132.         $this->addCompilerPassIfExists($containerTranslatorPathsPass::class, PassConfig::TYPE_AFTER_REMOVING);
  133.         $container->addCompilerPass(new LoggingTranslatorPass());
  134.         $container->addCompilerPass(new AddExpressionLanguageProvidersPass(false));
  135.         $this->addCompilerPassIfExists($containerTranslationExtractorPass::class);
  136.         $this->addCompilerPassIfExists($containerTranslationDumperPass::class);
  137.         $container->addCompilerPass(new FragmentRendererPass());
  138.         $this->addCompilerPassIfExists($containerSerializerPass::class);
  139.         $this->addCompilerPassIfExists($containerPropertyInfoPass::class);
  140.         $container->addCompilerPass(new ControllerArgumentValueResolverPass());
  141.         $container->addCompilerPass(new CachePoolPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION32);
  142.         $container->addCompilerPass(new CachePoolClearerPass(), PassConfig::TYPE_AFTER_REMOVING);
  143.         $container->addCompilerPass(new CachePoolPrunerPass(), PassConfig::TYPE_AFTER_REMOVING);
  144.         $this->addCompilerPassIfExists($containerFormPass::class);
  145.         $container->addCompilerPass(new WorkflowGuardListenerPass());
  146.         $container->addCompilerPass(new ResettableServicePass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
  147.         $container->addCompilerPass(new RegisterLocaleAwareServicesPass());
  148.         $container->addCompilerPass(new TestServiceContainerWeakRefPass(), PassConfig::TYPE_BEFORE_REMOVING, -32);
  149.         $container->addCompilerPass(new TestServiceContainerRealRefPass(), PassConfig::TYPE_AFTER_REMOVING);
  150.         $this->addCompilerPassIfExists($containerAddMimeTypeGuesserPass::class);
  151.         $this->addCompilerPassIfExists($containerMessengerPass::class);
  152.         $this->addCompilerPassIfExists($containerHttpClientPass::class);
  153.         $this->addCompilerPassIfExists($containerAddAutoMappingConfigurationPass::class);
  154.         $container->addCompilerPass(new RegisterReverseContainerPass(true));
  155.         $container->addCompilerPass(new RegisterReverseContainerPass(false), PassConfig::TYPE_AFTER_REMOVING);
  156.         $container->addCompilerPass(new RemoveUnusedSessionMarshallingHandlerPass());
  157.         if ($container->getParameter('kernel.debug')) {
  158.             $container->addCompilerPass(new EnableLoggerDebugModePass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -33);
  159.             $container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION2);
  160.             $container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
  161.             $container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_BEFORE_REMOVING, -255);
  162.             $container->addCompilerPass(new CacheCollectorPass(), PassConfig::TYPE_BEFORE_REMOVING);
  163.         }
  164.     }
  165.     private function addCompilerPassIfExists(ContainerBuilder $containerstring $classstring $type PassConfig::TYPE_BEFORE_OPTIMIZATIONint $priority 0)
  166.     {
  167.         $container->addResource(new ClassExistenceResource($class));
  168.         if (class_exists($class)) {
  169.             $container->addCompilerPass(new $class(), $type$priority);
  170.         }
  171.     }
  172. }