vendor/doctrine/doctrine-bundle/DoctrineBundle.php line 36

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle;
  3. use Closure;
  4. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheCompatibilityPass;
  5. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheSchemaSubscriberPass;
  6. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DbalSchemaFilterPass;
  7. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
  8. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\IdGeneratorPass;
  9. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\MiddlewaresPass;
  10. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RemoveLoggingMiddlewarePass;
  11. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RemoveProfilerControllerPass;
  12. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
  13. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\WellKnownSchemaFilterPass;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Doctrine\ORM\Proxy\Autoloader;
  16. use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
  17. use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
  18. use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
  19. use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterUidTypePass;
  20. use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
  21. use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
  22. use Symfony\Component\Console\Application;
  23. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  24. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  25. use Symfony\Component\DependencyInjection\ContainerBuilder;
  26. use Symfony\Component\HttpKernel\Bundle\Bundle;
  27. use function assert;
  28. use function class_exists;
  29. use function clearstatcache;
  30. use function spl_autoload_unregister;
  31. /** @final since 2.9 */
  32. class DoctrineBundle extends Bundle
  33. {
  34.     private ?Closure $autoloader null;
  35.     /** @return void */
  36.     public function build(ContainerBuilder $container)
  37.     {
  38.         parent::build($container);
  39.         $container->addCompilerPass(new class () implements CompilerPassInterface {
  40.             public function process(ContainerBuilder $container): void
  41.             {
  42.                 if ($container->has('session.handler')) {
  43.                     return;
  44.                 }
  45.                 $container->removeDefinition('doctrine.orm.listeners.pdo_session_handler_schema_listener');
  46.             }
  47.         }, PassConfig::TYPE_BEFORE_OPTIMIZATION);
  48.         $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.connections''doctrine.dbal.%s_connection.event_manager''doctrine'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  49.         if ($container->hasExtension('security')) {
  50.             $security $container->getExtension('security');
  51.             if ($security instanceof SecurityExtension) {
  52.                 $security->addUserProviderFactory(new EntityFactory('entity''doctrine.orm.security.user.provider'));
  53.             }
  54.         }
  55.         $container->addCompilerPass(new CacheCompatibilityPass());
  56.         $container->addCompilerPass(new DoctrineValidationPass('orm'));
  57.         $container->addCompilerPass(new EntityListenerPass());
  58.         $container->addCompilerPass(new ServiceRepositoryCompilerPass());
  59.         $container->addCompilerPass(new IdGeneratorPass());
  60.         $container->addCompilerPass(new WellKnownSchemaFilterPass());
  61.         $container->addCompilerPass(new DbalSchemaFilterPass());
  62.         $container->addCompilerPass(new CacheSchemaSubscriberPass(), PassConfig::TYPE_BEFORE_REMOVING, -10);
  63.         $container->addCompilerPass(new RemoveProfilerControllerPass());
  64.         $container->addCompilerPass(new RemoveLoggingMiddlewarePass());
  65.         $container->addCompilerPass(new MiddlewaresPass());
  66.         if (! class_exists(RegisterUidTypePass::class)) {
  67.             return;
  68.         }
  69.         $container->addCompilerPass(new RegisterUidTypePass());
  70.     }
  71.     /** @return void */
  72.     public function boot()
  73.     {
  74.         // Register an autoloader for proxies to avoid issues when unserializing them
  75.         // when the ORM is used.
  76.         if (! $this->container->hasParameter('doctrine.orm.proxy_namespace')) {
  77.             return;
  78.         }
  79.         $namespace      = (string) $this->container->getParameter('doctrine.orm.proxy_namespace');
  80.         $dir            = (string) $this->container->getParameter('doctrine.orm.proxy_dir');
  81.         $proxyGenerator null;
  82.         if ($this->container->getParameter('doctrine.orm.auto_generate_proxy_classes')) {
  83.             // See https://github.com/symfony/symfony/pull/3419 for usage of references
  84.             /** @psalm-suppress UnsupportedPropertyReferenceUsage */
  85.             $container = &$this->container;
  86.             $proxyGenerator = static function ($proxyDir$proxyNamespace$class) use (&$container): void {
  87.                 $originalClassName = (new DefaultProxyClassNameResolver())->resolveClassName($class);
  88.                 $registry          $container->get('doctrine');
  89.                 assert($registry instanceof Registry);
  90.                 foreach ($registry->getManagers() as $em) {
  91.                     assert($em instanceof EntityManagerInterface);
  92.                     if (! $em->getConfiguration()->getAutoGenerateProxyClasses()) {
  93.                         continue;
  94.                     }
  95.                     $metadataFactory $em->getMetadataFactory();
  96.                     if ($metadataFactory->isTransient($originalClassName)) {
  97.                         continue;
  98.                     }
  99.                     $classMetadata $metadataFactory->getMetadataFor($originalClassName);
  100.                     $em->getProxyFactory()->generateProxyClasses([$classMetadata]);
  101.                     clearstatcache(trueAutoloader::resolveFile($proxyDir$proxyNamespace$class));
  102.                     break;
  103.                 }
  104.             };
  105.         }
  106.         $this->autoloader Autoloader::register($dir$namespace$proxyGenerator);
  107.     }
  108.     /** @return void */
  109.     public function shutdown()
  110.     {
  111.         if ($this->autoloader !== null) {
  112.             spl_autoload_unregister($this->autoloader);
  113.             $this->autoloader null;
  114.         }
  115.         // Clear all entity managers to clear references to entities for GC
  116.         if ($this->container->hasParameter('doctrine.entity_managers')) {
  117.             foreach ($this->container->getParameter('doctrine.entity_managers') as $id) {
  118.                 if (! $this->container->initialized($id)) {
  119.                     continue;
  120.                 }
  121.                 $this->container->get($id)->clear();
  122.             }
  123.         }
  124.         // Close all connections to avoid reaching too many connections in the process when booting again later (tests)
  125.         if (! $this->container->hasParameter('doctrine.connections')) {
  126.             return;
  127.         }
  128.         foreach ($this->container->getParameter('doctrine.connections') as $id) {
  129.             if (! $this->container->initialized($id)) {
  130.                 continue;
  131.             }
  132.             $this->container->get($id)->close();
  133.         }
  134.     }
  135.     /** @return void */
  136.     public function registerCommands(Application $application)
  137.     {
  138.     }
  139. }