SimplifiedYamlDriver.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * YamlDriver 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 SimplifiedYamlDriver extends YamlDriver
  29. {
  30. protected $_prefixes = array();
  31. protected $_globalBasename;
  32. protected $_classCache;
  33. protected $_fileExtension = '.orm.yml';
  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 addNamespacePrefix($prefix, $path)
  52. {
  53. $this->_prefixes[$path] = $prefix;
  54. }
  55. public function getNamespacePrefixes()
  56. {
  57. return $this->_prefixes;
  58. }
  59. public function isTransient($className)
  60. {
  61. if (null === $this->_classCache) {
  62. $this->initialize();
  63. }
  64. // The mapping is defined in the global mapping file
  65. if (isset($this->_classCache[$className])) {
  66. return false;
  67. }
  68. try {
  69. $this->_findMappingFile($className);
  70. return false;
  71. } catch (MappingException $e) {
  72. return true;
  73. }
  74. }
  75. public function getAllClassNames()
  76. {
  77. if (null === $this->_classCache) {
  78. $this->initialize();
  79. }
  80. $classes = array();
  81. if ($this->_paths) {
  82. foreach ((array) $this->_paths as $path) {
  83. if (!is_dir($path)) {
  84. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  85. }
  86. $iterator = new \RecursiveIteratorIterator(
  87. new \RecursiveDirectoryIterator($path),
  88. \RecursiveIteratorIterator::LEAVES_ONLY
  89. );
  90. foreach ($iterator as $file) {
  91. $fileName = $file->getBasename($this->_fileExtension);
  92. if ($fileName == $file->getBasename() || $fileName == $this->_globalBasename) {
  93. continue;
  94. }
  95. // NOTE: All files found here means classes are not transient!
  96. if (isset($this->_prefixes[$path])) {
  97. $classes[] = $this->_prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
  98. } else {
  99. $classes[] = str_replace('.', '\\', $fileName);
  100. }
  101. }
  102. }
  103. }
  104. return array_merge($classes, array_keys($this->_classCache));
  105. }
  106. public function getElement($className)
  107. {
  108. if (null === $this->_classCache) {
  109. $this->initialize();
  110. }
  111. if (!isset($this->_classCache[$className])) {
  112. $this->_classCache[$className] = parent::getElement($className);
  113. }
  114. return $this->_classCache[$className];
  115. }
  116. protected function initialize()
  117. {
  118. $this->_classCache = array();
  119. if (null !== $this->_globalBasename) {
  120. foreach ($this->_paths as $path) {
  121. if (is_file($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
  122. $this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
  123. }
  124. }
  125. }
  126. }
  127. protected function _findMappingFile($className)
  128. {
  129. $defaultFileName = str_replace('\\', '.', $className).$this->_fileExtension;
  130. foreach ($this->_paths as $path) {
  131. if (!isset($this->_prefixes[$path])) {
  132. if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
  133. return $path.DIRECTORY_SEPARATOR.$defaultFileName;
  134. }
  135. continue;
  136. }
  137. $prefix = $this->_prefixes[$path];
  138. if (0 !== strpos($className, $prefix.'\\')) {
  139. continue;
  140. }
  141. $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->_fileExtension;
  142. if (is_file($filename)) {
  143. return $filename;
  144. }
  145. throw MappingException::mappingFileNotFound($className, $filename);
  146. }
  147. throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->_fileExtension);
  148. }
  149. }