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

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle;
  3. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheSchemaSubscriberPass;
  4. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DbalSchemaFilterPass;
  5. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
  6. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
  7. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\WellKnownSchemaFilterPass;
  8. use Doctrine\Common\Util\ClassUtils;
  9. use Doctrine\ORM\EntityManager;
  10. use Doctrine\ORM\Proxy\Autoloader;
  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\Console\Application;
  15. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\HttpKernel\Bundle\Bundle;
  18. class DoctrineBundle extends Bundle
  19. {
  20.     /** @var callable|null */
  21.     private $autoloader;
  22.     /**
  23.      * {@inheritDoc}
  24.      */
  25.     public function build(ContainerBuilder $container)
  26.     {
  27.         parent::build($container);
  28.         $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.connections''doctrine.dbal.%s_connection.event_manager''doctrine'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  29.         if ($container->hasExtension('security')) {
  30.             $container->getExtension('security')->addUserProviderFactory(new EntityFactory('entity''doctrine.orm.security.user.provider'));
  31.         }
  32.         $container->addCompilerPass(new DoctrineValidationPass('orm'));
  33.         $container->addCompilerPass(new EntityListenerPass());
  34.         $container->addCompilerPass(new ServiceRepositoryCompilerPass());
  35.         $container->addCompilerPass(new WellKnownSchemaFilterPass());
  36.         $container->addCompilerPass(new DbalSchemaFilterPass());
  37.         $container->addCompilerPass(new CacheSchemaSubscriberPass(), PassConfig::TYPE_OPTIMIZE, -10);
  38.     }
  39.     /**
  40.      * {@inheritDoc}
  41.      */
  42.     public function boot()
  43.     {
  44.         // Register an autoloader for proxies to avoid issues when unserializing them
  45.         // when the ORM is used.
  46.         if (! $this->container->hasParameter('doctrine.orm.proxy_namespace')) {
  47.             return;
  48.         }
  49.         $namespace      $this->container->getParameter('doctrine.orm.proxy_namespace');
  50.         $dir            $this->container->getParameter('doctrine.orm.proxy_dir');
  51.         $proxyGenerator null;
  52.         if ($this->container->getParameter('doctrine.orm.auto_generate_proxy_classes')) {
  53.             // See https://github.com/symfony/symfony/pull/3419 for usage of references
  54.             $container = &$this->container;
  55.             $proxyGenerator = static function ($proxyDir$proxyNamespace$class) use (&$container) {
  56.                 $originalClassName ClassUtils::getRealClass($class);
  57.                 /** @var Registry $registry */
  58.                 $registry $container->get('doctrine');
  59.                 // Tries to auto-generate the proxy file
  60.                 /** @var EntityManager $em */
  61.                 foreach ($registry->getManagers() as $em) {
  62.                     if (! $em->getConfiguration()->getAutoGenerateProxyClasses()) {
  63.                         continue;
  64.                     }
  65.                     $metadataFactory $em->getMetadataFactory();
  66.                     if ($metadataFactory->isTransient($originalClassName)) {
  67.                         continue;
  68.                     }
  69.                     $classMetadata $metadataFactory->getMetadataFor($originalClassName);
  70.                     $em->getProxyFactory()->generateProxyClasses([$classMetadata]);
  71.                     clearstatcache(trueAutoloader::resolveFile($proxyDir$proxyNamespace$class));
  72.                     break;
  73.                 }
  74.             };
  75.         }
  76.         $this->autoloader Autoloader::register($dir$namespace$proxyGenerator);
  77.     }
  78.     /**
  79.      * {@inheritDoc}
  80.      */
  81.     public function shutdown()
  82.     {
  83.         if ($this->autoloader !== null) {
  84.             spl_autoload_unregister($this->autoloader);
  85.             $this->autoloader null;
  86.         }
  87.         // Clear all entity managers to clear references to entities for GC
  88.         if ($this->container->hasParameter('doctrine.entity_managers')) {
  89.             foreach ($this->container->getParameter('doctrine.entity_managers') as $id) {
  90.                 if (! $this->container->initialized($id)) {
  91.                     continue;
  92.                 }
  93.                 $this->container->get($id)->clear();
  94.             }
  95.         }
  96.         // Close all connections to avoid reaching too many connections in the process when booting again later (tests)
  97.         if (! $this->container->hasParameter('doctrine.connections')) {
  98.             return;
  99.         }
  100.         foreach ($this->container->getParameter('doctrine.connections') as $id) {
  101.             if (! $this->container->initialized($id)) {
  102.                 continue;
  103.             }
  104.             $this->container->get($id)->close();
  105.         }
  106.     }
  107.     /**
  108.      * {@inheritDoc}
  109.      */
  110.     public function registerCommands(Application $application)
  111.     {
  112.     }
  113. }