MappingDriverChain.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Persistence\Mapping\Driver;
  20. use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver,
  21. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  22. Doctrine\Common\Persistence\Mapping\MappingException;
  23. /**
  24. * The DriverChain allows you to add multiple other mapping drivers for
  25. * certain namespaces
  26. *
  27. * @since 2.2
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  30. * @author Jonathan H. Wage <jonwage@gmail.com>
  31. * @author Roman Borschel <roman@code-factory.org>
  32. */
  33. class MappingDriverChain implements MappingDriver
  34. {
  35. /**
  36. * @var array
  37. */
  38. private $drivers = array();
  39. /**
  40. * Add a nested driver.
  41. *
  42. * @param Driver $nestedDriver
  43. * @param string $namespace
  44. */
  45. public function addDriver(MappingDriver $nestedDriver, $namespace)
  46. {
  47. $this->drivers[$namespace] = $nestedDriver;
  48. }
  49. /**
  50. * Get the array of nested drivers.
  51. *
  52. * @return array $drivers
  53. */
  54. public function getDrivers()
  55. {
  56. return $this->drivers;
  57. }
  58. /**
  59. * Loads the metadata for the specified class into the provided container.
  60. *
  61. * @param string $className
  62. * @param ClassMetadataInfo $metadata
  63. */
  64. public function loadMetadataForClass($className, ClassMetadata $metadata)
  65. {
  66. foreach ($this->drivers as $namespace => $driver) {
  67. if (strpos($className, $namespace) === 0) {
  68. $driver->loadMetadataForClass($className, $metadata);
  69. return;
  70. }
  71. }
  72. throw MappingException::classNotFoundInNamespaces($className, array_keys($this->drivers));
  73. }
  74. /**
  75. * Gets the names of all mapped classes known to this driver.
  76. *
  77. * @return array The names of all mapped classes known to this driver.
  78. */
  79. public function getAllClassNames()
  80. {
  81. $classNames = array();
  82. $driverClasses = array();
  83. foreach ($this->drivers AS $namespace => $driver) {
  84. $oid = spl_object_hash($driver);
  85. if (!isset($driverClasses[$oid])) {
  86. $driverClasses[$oid] = $driver->getAllClassNames();
  87. }
  88. foreach ($driverClasses[$oid] AS $className) {
  89. if (strpos($className, $namespace) === 0) {
  90. $classNames[$className] = true;
  91. }
  92. }
  93. }
  94. return array_keys($classNames);
  95. }
  96. /**
  97. * Whether the class with the specified name should have its metadata loaded.
  98. *
  99. * This is only the case for non-transient classes either mapped as an Entity or MappedSuperclass.
  100. *
  101. * @param string $className
  102. * @return boolean
  103. */
  104. public function isTransient($className)
  105. {
  106. foreach ($this->drivers AS $namespace => $driver) {
  107. if (strpos($className, $namespace) === 0) {
  108. return $driver->isTransient($className);
  109. }
  110. }
  111. // class isTransient, i.e. not an entity or mapped superclass
  112. return true;
  113. }
  114. }