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

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

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