Skip to content

Commit

Permalink
Deprecate using more than one document annotations on classes (doctri…
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jan 14, 2019
1 parent 7002c26 commit b01cd0a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions UPGRADE-1.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ in favor of `Doctrine\ODM\MongoDB\Mapping\ClassMetadata` and will be dropped in
* The `@NotSaved` annotation was deprecated and will be dropped in 2.0. Use the
`notSaved` option on the `@Field`, `@ReferenceOne`, `@ReferenceMany`,
`@EmbedOne` or `@EmbedMany` annotations instead.
* Using more than one class-level document annotation (e.g. `@Document`,
`@MappedSuperclass`) is deprecated and will throw an exception in 2.0.
Classes should only be annotated with a single document annotation.

### XML mappings

Expand Down
31 changes: 29 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MappingClassMetadata;
use Doctrine\ODM\MongoDB\Mapping\MappingException;
use ReflectionClass;
use const E_USER_DEPRECATED;
use function sprintf;
use function trigger_error;

/**
* The AnnotationDriver reads the mapping metadata from docblock annotations.
Expand Down Expand Up @@ -59,6 +63,18 @@ public static function registerAnnotationClasses()
AnnotationRegistry::registerFile(__DIR__ . '/../Annotations/DoctrineAnnotations.php');
}

public function isTransient($className)
{
$classAnnotations = $this->reader->getClassAnnotations(new ReflectionClass($className));

foreach ($classAnnotations as $annot) {
if ($annot instanceof ODM\AbstractDocument) {
return false;
}
}
return true;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -121,8 +137,19 @@ public function loadMetadataForClass($className, ClassMetadata $class)

}

if ( ! $documentAnnots) {
throw MappingException::classIsNotAValidDocument($className);
switch (count($documentAnnots)) {
case 0:
throw MappingException::classIsNotAValidDocument($className);

case 1:
// Everything is good
break;

default:
@trigger_error(
sprintf('Class "%s" uses more than one document annotation. This is deprecated and will cause an exception in 2.0.', $className),
E_USER_DEPRECATED
);
}

// find the winning document annotation
Expand Down

0 comments on commit b01cd0a

Please sign in to comment.