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

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