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

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