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

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