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

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