AnnotationDriver.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 && is_numeric(key($classAnnotations))) {
  131. foreach ($classAnnotations as $annot) {
  132. $classAnnotations[get_class($annot)] = $annot;
  133. }
  134. }
  135. // Evaluate Entity annotation
  136. if (isset($classAnnotations['Doctrine\ORM\Mapping\Entity'])) {
  137. $entityAnnot = $classAnnotations['Doctrine\ORM\Mapping\Entity'];
  138. if ($entityAnnot->repositoryClass !== null) {
  139. $metadata->setCustomRepositoryClass($entityAnnot->repositoryClass);
  140. }
  141. if ($entityAnnot->readOnly) {
  142. $metadata->markReadOnly();
  143. }
  144. } else if (isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'])) {
  145. $mappedSuperclassAnnot = $classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass'];
  146. $metadata->setCustomRepositoryClass($mappedSuperclassAnnot->repositoryClass);
  147. $metadata->isMappedSuperclass = true;
  148. } else {
  149. throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
  150. }
  151. // Evaluate Table annotation
  152. if (isset($classAnnotations['Doctrine\ORM\Mapping\Table'])) {
  153. $tableAnnot = $classAnnotations['Doctrine\ORM\Mapping\Table'];
  154. $primaryTable = array(
  155. 'name' => $tableAnnot->name,
  156. 'schema' => $tableAnnot->schema
  157. );
  158. if ($tableAnnot->indexes !== null) {
  159. foreach ($tableAnnot->indexes as $indexAnnot) {
  160. $index = array('columns' => $indexAnnot->columns);
  161. if ( ! empty($indexAnnot->name)) {
  162. $primaryTable['indexes'][$indexAnnot->name] = $index;
  163. } else {
  164. $primaryTable['indexes'][] = $index;
  165. }
  166. }
  167. }
  168. if ($tableAnnot->uniqueConstraints !== null) {
  169. foreach ($tableAnnot->uniqueConstraints as $uniqueConstraintAnnot) {
  170. $uniqueConstraint = array('columns' => $uniqueConstraintAnnot->columns);
  171. if ( ! empty($uniqueConstraintAnnot->name)) {
  172. $primaryTable['uniqueConstraints'][$uniqueConstraintAnnot->name] = $uniqueConstraint;
  173. } else {
  174. $primaryTable['uniqueConstraints'][] = $uniqueConstraint;
  175. }
  176. }
  177. }
  178. $metadata->setPrimaryTable($primaryTable);
  179. }
  180. // Evaluate NamedQueries annotation
  181. if (isset($classAnnotations['Doctrine\ORM\Mapping\NamedQueries'])) {
  182. $namedQueriesAnnot = $classAnnotations['Doctrine\ORM\Mapping\NamedQueries'];
  183. if (!is_array($namedQueriesAnnot->value)) {
  184. throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations.");
  185. }
  186. foreach ($namedQueriesAnnot->value as $namedQuery) {
  187. if (!($namedQuery instanceof \Doctrine\ORM\Mapping\NamedQuery)) {
  188. throw new \UnexpectedValueException("@NamedQueries should contain an array of @NamedQuery annotations.");
  189. }
  190. $metadata->addNamedQuery(array(
  191. 'name' => $namedQuery->name,
  192. 'query' => $namedQuery->query
  193. ));
  194. }
  195. }
  196. // Evaluate InheritanceType annotation
  197. if (isset($classAnnotations['Doctrine\ORM\Mapping\InheritanceType'])) {
  198. $inheritanceTypeAnnot = $classAnnotations['Doctrine\ORM\Mapping\InheritanceType'];
  199. $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceTypeAnnot->value));
  200. if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) {
  201. // Evaluate DiscriminatorColumn annotation
  202. if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'])) {
  203. $discrColumnAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorColumn'];
  204. $metadata->setDiscriminatorColumn(array(
  205. 'name' => $discrColumnAnnot->name,
  206. 'type' => $discrColumnAnnot->type,
  207. 'length' => $discrColumnAnnot->length
  208. ));
  209. } else {
  210. $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255));
  211. }
  212. // Evaluate DiscriminatorMap annotation
  213. if (isset($classAnnotations['Doctrine\ORM\Mapping\DiscriminatorMap'])) {
  214. $discrMapAnnot = $classAnnotations['Doctrine\ORM\Mapping\DiscriminatorMap'];
  215. $metadata->setDiscriminatorMap($discrMapAnnot->value);
  216. }
  217. }
  218. }
  219. // Evaluate DoctrineChangeTrackingPolicy annotation
  220. if (isset($classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'])) {
  221. $changeTrackingAnnot = $classAnnotations['Doctrine\ORM\Mapping\ChangeTrackingPolicy'];
  222. $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_' . $changeTrackingAnnot->value));
  223. }
  224. // Evaluate annotations on properties/fields
  225. foreach ($class->getProperties() as $property) {
  226. if ($metadata->isMappedSuperclass && ! $property->isPrivate()
  227. ||
  228. $metadata->isInheritedField($property->name)
  229. ||
  230. $metadata->isInheritedAssociation($property->name)) {
  231. continue;
  232. }
  233. $mapping = array();
  234. $mapping['fieldName'] = $property->getName();
  235. // Check for JoinColummn/JoinColumns annotations
  236. $joinColumns = array();
  237. if ($joinColumnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumn')) {
  238. $joinColumns[] = array(
  239. 'name' => $joinColumnAnnot->name,
  240. 'referencedColumnName' => $joinColumnAnnot->referencedColumnName,
  241. 'unique' => $joinColumnAnnot->unique,
  242. 'nullable' => $joinColumnAnnot->nullable,
  243. 'onDelete' => $joinColumnAnnot->onDelete,
  244. 'columnDefinition' => $joinColumnAnnot->columnDefinition,
  245. );
  246. } else if ($joinColumnsAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinColumns')) {
  247. foreach ($joinColumnsAnnot->value as $joinColumn) {
  248. $joinColumns[] = array(
  249. 'name' => $joinColumn->name,
  250. 'referencedColumnName' => $joinColumn->referencedColumnName,
  251. 'unique' => $joinColumn->unique,
  252. 'nullable' => $joinColumn->nullable,
  253. 'onDelete' => $joinColumn->onDelete,
  254. 'columnDefinition' => $joinColumn->columnDefinition,
  255. );
  256. }
  257. }
  258. // Field can only be annotated with one of:
  259. // @Column, @OneToOne, @OneToMany, @ManyToOne, @ManyToMany
  260. if ($columnAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Column')) {
  261. if ($columnAnnot->type == null) {
  262. throw MappingException::propertyTypeIsRequired($className, $property->getName());
  263. }
  264. $mapping['type'] = $columnAnnot->type;
  265. $mapping['length'] = $columnAnnot->length;
  266. $mapping['precision'] = $columnAnnot->precision;
  267. $mapping['scale'] = $columnAnnot->scale;
  268. $mapping['nullable'] = $columnAnnot->nullable;
  269. $mapping['unique'] = $columnAnnot->unique;
  270. if ($columnAnnot->options) {
  271. $mapping['options'] = $columnAnnot->options;
  272. }
  273. if (isset($columnAnnot->name)) {
  274. $mapping['columnName'] = $columnAnnot->name;
  275. }
  276. if (isset($columnAnnot->columnDefinition)) {
  277. $mapping['columnDefinition'] = $columnAnnot->columnDefinition;
  278. }
  279. if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
  280. $mapping['id'] = true;
  281. }
  282. if ($generatedValueAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\GeneratedValue')) {
  283. $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy));
  284. }
  285. if ($versionAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Version')) {
  286. $metadata->setVersionMapping($mapping);
  287. }
  288. $metadata->mapField($mapping);
  289. // Check for SequenceGenerator/TableGenerator definition
  290. if ($seqGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\SequenceGenerator')) {
  291. $metadata->setSequenceGeneratorDefinition(array(
  292. 'sequenceName' => $seqGeneratorAnnot->sequenceName,
  293. 'allocationSize' => $seqGeneratorAnnot->allocationSize,
  294. 'initialValue' => $seqGeneratorAnnot->initialValue
  295. ));
  296. } else if ($tblGeneratorAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\TableGenerator')) {
  297. throw MappingException::tableIdGeneratorNotImplemented($className);
  298. }
  299. } else if ($oneToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToOne')) {
  300. if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
  301. $mapping['id'] = true;
  302. }
  303. $mapping['targetEntity'] = $oneToOneAnnot->targetEntity;
  304. $mapping['joinColumns'] = $joinColumns;
  305. $mapping['mappedBy'] = $oneToOneAnnot->mappedBy;
  306. $mapping['inversedBy'] = $oneToOneAnnot->inversedBy;
  307. $mapping['cascade'] = $oneToOneAnnot->cascade;
  308. $mapping['orphanRemoval'] = $oneToOneAnnot->orphanRemoval;
  309. $mapping['fetch'] = $this->getFetchMode($className, $oneToOneAnnot->fetch);
  310. $metadata->mapOneToOne($mapping);
  311. } else if ($oneToManyAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OneToMany')) {
  312. $mapping['mappedBy'] = $oneToManyAnnot->mappedBy;
  313. $mapping['targetEntity'] = $oneToManyAnnot->targetEntity;
  314. $mapping['cascade'] = $oneToManyAnnot->cascade;
  315. $mapping['indexBy'] = $oneToManyAnnot->indexBy;
  316. $mapping['orphanRemoval'] = $oneToManyAnnot->orphanRemoval;
  317. $mapping['fetch'] = $this->getFetchMode($className, $oneToManyAnnot->fetch);
  318. if ($orderByAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OrderBy')) {
  319. $mapping['orderBy'] = $orderByAnnot->value;
  320. }
  321. $metadata->mapOneToMany($mapping);
  322. } else if ($manyToOneAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToOne')) {
  323. if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
  324. $mapping['id'] = true;
  325. }
  326. $mapping['joinColumns'] = $joinColumns;
  327. $mapping['cascade'] = $manyToOneAnnot->cascade;
  328. $mapping['inversedBy'] = $manyToOneAnnot->inversedBy;
  329. $mapping['targetEntity'] = $manyToOneAnnot->targetEntity;
  330. $mapping['fetch'] = $this->getFetchMode($className, $manyToOneAnnot->fetch);
  331. $metadata->mapManyToOne($mapping);
  332. } else if ($manyToManyAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\ManyToMany')) {
  333. $joinTable = array();
  334. if ($joinTableAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\JoinTable')) {
  335. $joinTable = array(
  336. 'name' => $joinTableAnnot->name,
  337. 'schema' => $joinTableAnnot->schema
  338. );
  339. foreach ($joinTableAnnot->joinColumns as $joinColumn) {
  340. $joinTable['joinColumns'][] = array(
  341. 'name' => $joinColumn->name,
  342. 'referencedColumnName' => $joinColumn->referencedColumnName,
  343. 'unique' => $joinColumn->unique,
  344. 'nullable' => $joinColumn->nullable,
  345. 'onDelete' => $joinColumn->onDelete,
  346. 'columnDefinition' => $joinColumn->columnDefinition,
  347. );
  348. }
  349. foreach ($joinTableAnnot->inverseJoinColumns as $joinColumn) {
  350. $joinTable['inverseJoinColumns'][] = array(
  351. 'name' => $joinColumn->name,
  352. 'referencedColumnName' => $joinColumn->referencedColumnName,
  353. 'unique' => $joinColumn->unique,
  354. 'nullable' => $joinColumn->nullable,
  355. 'onDelete' => $joinColumn->onDelete,
  356. 'columnDefinition' => $joinColumn->columnDefinition,
  357. );
  358. }
  359. }
  360. $mapping['joinTable'] = $joinTable;
  361. $mapping['targetEntity'] = $manyToManyAnnot->targetEntity;
  362. $mapping['mappedBy'] = $manyToManyAnnot->mappedBy;
  363. $mapping['inversedBy'] = $manyToManyAnnot->inversedBy;
  364. $mapping['cascade'] = $manyToManyAnnot->cascade;
  365. $mapping['indexBy'] = $manyToManyAnnot->indexBy;
  366. $mapping['orphanRemoval'] = $manyToManyAnnot->orphanRemoval;
  367. $mapping['fetch'] = $this->getFetchMode($className, $manyToManyAnnot->fetch);
  368. if ($orderByAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\OrderBy')) {
  369. $mapping['orderBy'] = $orderByAnnot->value;
  370. }
  371. $metadata->mapManyToMany($mapping);
  372. }
  373. }
  374. // Evaluate @HasLifecycleCallbacks annotation
  375. if (isset($classAnnotations['Doctrine\ORM\Mapping\HasLifecycleCallbacks'])) {
  376. foreach ($class->getMethods() as $method) {
  377. // filter for the declaring class only, callbacks from parents will already be registered.
  378. if ($method->isPublic() && $method->getDeclaringClass()->getName() == $class->name) {
  379. $annotations = $this->_reader->getMethodAnnotations($method);
  380. if ($annotations && is_numeric(key($annotations))) {
  381. foreach ($annotations as $annot) {
  382. $annotations[get_class($annot)] = $annot;
  383. }
  384. }
  385. if (isset($annotations['Doctrine\ORM\Mapping\PrePersist'])) {
  386. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::prePersist);
  387. }
  388. if (isset($annotations['Doctrine\ORM\Mapping\PostPersist'])) {
  389. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postPersist);
  390. }
  391. if (isset($annotations['Doctrine\ORM\Mapping\PreUpdate'])) {
  392. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preUpdate);
  393. }
  394. if (isset($annotations['Doctrine\ORM\Mapping\PostUpdate'])) {
  395. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postUpdate);
  396. }
  397. if (isset($annotations['Doctrine\ORM\Mapping\PreRemove'])) {
  398. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preRemove);
  399. }
  400. if (isset($annotations['Doctrine\ORM\Mapping\PostRemove'])) {
  401. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postRemove);
  402. }
  403. if (isset($annotations['Doctrine\ORM\Mapping\PostLoad'])) {
  404. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::postLoad);
  405. }
  406. if (isset($annotations['Doctrine\ORM\Mapping\PreFlush'])) {
  407. $metadata->addLifecycleCallback($method->getName(), \Doctrine\ORM\Events::preFlush);
  408. }
  409. }
  410. }
  411. }
  412. }
  413. /**
  414. * Whether the class with the specified name is transient. Only non-transient
  415. * classes, that is entities and mapped superclasses, should have their metadata loaded.
  416. * A class is non-transient if it is annotated with either @Entity or
  417. * @MappedSuperclass in the class doc block.
  418. *
  419. * @param string $className
  420. * @return boolean
  421. */
  422. public function isTransient($className)
  423. {
  424. $classAnnotations = $this->_reader->getClassAnnotations(new \ReflectionClass($className));
  425. if ($classAnnotations && is_numeric(key($classAnnotations))) {
  426. foreach ($classAnnotations as $annot) {
  427. if ($annot instanceof \Doctrine\ORM\Mapping\Entity) {
  428. return false;
  429. }
  430. if ($annot instanceof \Doctrine\ORM\Mapping\MappedSuperclass) {
  431. return false;
  432. }
  433. }
  434. return true;
  435. }
  436. return ! isset($classAnnotations['Doctrine\ORM\Mapping\Entity']) &&
  437. ! isset($classAnnotations['Doctrine\ORM\Mapping\MappedSuperclass']);
  438. }
  439. /**
  440. * {@inheritDoc}
  441. */
  442. public function getAllClassNames()
  443. {
  444. if ($this->_classNames !== null) {
  445. return $this->_classNames;
  446. }
  447. if (!$this->_paths) {
  448. throw MappingException::pathRequired();
  449. }
  450. $classes = array();
  451. $includedFiles = array();
  452. foreach ($this->_paths as $path) {
  453. if ( ! is_dir($path)) {
  454. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  455. }
  456. $iterator = new \RegexIterator(
  457. new \RecursiveIteratorIterator(
  458. new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
  459. \RecursiveIteratorIterator::LEAVES_ONLY
  460. ),
  461. '/^.+' . str_replace('.', '\.', $this->_fileExtension) . '$/i',
  462. \RecursiveRegexIterator::GET_MATCH
  463. );
  464. foreach ($iterator as $file) {
  465. $sourceFile = realpath($file[0]);
  466. require_once $sourceFile;
  467. $includedFiles[] = $sourceFile;
  468. }
  469. }
  470. $declared = get_declared_classes();
  471. foreach ($declared as $className) {
  472. $rc = new \ReflectionClass($className);
  473. $sourceFile = $rc->getFileName();
  474. if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
  475. $classes[] = $className;
  476. }
  477. }
  478. $this->_classNames = $classes;
  479. return $classes;
  480. }
  481. /**
  482. * Attempts to resolve the fetch mode.
  483. *
  484. * @param string $className The class name
  485. * @param string $fetchMode The fetch mode
  486. * @return integer The fetch mode as defined in ClassMetadata
  487. * @throws MappingException If the fetch mode is not valid
  488. */
  489. private function getFetchMode($className, $fetchMode)
  490. {
  491. if(!defined('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode)) {
  492. throw MappingException::invalidFetchMode($className, $fetchMode);
  493. }
  494. return constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $fetchMode);
  495. }
  496. /**
  497. * Factory method for the Annotation Driver
  498. *
  499. * @param array|string $paths
  500. * @param AnnotationReader $reader
  501. * @return AnnotationDriver
  502. */
  503. static public function create($paths = array(), AnnotationReader $reader = null)
  504. {
  505. if ($reader == null) {
  506. $reader = new AnnotationReader();
  507. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  508. }
  509. return new self($reader, $paths);
  510. }
  511. }