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

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