vendor/doctrine/phpcr-bundle/src/DoctrinePHPCRBundle.php line 25

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Bundle\PHPCRBundle;
  3. use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\InitializerPass;
  4. use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\MigratorPass;
  5. use Doctrine\Bundle\PHPCRBundle\OptionalCommand\Jackalope\InitDoctrineDbalCommand;
  6. use Doctrine\Bundle\PHPCRBundle\OptionalCommand\Jackalope\JackrabbitCommand;
  7. use Doctrine\Bundle\PHPCRBundle\OptionalCommand\ODM\DocumentConvertTranslationCommand;
  8. use Doctrine\Bundle\PHPCRBundle\OptionalCommand\ODM\DocumentMigrateClassCommand;
  9. use Doctrine\Bundle\PHPCRBundle\OptionalCommand\ODM\InfoDoctrineCommand;
  10. use Doctrine\Bundle\PHPCRBundle\OptionalCommand\ODM\VerifyUniqueNodeTypesMappingCommand;
  11. use Doctrine\Common\Util\ClassUtils;
  12. use Doctrine\ODM\PHPCR\Version;
  13. use Jackalope\Session;
  14. use Jackalope\Tools\Console\Command\InitDoctrineDbalCommand as BaseInitDoctrineDbalCommand;
  15. use Jackalope\Tools\Console\Command\JackrabbitCommand as BaseJackrabbitCommand;
  16. use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
  17. use Symfony\Component\Console\Application;
  18. use Symfony\Component\DependencyInjection\Compiler\PassConfig;
  19. use Symfony\Component\DependencyInjection\ContainerBuilder;
  20. use Symfony\Component\DependencyInjection\IntrospectableContainerInterface;
  21. use Symfony\Component\HttpKernel\Bundle\Bundle;
  22. class DoctrinePHPCRBundle extends Bundle
  23. {
  24.     /**
  25.      * Autoloader for proxies.
  26.      *
  27.      * @var \Closure
  28.      */
  29.     private $autoloader;
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function build(ContainerBuilder $container)
  34.     {
  35.         parent::build($container);
  36.         $container->addCompilerPass(new MigratorPass());
  37.         $container->addCompilerPass(new InitializerPass());
  38.         if (class_exists(Version::class)) {
  39.             $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine_phpcr.sessions''doctrine_phpcr.%s_session.event_manager''doctrine_phpcr'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
  40.         }
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function registerCommands(Application $application)
  46.     {
  47.         parent::registerCommands($application);
  48.         if (class_exists(Version::class)) {
  49.             $application->add(new DocumentMigrateClassCommand());
  50.             $application->add(new InfoDoctrineCommand());
  51.             $application->add(new VerifyUniqueNodeTypesMappingCommand());
  52.             $application->add(new DocumentConvertTranslationCommand());
  53.         }
  54.         if (class_exists(BaseJackrabbitCommand::class)) {
  55.             $application->add(new JackrabbitCommand());
  56.         }
  57.         if (class_exists(BaseInitDoctrineDbalCommand::class)) {
  58.             $application->add(new InitDoctrineDbalCommand());
  59.         }
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function boot()
  65.     {
  66.         // Register an autoloader for proxies to avoid issues when unserializing them when the ODM is used.
  67.         if ($this->container->hasParameter('doctrine_phpcr.odm.proxy_namespace')) {
  68.             $namespace $this->container->getParameter('doctrine_phpcr.odm.proxy_namespace');
  69.             $dir $this->container->getParameter('doctrine_phpcr.odm.proxy_dir');
  70.             // See https://github.com/symfony/symfony/pull/3419 for usage of references
  71.             $container = &$this->container;
  72.             $this->autoloader = function ($class) use ($namespace$dir, &$container) {
  73.                 if (=== strpos($class$namespace)) {
  74.                     $fileName str_replace('\\'''substr($class\strlen($namespace) + 1));
  75.                     $file $dir.\DIRECTORY_SEPARATOR.$fileName.'.php';
  76.                     if (!is_file($file) && $container->getParameter('doctrine_phpcr.odm.auto_generate_proxy_classes')) {
  77.                         $originalClassName ClassUtils::getRealClass($class);
  78.                         $registry $container->get('doctrine_phpcr');
  79.                         // Tries to auto-generate the proxy file
  80.                         foreach ($registry->getManagers() as $dm) {
  81.                             if ($dm->getConfiguration()->getAutoGenerateProxyClasses()) {
  82.                                 $classes $dm->getMetadataFactory()->getAllMetadata();
  83.                                 foreach ($classes as $classMetadata) {
  84.                                     if ($classMetadata->name === $originalClassName) {
  85.                                         $dm->getProxyFactory()->generateProxyClasses([$classMetadata]);
  86.                                     }
  87.                                 }
  88.                             }
  89.                         }
  90.                         clearstatcache($file);
  91.                     }
  92.                     if (is_file($file)) {
  93.                         require $file;
  94.                     }
  95.                 }
  96.             };
  97.             spl_autoload_register($this->autoloader);
  98.         }
  99.     }
  100.     /**
  101.      * {@inheritdoc}
  102.      */
  103.     public function shutdown()
  104.     {
  105.         if (null !== $this->autoloader) {
  106.             spl_autoload_unregister($this->autoloader);
  107.             $this->autoloader null;
  108.         }
  109.         $this->clearDocumentManagers();
  110.         $this->closeConnections();
  111.     }
  112.     /**
  113.      * Clear all document managers to clear references to entities for GC.
  114.      */
  115.     private function clearDocumentManagers()
  116.     {
  117.         if (!$this->container->hasParameter('doctrine_phpcr.odm.document_managers')) {
  118.             return;
  119.         }
  120.         foreach ($this->container->getParameter('doctrine_phpcr.odm.document_managers') as $id) {
  121.             if ($this->container instanceof IntrospectableContainerInterface && !$this->container->initialized($id)) {
  122.                 continue;
  123.             }
  124.             $this->container->get($id)->clear();
  125.         }
  126.     }
  127.     /**
  128.      * Close all connections to avoid reaching too many connections in the process when booting again later (tests).
  129.      */
  130.     private function closeConnections()
  131.     {
  132.         if (!$this->container->hasParameter('doctrine_phpcr.sessions')) {
  133.             return;
  134.         }
  135.         foreach ($this->container->getParameter('doctrine_phpcr.sessions') as $id) {
  136.             if ($this->container instanceof IntrospectableContainerInterface && !$this->container->initialized($id)) {
  137.                 continue;
  138.             }
  139.             $session $this->container->get($id);
  140.             if (!$session instanceof Session) {
  141.                 return;
  142.             }
  143.             $session->getTransport()->logout();
  144.         }
  145.     }
  146. }