PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/pr0055/symfonypizza
PHP | 256 lines | 107 code | 33 blank | 116 comment | 9 complexity | 82eaf1c2a74b99ef7365cf5c48689c2b 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\Annotations\AnnotationReader;
  21. use Doctrine\Common\Persistence\Mapping\MappingException;
  22. /**
  23. * The AnnotationDriver reads the mapping metadata from docblock annotations.
  24. *
  25. * @since 2.2
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  28. * @author Jonathan H. Wage <jonwage@gmail.com>
  29. * @author Roman Borschel <roman@code-factory.org>
  30. */
  31. abstract class AnnotationDriver implements MappingDriver
  32. {
  33. /**
  34. * The AnnotationReader.
  35. *
  36. * @var AnnotationReader
  37. */
  38. protected $reader;
  39. /**
  40. * The paths where to look for mapping files.
  41. *
  42. * @var array
  43. */
  44. protected $paths = [];
  45. /**
  46. * The paths excluded from path where to look for mapping files.
  47. *
  48. * @var array
  49. */
  50. protected $excludePaths = [];
  51. /**
  52. * The file extension of mapping documents.
  53. *
  54. * @var string
  55. */
  56. protected $fileExtension = '.php';
  57. /**
  58. * Cache for AnnotationDriver#getAllClassNames().
  59. *
  60. * @var array|null
  61. */
  62. protected $classNames;
  63. /**
  64. * Name of the entity annotations as keys.
  65. *
  66. * @var array
  67. */
  68. protected $entityAnnotationClasses = [];
  69. /**
  70. * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
  71. * docblock annotations.
  72. *
  73. * @param AnnotationReader $reader The AnnotationReader to use, duck-typed.
  74. * @param string|array|null $paths One or multiple paths where mapping classes can be found.
  75. */
  76. public function __construct($reader, $paths = null)
  77. {
  78. $this->reader = $reader;
  79. if ($paths) {
  80. $this->addPaths((array) $paths);
  81. }
  82. }
  83. /**
  84. * Appends lookup paths to metadata driver.
  85. *
  86. * @param array $paths
  87. *
  88. * @return void
  89. */
  90. public function addPaths(array $paths)
  91. {
  92. $this->paths = array_unique(array_merge($this->paths, $paths));
  93. }
  94. /**
  95. * Retrieves the defined metadata lookup paths.
  96. *
  97. * @return array
  98. */
  99. public function getPaths()
  100. {
  101. return $this->paths;
  102. }
  103. /**
  104. * Append exclude lookup paths to metadata driver.
  105. *
  106. * @param array $paths
  107. */
  108. public function addExcludePaths(array $paths)
  109. {
  110. $this->excludePaths = array_unique(array_merge($this->excludePaths, $paths));
  111. }
  112. /**
  113. * Retrieve the defined metadata lookup exclude paths.
  114. *
  115. * @return array
  116. */
  117. public function getExcludePaths()
  118. {
  119. return $this->excludePaths;
  120. }
  121. /**
  122. * Retrieve the current annotation reader
  123. *
  124. * @return AnnotationReader
  125. */
  126. public function getReader()
  127. {
  128. return $this->reader;
  129. }
  130. /**
  131. * Gets the file extension used to look for mapping files under.
  132. *
  133. * @return string
  134. */
  135. public function getFileExtension()
  136. {
  137. return $this->fileExtension;
  138. }
  139. /**
  140. * Sets the file extension used to look for mapping files under.
  141. *
  142. * @param string $fileExtension The file extension to set.
  143. *
  144. * @return void
  145. */
  146. public function setFileExtension($fileExtension)
  147. {
  148. $this->fileExtension = $fileExtension;
  149. }
  150. /**
  151. * Returns whether the class with the specified name is transient. Only non-transient
  152. * classes, that is entities and mapped superclasses, should have their metadata loaded.
  153. *
  154. * A class is non-transient if it is annotated with an annotation
  155. * from the {@see AnnotationDriver::entityAnnotationClasses}.
  156. *
  157. * @param string $className
  158. *
  159. * @return boolean
  160. */
  161. public function isTransient($className)
  162. {
  163. $classAnnotations = $this->reader->getClassAnnotations(new \ReflectionClass($className));
  164. foreach ($classAnnotations as $annot) {
  165. if (isset($this->entityAnnotationClasses[get_class($annot)])) {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. /**
  172. * {@inheritDoc}
  173. */
  174. public function getAllClassNames()
  175. {
  176. if ($this->classNames !== null) {
  177. return $this->classNames;
  178. }
  179. if (!$this->paths) {
  180. throw MappingException::pathRequired();
  181. }
  182. $classes = [];
  183. $includedFiles = [];
  184. foreach ($this->paths as $path) {
  185. if ( ! is_dir($path)) {
  186. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  187. }
  188. $iterator = new \RegexIterator(
  189. new \RecursiveIteratorIterator(
  190. new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
  191. \RecursiveIteratorIterator::LEAVES_ONLY
  192. ),
  193. '/^.+' . preg_quote($this->fileExtension) . '$/i',
  194. \RecursiveRegexIterator::GET_MATCH
  195. );
  196. foreach ($iterator as $file) {
  197. $sourceFile = $file[0];
  198. if ( ! preg_match('(^phar:)i', $sourceFile)) {
  199. $sourceFile = realpath($sourceFile);
  200. }
  201. foreach ($this->excludePaths as $excludePath) {
  202. $exclude = str_replace('\\', '/', realpath($excludePath));
  203. $current = str_replace('\\', '/', $sourceFile);
  204. if (strpos($current, $exclude) !== false) {
  205. continue 2;
  206. }
  207. }
  208. require_once $sourceFile;
  209. $includedFiles[] = $sourceFile;
  210. }
  211. }
  212. $declared = get_declared_classes();
  213. foreach ($declared as $className) {
  214. $rc = new \ReflectionClass($className);
  215. $sourceFile = $rc->getFileName();
  216. if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
  217. $classes[] = $className;
  218. }
  219. }
  220. $this->classNames = $classes;
  221. return $classes;
  222. }
  223. }