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

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