PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/SymfonyFileLocator.php

https://gitlab.com/reasonat/test8
PHP | 239 lines | 136 code | 22 blank | 81 comment | 6 complexity | f813eb32bab2015e0a3cfebb62e42285 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. * The Symfony File Locator makes a simplifying assumptions compared
  23. * to the DefaultFileLocator. By assuming paths only contain entities of a certain
  24. * namespace the mapping files consists of the short classname only.
  25. *
  26. * @author Fabien Potencier <fabien@symfony.com>
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. * @license MIT
  29. */
  30. class SymfonyFileLocator implements FileLocator
  31. {
  32. /**
  33. * The paths where to look for mapping files.
  34. *
  35. * @var array
  36. */
  37. protected $paths = array();
  38. /**
  39. * A map of mapping directory path to namespace prefix used to expand class shortnames.
  40. *
  41. * @var array
  42. */
  43. protected $prefixes = array();
  44. /**
  45. * File extension that is searched for.
  46. *
  47. * @var string|null
  48. */
  49. protected $fileExtension;
  50. /**
  51. * Represents PHP namespace delimiters when looking for files
  52. *
  53. * @var string
  54. */
  55. private $nsSeparator;
  56. /**
  57. * Constructor.
  58. *
  59. * @param array $prefixes
  60. * @param string|null $fileExtension
  61. * @param string $nsSeparator String which would be used when converting FQCN to filename and vice versa. Should not be empty
  62. */
  63. public function __construct(array $prefixes, $fileExtension = null, $nsSeparator = '.')
  64. {
  65. $this->addNamespacePrefixes($prefixes);
  66. $this->fileExtension = $fileExtension;
  67. if (empty($nsSeparator)) {
  68. throw new \InvalidArgumentException('Namespace separator should not be empty');
  69. }
  70. $this->nsSeparator = (string) $nsSeparator;
  71. }
  72. /**
  73. * Adds Namespace Prefixes.
  74. *
  75. * @param array $prefixes
  76. *
  77. * @return void
  78. */
  79. public function addNamespacePrefixes(array $prefixes)
  80. {
  81. $this->prefixes = array_merge($this->prefixes, $prefixes);
  82. $this->paths = array_merge($this->paths, array_keys($prefixes));
  83. }
  84. /**
  85. * Gets Namespace Prefixes.
  86. *
  87. * @return array
  88. */
  89. public function getNamespacePrefixes()
  90. {
  91. return $this->prefixes;
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function getPaths()
  97. {
  98. return $this->paths;
  99. }
  100. /**
  101. * {@inheritDoc}
  102. */
  103. public function getFileExtension()
  104. {
  105. return $this->fileExtension;
  106. }
  107. /**
  108. * Sets the file extension used to look for mapping files under.
  109. *
  110. * @param string $fileExtension The file extension to set.
  111. *
  112. * @return void
  113. */
  114. public function setFileExtension($fileExtension)
  115. {
  116. $this->fileExtension = $fileExtension;
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function fileExists($className)
  122. {
  123. $defaultFileName = str_replace('\\', $this->nsSeparator, $className).$this->fileExtension;
  124. foreach ($this->paths as $path) {
  125. if (!isset($this->prefixes[$path])) {
  126. // global namespace class
  127. if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
  128. return true;
  129. }
  130. continue;
  131. }
  132. $prefix = $this->prefixes[$path];
  133. if (0 !== strpos($className, $prefix.'\\')) {
  134. continue;
  135. }
  136. $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', $this->nsSeparator).$this->fileExtension;
  137. return is_file($filename);
  138. }
  139. return false;
  140. }
  141. /**
  142. * {@inheritDoc}
  143. */
  144. public function getAllClassNames($globalBasename = null)
  145. {
  146. $classes = array();
  147. if ($this->paths) {
  148. foreach ((array) $this->paths as $path) {
  149. if (!is_dir($path)) {
  150. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  151. }
  152. $iterator = new \RecursiveIteratorIterator(
  153. new \RecursiveDirectoryIterator($path),
  154. \RecursiveIteratorIterator::LEAVES_ONLY
  155. );
  156. foreach ($iterator as $file) {
  157. $fileName = $file->getBasename($this->fileExtension);
  158. if ($fileName == $file->getBasename() || $fileName == $globalBasename) {
  159. continue;
  160. }
  161. // NOTE: All files found here means classes are not transient!
  162. if (isset($this->prefixes[$path])) {
  163. // Calculate namespace suffix for given prefix as a relative path from basepath to file path
  164. $nsSuffix = strtr(
  165. substr(realpath($file->getPath()), strlen(realpath($path))),
  166. $this->nsSeparator,
  167. '\\'
  168. );
  169. $classes[] = $this->prefixes[$path] . $nsSuffix . '\\' .str_replace($this->nsSeparator, '\\', $fileName);
  170. } else {
  171. $classes[] = str_replace($this->nsSeparator, '\\', $fileName);
  172. }
  173. }
  174. }
  175. }
  176. return $classes;
  177. }
  178. /**
  179. * {@inheritDoc}
  180. */
  181. public function findMappingFile($className)
  182. {
  183. $defaultFileName = str_replace('\\', $this->nsSeparator, $className).$this->fileExtension;
  184. foreach ($this->paths as $path) {
  185. if (!isset($this->prefixes[$path])) {
  186. if (is_file($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
  187. return $path.DIRECTORY_SEPARATOR.$defaultFileName;
  188. }
  189. continue;
  190. }
  191. $prefix = $this->prefixes[$path];
  192. if (0 !== strpos($className, $prefix.'\\')) {
  193. continue;
  194. }
  195. $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', $this->nsSeparator ).$this->fileExtension;
  196. if (is_file($filename)) {
  197. return $filename;
  198. }
  199. throw MappingException::mappingFileNotFound($className, $filename);
  200. }
  201. throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->fileExtension);
  202. }
  203. }