MappingDriverChain.php 3.4 KB

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