vendor/doctrine/mongodb-odm-bundle/DoctrineMongoDBBundle.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Bundle\MongoDBBundle;
  4. use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CacheCompatibilityPass;
  5. use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateHydratorDirectoryPass;
  6. use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateProxyDirectoryPass;
  7. use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\FixturesCompilerPass;
  8. use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
  9. use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension;
  10. use Doctrine\ODM\MongoDB\Configuration;
  11. use Doctrine\ODM\MongoDB\DocumentManager;
  12. use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
  13. use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
  14. use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
  15. use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
  16. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  19. use Symfony\Component\HttpKernel\Bundle\Bundle;
  20. use function assert;
  21. use function spl_autoload_register;
  22. use function spl_autoload_unregister;
  23. /**
  24.  * Doctrine MongoDB ODM bundle.
  25.  */
  26. class DoctrineMongoDBBundle extends Bundle
  27. {
  28.     /** @var callable|null */
  29.     private $autoloader;
  30.     public function build(ContainerBuilder $container)
  31.     {
  32.         $container->addCompilerPass(new CacheCompatibilityPass());
  33.         $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine_mongodb.odm.connections''doctrine_mongodb.odm.%s_connection.event_manager''doctrine_mongodb.odm'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  34.         $container->addCompilerPass(new CreateProxyDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
  35.         $container->addCompilerPass(new CreateHydratorDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
  36.         $container->addCompilerPass(new DoctrineValidationPass('mongodb'));
  37.         $container->addCompilerPass(new ServiceRepositoryCompilerPass());
  38.         $container->addCompilerPass(new FixturesCompilerPass());
  39.         if (! $container->hasExtension('security')) {
  40.             return;
  41.         }
  42.         $security $container->getExtension('security');
  43.         if (! ($security instanceof SecurityExtension)) {
  44.             return;
  45.         }
  46.         $security->addUserProviderFactory(new EntityFactory('mongodb''doctrine_mongodb.odm.security.user.provider'));
  47.     }
  48.     public function getContainerExtension(): ?ExtensionInterface
  49.     {
  50.         return new DoctrineMongoDBExtension();
  51.     }
  52.     public function boot()
  53.     {
  54.         $registry $this->container->get('doctrine_mongodb');
  55.         assert($registry instanceof ManagerRegistry);
  56.         $this->registerAutoloader($registry->getManager());
  57.         $this->registerCommandLoggers();
  58.     }
  59.     private function registerAutoloader(DocumentManager $documentManager): void
  60.     {
  61.         $configuration $documentManager->getConfiguration();
  62.         if ($configuration->getAutoGenerateProxyClasses() !== Configuration::AUTOGENERATE_FILE_NOT_EXISTS) {
  63.             return;
  64.         }
  65.         $this->autoloader $configuration->getProxyManagerConfiguration()->getProxyAutoloader();
  66.         spl_autoload_register($this->autoloader);
  67.     }
  68.     private function unregisterAutoloader(): void
  69.     {
  70.         if ($this->autoloader === null) {
  71.             return;
  72.         }
  73.         spl_autoload_unregister($this->autoloader);
  74.         $this->autoloader null;
  75.     }
  76.     private function registerCommandLoggers(): void
  77.     {
  78.         $commandLoggerRegistry $this->container->get('doctrine_mongodb.odm.command_logger_registry');
  79.         $commandLoggerRegistry->register();
  80.     }
  81.     private function unregisterCommandLoggers(): void
  82.     {
  83.         $commandLoggerRegistry $this->container->get('doctrine_mongodb.odm.command_logger_registry');
  84.         $commandLoggerRegistry->unregister();
  85.     }
  86.     public function shutdown()
  87.     {
  88.         $this->unregisterAutoloader();
  89.         $this->unregisterCommandLoggers();
  90.     }
  91. }