SimplifiedXmlDriver.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\MappingException;
  21. /**
  22. * XmlDriver that additionally looks for mapping information in a global file.
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. * @author Benjamin Eberlei <kontakt@beberlei.de>
  26. * @license MIT
  27. */
  28. class SimplifiedXmlDriver extends XmlDriver
  29. {
  30. protected $_prefixes = array();
  31. protected $_globalBasename;
  32. protected $_classCache;
  33. protected $_fileExtension = '.orm.xml';
  34. public function __construct($prefixes)
  35. {
  36. $this->addNamespacePrefixes($prefixes);
  37. }
  38. public function setGlobalBasename($file)
  39. {
  40. $this->_globalBasename = $file;
  41. }
  42. public function getGlobalBasename()
  43. {
  44. return $this->_globalBasename;
  45. }
  46. public function addNamespacePrefixes($prefixes)
  47. {
  48. $this->_prefixes = array_merge($this->_prefixes, $prefixes);
  49. $this->addPaths(array_flip($prefixes));
  50. }
  51. public function getNamespacePrefixes()
  52. {
  53. return $this->_prefixes;
  54. }
  55. public function isTransient($className)
  56. {
  57. if (null === $this->_classCache) {
  58. $this->initialize();
  59. }
  60. // The mapping is defined in the global mapping file
  61. if (isset($this->_classCache[$className])) {
  62. return false;
  63. }
  64. try {
  65. $this->_findMappingFile($className);
  66. return false;
  67. } catch (MappingException $e) {
  68. return true;
  69. }
  70. }
  71. public function getAllClassNames()
  72. {
  73. if (null === $this->_classCache) {
  74. $this->initialize();
  75. }
  76. $classes = array();
  77. if ($this->_paths) {
  78. foreach ((array) $this->_paths as $path) {
  79. if (!is_dir($path)) {
  80. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  81. }
  82. $iterator = new \RecursiveIteratorIterator(
  83. new \RecursiveDirectoryIterator($path),
  84. \RecursiveIteratorIterator::LEAVES_ONLY
  85. );
  86. foreach ($iterator as $file) {
  87. $fileName = $file->getBasename($this->_fileExtension);
  88. if ($fileName == $file->getBasename() || $fileName == $this->_globalBasename) {
  89. continue;
  90. }
  91. // NOTE: All files found here means classes are not transient!
  92. if (isset($this->_prefixes[$path])) {
  93. $classes[] = $this->_prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
  94. } else {
  95. $classes[] = str_replace('.', '\\', $fileName);
  96. }
  97. }
  98. }
  99. }
  100. return array_merge($classes, array_keys($this->_classCache));
  101. }
  102. public function getElement($className)
  103. {
  104. if (null === $this->_classCache) {
  105. $this->initialize();
  106. }
  107. if (!isset($this->_classCache[$className])) {
  108. $this->_classCache[$className] = parent::getElement($className);
  109. }
  110. return $this->_classCache[$className];
  111. }
  112. protected function initialize()
  113. {
  114. $this->_classCache = array();
  115. if (null !== $this->_globalBasename) {
  116. foreach ($this->_paths as $path) {
  117. if (is_file($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
  118. $this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
  119. }
  120. }
  121. }
  122. }
  123. protected function _findMappingFile($className)
  124. {
  125. $defaultFileName = str_replace('\\', '.', $className).$this->_fileExtension;
  126. foreach ($this->_paths as $path) {
  127. if (!isset($this->_prefixes[$path])) {
  128. if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
  129. return $path.DIRECTORY_SEPARATOR.$defaultFileName;
  130. }
  131. continue;
  132. }
  133. $prefix = $this->_prefixes[$path];
  134. if (0 !== strpos($className, $prefix.'\\')) {
  135. continue;
  136. }
  137. $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->_fileExtension;
  138. if (is_file($filename)) {
  139. return $filename;
  140. }
  141. throw MappingException::mappingFileNotFound($className, $filename);
  142. }
  143. throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->_fileExtension);
  144. }
  145. }