DriverChain.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\ORM\Mapping\Driver;
  20. use Doctrine\ORM\Mapping\Driver\Driver,
  21. Doctrine\ORM\Mapping\ClassMetadataInfo,
  22. Doctrine\ORM\Mapping\MappingException;
  23. /**
  24. * The DriverChain allows you to add multiple other mapping drivers for
  25. * certain namespaces
  26. *
  27. * @since 2.0
  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. * @todo Rename: MappingDriverChain or MetadataDriverChain
  33. */
  34. class DriverChain implements Driver
  35. {
  36. /**
  37. * @var array
  38. */
  39. private $_drivers = array();
  40. /**
  41. * Add a nested driver.
  42. *
  43. * @param Driver $nestedDriver
  44. * @param string $namespace
  45. */
  46. public function addDriver(Driver $nestedDriver, $namespace)
  47. {
  48. $this->_drivers[$namespace] = $nestedDriver;
  49. }
  50. /**
  51. * Get the array of nested drivers.
  52. *
  53. * @return array $drivers
  54. */
  55. public function getDrivers()
  56. {
  57. return $this->_drivers;
  58. }
  59. /**
  60. * Loads the metadata for the specified class into the provided container.
  61. *
  62. * @param string $className
  63. * @param ClassMetadataInfo $metadata
  64. */
  65. public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
  66. {
  67. foreach ($this->_drivers as $namespace => $driver) {
  68. if (strpos($className, $namespace) === 0) {
  69. $driver->loadMetadataForClass($className, $metadata);
  70. return;
  71. }
  72. }
  73. throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
  74. }
  75. /**
  76. * Gets the names of all mapped classes known to this driver.
  77. *
  78. * @return array The names of all mapped classes known to this driver.
  79. */
  80. public function getAllClassNames()
  81. {
  82. $classNames = array();
  83. $driverClasses = array();
  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 (strpos($className, $namespace) === 0) {
  91. $classNames[$className] = true;
  92. }
  93. }
  94. }
  95. return array_keys($classNames);
  96. }
  97. /**
  98. * Whether the class with the specified name should have its metadata loaded.
  99. *
  100. * This is only the case for non-transient classes either mapped as an Entity or MappedSuperclass.
  101. *
  102. * @param string $className
  103. * @return boolean
  104. */
  105. public function isTransient($className)
  106. {
  107. foreach ($this->_drivers AS $namespace => $driver) {
  108. if (strpos($className, $namespace) === 0) {
  109. return $driver->isTransient($className);
  110. }
  111. }
  112. // class isTransient, i.e. not an entity or mapped superclass
  113. return true;
  114. }
  115. }