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

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