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

  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\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.             $container = &$this->container;
  85.             $proxyGenerator = static function ($proxyDir$proxyNamespace$class) use (&$container): void {
  86.                 $originalClassName ClassUtils::getRealClass($class);
  87.                 $registry          $container->get('doctrine');
  88.                 assert($registry instanceof Registry);
  89.                 foreach ($registry->getManagers() as $em) {
  90.                     assert($em instanceof EntityManagerInterface);
  91.                     if (! $em->getConfiguration()->getAutoGenerateProxyClasses()) {
  92.                         continue;
  93.                     }
  94.                     $metadataFactory $em->getMetadataFactory();
  95.                     if ($metadataFactory->isTransient($originalClassName)) {
  96.                         continue;
  97.                     }
  98.                     $classMetadata $metadataFactory->getMetadataFor($originalClassName);
  99.                     $em->getProxyFactory()->generateProxyClasses([$classMetadata]);
  100.                     clearstatcache(trueAutoloader::resolveFile($proxyDir$proxyNamespace$class));
  101.                     break;
  102.                 }
  103.             };
  104.         }
  105.         $this->autoloader Autoloader::register($dir$namespace$proxyGenerator);
  106.     }
  107.     /** @return void */
  108.     public function shutdown()
  109.     {
  110.         if ($this->autoloader !== null) {
  111.             spl_autoload_unregister($this->autoloader);
  112.             $this->autoloader null;
  113.         }
  114.         // Clear all entity managers to clear references to entities for GC
  115.         if ($this->container->hasParameter('doctrine.entity_managers')) {
  116.             foreach ($this->container->getParameter('doctrine.entity_managers') as $id) {
  117.                 if (! $this->container->initialized($id)) {
  118.                     continue;
  119.                 }
  120.                 $this->container->get($id)->clear();
  121.             }
  122.         }
  123.         // Close all connections to avoid reaching too many connections in the process when booting again later (tests)
  124.         if (! $this->container->hasParameter('doctrine.connections')) {
  125.             return;
  126.         }
  127.         foreach ($this->container->getParameter('doctrine.connections') as $id) {
  128.             if (! $this->container->initialized($id)) {
  129.                 continue;
  130.             }
  131.             $this->container->get($id)->close();
  132.         }
  133.     }
  134.     /** @return void */
  135.     public function registerCommands(Application $application)
  136.     {
  137.     }
  138. }