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.     /** @return void */
  31.     public function build(ContainerBuilder $container)
  32.     {
  33.         $container->addCompilerPass(new CacheCompatibilityPass());
  34.         $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine_mongodb.odm.connections''doctrine_mongodb.odm.%s_connection.event_manager''doctrine_mongodb.odm'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  35.         $container->addCompilerPass(new CreateProxyDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
  36.         $container->addCompilerPass(new CreateHydratorDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
  37.         $container->addCompilerPass(new DoctrineValidationPass('mongodb'));
  38.         $container->addCompilerPass(new ServiceRepositoryCompilerPass());
  39.         $container->addCompilerPass(new FixturesCompilerPass());
  40.         if (! $container->hasExtension('security')) {
  41.             return;
  42.         }
  43.         $security $container->getExtension('security');
  44.         if (! ($security instanceof SecurityExtension)) {
  45.             return;
  46.         }
  47.         $security->addUserProviderFactory(new EntityFactory('mongodb''doctrine_mongodb.odm.security.user.provider'));
  48.     }
  49.     public function getContainerExtension(): ?ExtensionInterface
  50.     {
  51.         return new DoctrineMongoDBExtension();
  52.     }
  53.     /** @return void */
  54.     public function boot()
  55.     {
  56.         $registry $this->container->get('doctrine_mongodb');
  57.         assert($registry instanceof ManagerRegistry);
  58.         $this->registerAutoloader($registry->getManager());
  59.         $this->registerCommandLoggers();
  60.     }
  61.     private function registerAutoloader(DocumentManager $documentManager): void
  62.     {
  63.         $configuration $documentManager->getConfiguration();
  64.         if ($configuration->getAutoGenerateProxyClasses() !== Configuration::AUTOGENERATE_FILE_NOT_EXISTS) {
  65.             return;
  66.         }
  67.         $this->autoloader $configuration->getProxyManagerConfiguration()->getProxyAutoloader();
  68.         spl_autoload_register($this->autoloader);
  69.     }
  70.     private function unregisterAutoloader(): void
  71.     {
  72.         if ($this->autoloader === null) {
  73.             return;
  74.         }
  75.         spl_autoload_unregister($this->autoloader);
  76.         $this->autoloader null;
  77.     }
  78.     private function registerCommandLoggers(): void
  79.     {
  80.         $commandLoggerRegistry $this->container->get('doctrine_mongodb.odm.command_logger_registry');
  81.         $commandLoggerRegistry->register();
  82.     }
  83.     private function unregisterCommandLoggers(): void
  84.     {
  85.         $commandLoggerRegistry $this->container->get('doctrine_mongodb.odm.command_logger_registry');
  86.         $commandLoggerRegistry->unregister();
  87.     }
  88.     /** @return void */
  89.     public function shutdown()
  90.     {
  91.         $this->unregisterAutoloader();
  92.         $this->unregisterCommandLoggers();
  93.     }
  94. }