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

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