AnnotationDriver.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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\ORM\Mapping\Driver;
  20. use Doctrine\Common\Cache\ArrayCache,
  21. Doctrine\Common\Annotations\AnnotationReader,
  22. Doctrine\Common\Annotations\AnnotationRegistry,
  23. Doctrine\ORM\Mapping\ClassMetadataInfo,
  24. Doctrine\ORM\Mapping\MappingException;
  25. /**
  26. * The AnnotationDriver reads the mapping metadata from docblock annotations.
  27. *
  28. * @since 2.0
  29. * @author Benjamin Eberlei <kontakt@beberlei.de>
  30. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  31. * @author Jonathan H. Wage <jonwage@gmail.com>
  32. * @author Roman Borschel <roman@code-factory.org>
  33. */
  34. class AnnotationDriver implements Driver
  35. {
  36. /**
  37. * The AnnotationReader.
  38. *
  39. * @var AnnotationReader
  40. */
  41. protected $_reader;
  42. /**
  43. * The paths where to look for mapping files.
  44. *
  45. * @var array
  46. */
  47. protected $_paths = array();
  48. /**
  49. * The file extension of mapping documents.
  50. *
  51. * @var string
  52. */
  53. protected $_fileExtension = '.php';
  54. /**
  55. * @param array
  56. */
  57. protected $_classNames;
  58. /**
  59. * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
  60. * docblock annotations.
  61. *
  62. * @param AnnotationReader $reader The AnnotationReader to use, duck-typed.
  63. * @param string|array $paths One or multiple paths where mapping classes can be found.
  64. */
  65. public function __construct($reader, $paths = null)
  66. {
  67. $this->_reader = $reader;
  68. if ($paths) {
  69. $this->addPaths((array) $paths);
  70. }
  71. }
  72. /**
  73. * Append lookup paths to metadata driver.
  74. *
  75. * @param array $paths
  76. */
  77. public function addPaths(array $paths)
  78. {
  79. $this->_paths = array_unique(array_merge($this->_paths, $paths));
  80. }
  81. /**
  82. * Retrieve the defined metadata lookup paths.
  83. *
  84. * @return array
  85. */
  86. public function getPaths()
  87. {
  88. return $this->_paths;
  89. }
  90. /**
  91. * Retrieve the current annotation reader
  92. *
  93. * @return AnnotationReader
  94. */
  95. public function getReader()
  96. {
  97. return $this->_reader;
  98. }
  99. /**
  100. * Get the file extension used to look for mapping files under
  101. *
  102. * @return void
  103. */
  104. public function getFileExtension()
  105. {
  106. return $this->_fileExtension;
  107. }
  108. /**
  109. * Set the file extension used to look for mapping files under
  110. *
  111. * @param string $fileExtension The file extension to set
  112. * @return void
  113. */
  114. public function setFileExtension($fileExtension)
  115. {
  116. $this->_fileExtension = $fileExtension;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
  122. {
  123. $class = $metadata->getReflectionClass();
  124. if (!$class) {
  125. // this happens when running annotation driver in combination with
  126. // static reflection services. This is not the nicest fix
  127. $class = new \ReflectionClass($metadata->name);
  128. }
  129. $classAnnotations = $this->_reader->getClassAnnotations($class);
  130. if ($classAnnotations) {
  131. foreach ($classAnnotations as $key => $annot) {
  132. if ( ! is_numeric($key)) {
  133. continue;
  134. }
  135. $classAnnotations[get_class($annot)] = $annot;
  136. }
  137. }
  138. // Evaluate Entity annotation
  139. if (isset($classAnnotations['Doctrine\ORM\Mapping\Entity'])) {
  140. $entityAnnot = $classAnnotations['Doctrine\ORM\Mapping\Entity'];
  141. if ($entityAnnot->repositoryClass !== null) {
  142. $metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
  143. }
  144. if ($entityAnnot->readOnly) {
  145. $metadata->markReadOnly();
  146. }
  147. } else if (isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'])) {
  148. $mappedSuperclassAnnot = $classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'];
  149. $metadata->setCustomRepositoryClass($mappedSuperclassAnnot->repositoryClass);
  150. $metadata->isMappedSuperclass = true;
  151. } else {
  152. throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
  153. }
  154. // Evaluate Table annotation
  155. if (isset($classAnnotations['Doctrine\ORM\Mapping\Table'])) {
  156. $tableAnnot = $classAnnotations['Doctrine\ORM\Mapping\Table'];
  157. $primaryTable = array(
  158. 'name' => $tableAnnot->name,
  159. 'schema' => $tableAnnot->schema
  160. );
  161. if ($tableAnnot->indexes !== null) {
  162. foreach ($tableAnnot->indexes as $indexAnnot) {
  163. $index = array('columns' => $indexAnnot->columns);
  164. if ( ! empty($indexAnnot->name)) {
  165. $primaryTable['indexes'][$indexAnnot->name] = $index;
  166. } else {
  167. $primaryTable['indexes'][] = $index;
  168. }
  169. }
  170. }
  171. if ($tableAnnot->uniqueConstraints !== null) {
  172. foreach ($tableAnnot->uniqueConstraints as $uniqueConstraintAnnot) {
  173. $uniqueConstraint = array('columns' => $uniqueConstraintAnnot->columns);
  174. if ( ! empty($uniqueConstraintAnnot->name)) {
  175. $primaryTable['uniqueConstraints'][$uniqueConstraintAnnot->name] = $uniqueConstraint;
  176. } else {
  177. $primaryTable['uniqueConstraints'][] = $uniqueConstraint;
  178. }
  179. }
  180. }
  181. $metadata->setPrimaryTable($primaryTable);
  182. }
  183. // Evaluate NamedQueries annotation
  184. if (isset($classAnnotations['Doctrine\ORM\Mapping\NamedQueries'])) {
  185. $namedQueriesAnnot = $classAnnotations['Doctrine\ORM\Mapping\NamedQueries'];
  186. if (!is_array($namedQueriesAnnot->value)) {
  187. throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations.");
  188. }
  189. foreach ($namedQueriesAnnot->value as $namedQuery) {
  190. if (!($namedQuery instanceof \Doctrine\ORM\Mapping\NamedQuery)) {
  191. throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations.");
  192. }
  193. $metadata->addNamedQuery(array(
  194. 'name' => $namedQuery->name,
  195. 'query' => $namedQuery->query
  196. ));
  197. }
  198. }
  199. // Evaluate InheritanceType annotation
  200. if (isset($classAnnotations['Doctrine\ORM\Mapping\InheritanceType'])) {
  201. $inheritanceTypeAnnot = $classAnnotations['Doctrine\ORM\Mapping\InheritanceType'];
  202. $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value));
  203. if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) {
  204. // Evaluate DiscriminatorColumn annotation
  205. if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'])) {
  206. $discrColumnAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'];
  207. $metadata->setDiscriminatorColumn(array(
  208. 'name' => $discrColumnAnnot->name,
  209. 'type' => $discrColumnAnnot->type,
  210. 'length' => $discrColumnAnnot->length
  211. ));
  212. } else {
  213. $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255));
  214. }
  215. // Evaluate DiscriminatorMap annotation
  216. if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorMap'])) {
  217. $discrMapAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorMap'];
  218. $metadata->setDiscriminatorMap($discrMapAnnot->value);
  219. }
  220. }
  221. }
  222. // Evaluate DoctrineChangeTrackingPolicy annotation
  223. if (isset($classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'])) {
  224. $changeTrackingAnnot = $classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'];
  225. $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_' . $changeTrackingAnnot->value));
  226. }
  227. // Evaluate annotations on properties/fields
  228. foreach ($class->getProperties() as $property) {
  229. if ($metadata->isMappedSuperclass && ! $property->isPrivate()
  230. ||
  231. $metadata->isInheritedField($property->name)
  232. ||
  233. $metadata->isInheritedAssociation($property->name)) {
  234. continue;
  235. }
  236. $mapping = array();
  237. $mapping['fieldName'] = $property->getName();
  238. // Check for JoinColummn/JoinColumns annotations
  239. $joinColumns = array();
  240. if ($joinColumnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumn')) {
  241. $joinColumns[] = array(
  242. 'name' => $joinColumnAnnot->name,
  243. 'referencedColumnName' => $joinColumnAnnot->referencedColumnName,
  244. 'unique' => $joinColumnAnnot->unique,
  245. 'nullable' => $joinColumnAnnot->nullable,
  246. 'onDelete' => $joinColumnAnnot->onDelete,
  247. 'columnDefinition' => $joinColumnAnnot->columnDefinition,
  248. );
  249. } else if ($joinColumnsAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumns')) {
  250. foreach ($joinColumnsAnnot->value as $joinColumn) {
  251. $joinColumns[] = array(
  252. 'name' => $joinColumn->name,
  253. 'referencedColumnName' => $joinColumn->referencedColumnName,
  254. 'unique' => $joinColumn->unique,
  255. 'nullable' => $joinColumn->nullable,
  256. 'onDelete' => $joinColumn->onDelete,
  257. 'columnDefinition' => $joinColumn->columnDefinition,
  258. );
  259. }
  260. }
  261. // Field can only be annotated with one of:
  262. // @Column, @OneToOne, @OneToMany, @ManyToOne, @ManyToMany
  263. if ($columnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Column')) {
  264. if ($columnAnnot->type == null) {
  265. throw MappingException::propertyTypeIsRequired($className, $property->getName());
  266. }
  267. $mapping['type'] = $columnAnnot->type;
  268. $mapping['length'] = $columnAnnot->length;
  269. $mapping['precision'] = $columnAnnot->precision;
  270. $mapping['scale'] = $columnAnnot->scale;
  271. $mapping['nullable'] = $columnAnnot->nullable;
  272. $mapping['unique'] = $columnAnnot->unique;
  273. if ($columnAnnot->options) {
  274. $mapping['options'] = $columnAnnot->options;
  275. }
  276. if (isset($columnAnnot->name)) {
  277. $mapping['columnName'] = $columnAnnot->name;
  278. }
  279. if (isset($columnAnnot->columnDefinition)) {
  280. $mapping['columnDefinition'] = $columnAnnot->columnDefinition;
  281. }
  282. if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
  283. $mapping['id'] = true;
  284. }
  285. if ($generatedValueAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\GeneratedValue')) {
  286. $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy));
  287. }
  288. if ($versionAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Version')) {
  289. $metadata->setVersionMapping($mapping);
  290. }
  291. $metadata->mapField($mapping);
  292. // Check for SequenceGenerator/TableGenerator definition
  293. if ($seqGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\SequenceGenerator')) {
  294. $metadata->setSequenceGeneratorDefinition(array(
  295. 'sequenceName' => $seqGeneratorAnnot->sequenceName,
  296. 'allocationSize' => $seqGeneratorAnnot->allocationSize,
  297. 'initialValue' => $seqGeneratorAnnot->initialValue
  298. ));
  299. } else if ($tblGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\TableGenerator')) {
  300. throw MappingException::tableIdGeneratorNotImplemented($className);
  301. }
  302. } else if ($oneToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToOne')) {
  303. if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
  304. $mapping['id'] = true;
  305. }
  306. $mapping['targetEntity'] = $oneToOneAnnot->targetEntity;
  307. $mapping['joinColumns'] = $joinColumns;
  308. $mapping['mappedBy'] = $oneToOneAnnot->mappedBy;
  309. $mapping['inversedBy'] = $oneToOneAnnot->inversedBy;
  310. $mapping['cascade'] = $oneToOneAnnot->cascade;
  311. $mapping['orphanRemoval'] = $oneToOneAnnot->orphanRemoval;
  312. $mapping['fetch'] = $this->getFetchMode($className, $oneToOneAnnot->fetch);
  313. $metadata->mapOneToOne($mapping);
  314. } else if ($oneToManyAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToMany')) {
  315. $mapping['mappedBy'] = $oneToManyAnnot->mappedBy;
  316. $mapping['targetEntity'] = $oneToManyAnnot->targetEntity;
  317. $mapping['cascade'] = $oneToManyAnnot->cascade;
  318. $mapping['indexBy'] = $oneToManyAnnot->indexBy;
  319. $mapping['orphanRemoval'] = $oneToManyAnnot->orphanRemoval;
  320. $mapping['fetch'] = $this->getFetchMode($className, $oneToManyAnnot->fetch);
  321. if ($orderByAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OrderBy')) {
  322. $mapping['orderBy'] = $orderByAnnot->value;
  323. }
  324. $metadata->mapOneToMany($mapping);
  325. } else if ($manyToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToOne')) {
  326. if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
  327. $mapping['id'] = true;
  328. }
  329. $mapping['joinColumns'] = $joinColumns;
  330. $mapping['cascade'] = $manyToOneAnnot->cascade;
  331. $mapping['inversedBy'] = $manyToOneAnnot->inversedBy;
  332. $mapping['targetEntity'] = $manyToOneAnnot->targetEntity;
  333. $mapping['fetch'] = $this->getFetchMode($className, $manyToOneAnnot->fetch);
  334. $metadata->mapManyToOne($mapping);
  335. } else if ($manyToManyAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToMany')) {
  336. $joinTable = array();
  337. if ($joinTableAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinTable')) {
  338. $joinTable = array(
  339. 'name' => $joinTableAnnot->name,
  340. 'schema' => $joinTableAnnot->schema
  341. );
  342. foreach ($joinTableAnnot->joinColumns as $joinColumn) {
  343. $joinTable['joinColumns'][] = array(
  344. 'name' => $joinColumn->name,
  345. 'referencedColumnName' => $joinColumn->referencedColumnName,
  346. 'unique' => $joinColumn->unique,
  347. 'nullable' => $joinColumn->nullable,
  348. 'onDelete' => $joinColumn->onDelete,
  349. 'columnDefinition' => $joinColumn->columnDefinition,
  350. );
  351. }
  352. foreach ($joinTableAnnot->inverseJoinColumns as $joinColumn) {
  353. $joinTable['inverseJoinColumns'][] = array(
  354. 'name' => $joinColumn->name,
  355. 'referencedColumnName' => $joinColumn->referencedColumnName,
  356. 'unique' => $joinColumn->unique,
  357. 'nullable' => $joinColumn->nullable,
  358. 'onDelete' => $joinColumn->onDelete,
  359. 'columnDefinition' => $joinColumn->columnDefinition,
  360. );
  361. }
  362. }
  363. $mapping['joinTable'] = $joinTable;
  364. $mapping['targetEntity'] = $manyToManyAnnot->targetEntity;
  365. $mapping['mappedBy'] = $manyToManyAnnot->mappedBy;
  366. $mapping['inversedBy'] = $manyToManyAnnot->inversedBy;
  367. $mapping['cascade'] = $manyToManyAnnot->cascade;
  368. $mapping['indexBy'] = $manyToManyAnnot->indexBy;
  369. $mapping['orphanRemoval'] = $manyToManyAnnot->orphanRemoval;
  370. $mapping['fetch'] = $this->getFetchMode($className, $manyToManyAnnot->fetch);
  371. if ($orderByAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OrderBy')) {
  372. $mapping['orderBy'] = $orderByAnnot->value;
  373. }
  374. $metadata->mapManyToMany($mapping);
  375. }
  376. }
  377. // Evaluate @HasLifecycleCallbacks annotation
  378. if (isset($classAnnotations['Doctrine\ORM\Mapping\HasLifecycleCallbacks'])) {
  379. foreach ($class->getMethods() as $method) {
  380. // filter for the declaring class only, callbacks from parents will already be registered.
  381. if ($method->isPublic() && $method->getDeclaringClass()->getName() == $class->name) {
  382. $annotations = $this->_reader->getMethodAnnotations($method);
  383. if ($annotations) {
  384. foreach ($annotations as $key => $annot) {
  385. if ( ! is_numeric($key)) {
  386. continue;
  387. }
  388. $annotations[get_class($annot)] = $annot;
  389. }
  390. }
  391. if (isset($annotations['Doctrine\ORM\Mapping\PrePersist'])) {
  392. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::prePersist);
  393. }
  394. if (isset($annotations['Doctrine\ORM\Mapping\PostPersist'])) {
  395. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postPersist);
  396. }
  397. if (isset($annotations['Doctrine\ORM\Mapping\PreUpdate'])) {
  398. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preUpdate);
  399. }
  400. if (isset($annotations['Doctrine\ORM\Mapping\PostUpdate'])) {
  401. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postUpdate);
  402. }
  403. if (isset($annotations['Doctrine\ORM\Mapping\PreRemove'])) {
  404. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preRemove);
  405. }
  406. if (isset($annotations['Doctrine\ORM\Mapping\PostRemove'])) {
  407. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postRemove);
  408. }
  409. if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) {
  410. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad);
  411. }
  412. if (isset($annotations['Doctrine\ORM\Mapping\PreFlush'])) {
  413. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preFlush);
  414. }
  415. }
  416. }
  417. }
  418. }
  419. /**
  420. * Whether the class with the specified name is transient. Only non-transient
  421. * classes, that is entities and mapped superclasses, should have their metadata loaded.
  422. * A class is non-transient if it is annotated with either @Entity or
  423. * @MappedSuperclass in the class doc block.
  424. *
  425. * @param string $className
  426. * @return boolean
  427. */
  428. public function isTransient($className)
  429. {
  430. $classAnnotations = $this->_reader->getClassAnnotations(new \ReflectionClass($className));
  431. if ($classAnnotations && is_numeric(key($classAnnotations))) {
  432. foreach ($classAnnotations as $annot) {
  433. if ($annot instanceof \Doctrine\ORM\Mapping\Entity) {
  434. return false;
  435. }
  436. if ($annot instanceof \Doctrine\ORM\Mapping\MappedSuperclass) {
  437. return false;
  438. }
  439. }
  440. return true;
  441. }
  442. return ! isset($classAnnotations['Doctrine\ORM\Mapping\Entity']) &&
  443. ! isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass']);
  444. }
  445. /**
  446. * {@inheritDoc}
  447. */
  448. public function getAllClassNames()
  449. {
  450. if ($this->_classNames !== null) {
  451. return $this->_classNames;
  452. }
  453. if (!$this->_paths) {
  454. throw MappingException::pathRequired();
  455. }
  456. $classes = array();
  457. $includedFiles = array();
  458. foreach ($this->_paths as $path) {
  459. if ( ! is_dir($path)) {
  460. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  461. }
  462. $iterator = new \RegexIterator(
  463. new \RecursiveIteratorIterator(
  464. new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
  465. \RecursiveIteratorIterator::LEAVES_ONLY
  466. ),
  467. '/^.+' . str_replace('.', '\.', $this->_fileExtension) . '$/i',
  468. \RecursiveRegexIterator::GET_MATCH
  469. );
  470. foreach ($iterator as $file) {
  471. $sourceFile = realpath($file[0]);
  472. require_once $sourceFile;
  473. $includedFiles[] = $sourceFile;
  474. }
  475. }
  476. $declared = get_declared_classes();
  477. foreach ($declared as $className) {
  478. $rc = new \ReflectionClass($className);
  479. $sourceFile = $rc->getFileName();
  480. if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
  481. $classes[] = $className;
  482. }
  483. }
  484. $this->_classNames = $classes;
  485. return $classes;
  486. }
  487. /**
  488. * Attempts to resolve the fetch mode.
  489. *
  490. * @param string $className The class name
  491. * @param string $fetchMode The fetch mode
  492. * @return integer The fetch mode as defined in ClassMetadata
  493. * @throws MappingException If the fetch mode is not valid
  494. */
  495. private function getFetchMode($className, $fetchMode)
  496. {
  497. if(!defined('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode)) {
  498. throw MappingException::invalidFetchMode($className, $fetchMode);
  499. }
  500. return constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode);
  501. }
  502. /**
  503. * Factory method for the Annotation Driver
  504. *
  505. * @param array|string $paths
  506. * @param AnnotationReader $reader
  507. * @return AnnotationDriver
  508. */
  509. static public function create($paths = array(), AnnotationReader $reader = null)
  510. {
  511. if ($reader == null) {
  512. $reader = new AnnotationReader();
  513. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  514. }
  515. return new self($reader, $paths);
  516. }
  517. }