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

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle;
  3. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheCompatibilityPass;
  4. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheSchemaSubscriberPass;
  5. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DbalSchemaFilterPass;
  6. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
  7. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\IdGeneratorPass;
  8. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\MiddlewaresPass;
  9. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RemoveLoggingMiddlewarePass;
  10. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RemoveProfilerControllerPass;
  11. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
  12. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\WellKnownSchemaFilterPass;
  13. use Doctrine\Common\Util\ClassUtils;
  14. use Doctrine\DBAL\Driver\Middleware;
  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 interface_exists;
  30. use function spl_autoload_unregister;
  31. class DoctrineBundle extends Bundle
  32. {
  33.     /** @var callable|null */
  34.     private $autoloader;
  35.     /**
  36.      * {@inheritDoc}
  37.      */
  38.     public function build(ContainerBuilder $container)
  39.     {
  40.         parent::build($container);
  41.         $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.connections''doctrine.dbal.%s_connection.event_manager''doctrine'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  42.         if ($container->hasExtension('security')) {
  43.             $security $container->getExtension('security');
  44.             if ($security instanceof SecurityExtension) {
  45.                 $security->addUserProviderFactory(new EntityFactory('entity''doctrine.orm.security.user.provider'));
  46.             }
  47.         }
  48.         $container->addCompilerPass(new CacheCompatibilityPass());
  49.         $container->addCompilerPass(new DoctrineValidationPass('orm'));
  50.         $container->addCompilerPass(new EntityListenerPass());
  51.         $container->addCompilerPass(new ServiceRepositoryCompilerPass());
  52.         $container->addCompilerPass(new IdGeneratorPass());
  53.         $container->addCompilerPass(new WellKnownSchemaFilterPass());
  54.         $container->addCompilerPass(new DbalSchemaFilterPass());
  55.         $container->addCompilerPass(new CacheSchemaSubscriberPass(), PassConfig::TYPE_BEFORE_REMOVING, -10);
  56.         $container->addCompilerPass(new RemoveProfilerControllerPass());
  57.         /** @psalm-suppress UndefinedClass */
  58.         if (interface_exists(Middleware::class)) {
  59.             $container->addCompilerPass(new RemoveLoggingMiddlewarePass());
  60.             $container->addCompilerPass(new MiddlewaresPass());
  61.         }
  62.         if (! class_exists(RegisterUidTypePass::class)) {
  63.             return;
  64.         }
  65.         $container->addCompilerPass(new RegisterUidTypePass());
  66.     }
  67.     /**
  68.      * {@inheritDoc}
  69.      */
  70.     public function boot()
  71.     {
  72.         // Register an autoloader for proxies to avoid issues when unserializing them
  73.         // when the ORM is used.
  74.         if (! $this->container->hasParameter('doctrine.orm.proxy_namespace')) {
  75.             return;
  76.         }
  77.         $namespace      = (string) $this->container->getParameter('doctrine.orm.proxy_namespace');
  78.         $dir            = (string) $this->container->getParameter('doctrine.orm.proxy_dir');
  79.         $proxyGenerator null;
  80.         if ($this->container->getParameter('doctrine.orm.auto_generate_proxy_classes')) {
  81.             // See https://github.com/symfony/symfony/pull/3419 for usage of references
  82.             $container = &$this->container;
  83.             $proxyGenerator = static function ($proxyDir$proxyNamespace$class) use (&$container): void {
  84.                 $originalClassName ClassUtils::getRealClass($class);
  85.                 $registry          $container->get('doctrine');
  86.                 assert($registry instanceof Registry);
  87.                 foreach ($registry->getManagers() as $em) {
  88.                     assert($em instanceof EntityManagerInterface);
  89.                     if (! $em->getConfiguration()->getAutoGenerateProxyClasses()) {
  90.                         continue;
  91.                     }
  92.                     $metadataFactory $em->getMetadataFactory();
  93.                     if ($metadataFactory->isTransient($originalClassName)) {
  94.                         continue;
  95.                     }
  96.                     $classMetadata $metadataFactory->getMetadataFor($originalClassName);
  97.                     $em->getProxyFactory()->generateProxyClasses([$classMetadata]);
  98.                     clearstatcache(trueAutoloader::resolveFile($proxyDir$proxyNamespace$class));
  99.                     break;
  100.                 }
  101.             };
  102.         }
  103.         $this->autoloader Autoloader::register($dir$namespace$proxyGenerator);
  104.     }
  105.     /**
  106.      * {@inheritDoc}
  107.      */
  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.     /**
  135.      * {@inheritDoc}
  136.      */
  137.     public function registerCommands(Application $application)
  138.     {
  139.     }
  140. }