PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/iti-note-mvc-php/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/DefaultFileLocator.php

https://bitbucket.org/alessandro-aglietti/itis-leonardo-da-vinci
PHP | 173 lines | 88 code | 12 blank | 73 comment | 0 complexity | 655df7f1fa54fbc18cee0a6c342c948d MD5 | raw file
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Persistence\Mapping\Driver;
  20. use Doctrine\Common\Persistence\Mapping\MappingException;
  21. /**
  22. * Locates the file that contains the metadata information for a given class name.
  23. *
  24. * This behavior is independent of the actual content of the file. It just detects
  25. * the file which is responsible for the given class name.
  26. *
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  29. */
  30. class DefaultFileLocator implements FileLocator
  31. {
  32. /**
  33. * The paths where to look for mapping files.
  34. *
  35. * @var array
  36. */
  37. protected $paths = array();
  38. /**
  39. * The file extension of mapping documents.
  40. *
  41. * @var string|null
  42. */
  43. protected $fileExtension;
  44. /**
  45. * Initializes a new FileDriver that looks in the given path(s) for mapping
  46. * documents and operates in the specified operating mode.
  47. *
  48. * @param string|array $paths One or multiple paths where mapping documents can be found.
  49. * @param string|null $fileExtension The file extension of mapping documents.
  50. */
  51. public function __construct($paths, $fileExtension = null)
  52. {
  53. $this->addPaths((array) $paths);
  54. $this->fileExtension = $fileExtension;
  55. }
  56. /**
  57. * Appends lookup paths to metadata driver.
  58. *
  59. * @param array $paths
  60. *
  61. * @return void
  62. */
  63. public function addPaths(array $paths)
  64. {
  65. $this->paths = array_unique(array_merge($this->paths, $paths));
  66. }
  67. /**
  68. * Retrieves the defined metadata lookup paths.
  69. *
  70. * @return array
  71. */
  72. public function getPaths()
  73. {
  74. return $this->paths;
  75. }
  76. /**
  77. * Gets the file extension used to look for mapping files under.
  78. *
  79. * @return string|null
  80. */
  81. public function getFileExtension()
  82. {
  83. return $this->fileExtension;
  84. }
  85. /**
  86. * Sets the file extension used to look for mapping files under.
  87. *
  88. * @param string|null $fileExtension The file extension to set.
  89. *
  90. * @return void
  91. */
  92. public function setFileExtension($fileExtension)
  93. {
  94. $this->fileExtension = $fileExtension;
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function findMappingFile($className)
  100. {
  101. $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
  102. // Check whether file exists
  103. foreach ($this->paths as $path) {
  104. if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
  105. return $path . DIRECTORY_SEPARATOR . $fileName;
  106. }
  107. }
  108. throw MappingException::mappingFileNotFound($className, $fileName);
  109. }
  110. /**
  111. * {@inheritDoc}
  112. */
  113. public function getAllClassNames($globalBasename)
  114. {
  115. $classes = array();
  116. if ($this->paths) {
  117. foreach ($this->paths as $path) {
  118. if ( ! is_dir($path)) {
  119. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  120. }
  121. $iterator = new \RecursiveIteratorIterator(
  122. new \RecursiveDirectoryIterator($path),
  123. \RecursiveIteratorIterator::LEAVES_ONLY
  124. );
  125. foreach ($iterator as $file) {
  126. $fileName = $file->getBasename($this->fileExtension);
  127. if ($fileName == $file->getBasename() || $fileName == $globalBasename) {
  128. continue;
  129. }
  130. // NOTE: All files found here means classes are not transient!
  131. $classes[] = str_replace('.', '\\', $fileName);
  132. }
  133. }
  134. }
  135. return $classes;
  136. }
  137. /**
  138. * {@inheritDoc}
  139. */
  140. public function fileExists($className)
  141. {
  142. $fileName = str_replace('\\', '.', $className) . $this->fileExtension;
  143. // Check whether file exists
  144. foreach ((array) $this->paths as $path) {
  145. if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. }