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

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