vendor/doctrine/persistence/src/Persistence/Mapping/Driver/MappingDriverChain.php line 105

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Persistence\Mapping\Driver;
  4. use Doctrine\Persistence\Mapping\ClassMetadata;
  5. use Doctrine\Persistence\Mapping\MappingException;
  6. use function array_keys;
  7. use function rtrim;
  8. use function spl_object_hash;
  9. use function strpos;
  10. /**
  11. * The DriverChain allows you to add multiple other mapping drivers for
  12. * certain namespaces.
  13. */
  14. class MappingDriverChain implements MappingDriver
  15. {
  16. /**
  17. * The default driver.
  18. *
  19. * @var MappingDriver|null
  20. */
  21. private $defaultDriver;
  22. /** @var array<string, MappingDriver> */
  23. private $drivers = [];
  24. /**
  25. * Gets the default driver.
  26. *
  27. * @return MappingDriver|null
  28. */
  29. public function getDefaultDriver()
  30. {
  31. return $this->defaultDriver;
  32. }
  33. /**
  34. * Set the default driver.
  35. *
  36. * @return void
  37. */
  38. public function setDefaultDriver(MappingDriver $driver)
  39. {
  40. $this->defaultDriver = $driver;
  41. }
  42. /**
  43. * Adds a nested driver.
  44. *
  45. * @return void
  46. */
  47. public function addDriver(MappingDriver $nestedDriver, string $namespace)
  48. {
  49. $this->drivers[$namespace] = $nestedDriver;
  50. }
  51. /**
  52. * Gets the array of nested drivers.
  53. *
  54. * @return array<string, MappingDriver> $drivers
  55. */
  56. public function getDrivers()
  57. {
  58. return $this->drivers;
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function loadMetadataForClass(string $className, ClassMetadata $metadata)
  64. {
  65. foreach ($this->drivers as $namespace => $driver) {
  66. if ($this->isInNamespace($className, $namespace)) {
  67. $driver->loadMetadataForClass($className, $metadata);
  68. return;
  69. }
  70. }
  71. if ($this->defaultDriver !== null) {
  72. $this->defaultDriver->loadMetadataForClass($className, $metadata);
  73. return;
  74. }
  75. throw MappingException::classNotFoundInNamespaces($className, array_keys($this->drivers));
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function getAllClassNames()
  81. {
  82. $classNames = [];
  83. $driverClasses = [];
  84. foreach ($this->drivers as $namespace => $driver) {
  85. $oid = spl_object_hash($driver);
  86. if (! isset($driverClasses[$oid])) {
  87. $driverClasses[$oid] = $driver->getAllClassNames();
  88. }
  89. foreach ($driverClasses[$oid] as $className) {
  90. if (! $this->isInNamespace($className, $namespace)) {
  91. continue;
  92. }
  93. $classNames[$className] = true;
  94. }
  95. }
  96. if ($this->defaultDriver !== null) {
  97. foreach ($this->defaultDriver->getAllClassNames() as $className) {
  98. $classNames[$className] = true;
  99. }
  100. }
  101. return array_keys($classNames);
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. public function isTransient(string $className)
  107. {
  108. foreach ($this->drivers as $namespace => $driver) {
  109. if ($this->isInNamespace($className, $namespace)) {
  110. return $driver->isTransient($className);
  111. }
  112. }
  113. if ($this->defaultDriver !== null) {
  114. return $this->defaultDriver->isTransient($className);
  115. }
  116. return true;
  117. }
  118. private function isInNamespace(string $className, string $namespace): bool
  119. {
  120. $namespace = rtrim($namespace, '\\') . '\\';
  121. return strpos($className, $namespace) === 0;
  122. }
  123. }