AnnotationDriver.php 5.9 KB

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