Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between AnnotationReader & SimpleAnnotationReader?

Tags:

doctrine-orm

The Doctrine Common annotations library defines 2 annotation readers that both implement the Reader interface:

  • Doctrine\Common\Annotations\AnnotationReader
  • Doctrine\Common\Annotations\SimpleAnnotationReader

Anyone knows the difference between them?

The only hint I have is the docblock on SimpleAnnotationReader:

This annotation reader is intended to be used in projects where you have full-control over all annotations that are available.

like image 882
BenMorel Avatar asked Dec 26 '22 04:12

BenMorel


1 Answers

  • The AnnotationReader will try to read ALL annotations.

You can give it a list of excluded annotations like @param and @return to ignore. It can be a problem since it's hard to exclude any possible annotation that you don't want.

  • The SimpleAnnotationReader can load only the annotations you register to it.

Code example:

AnnotationRegistry::registerAutoloadNamespace('My\Annotation', 'dir/');
$this->annotationReader = new SimpleAnnotationReader();
$this->annotationReader->addNamespace('My\Annotation');

I will not get errors if there is any unknown annotation in the file.

There's one catch though: if the user makes a typo in the annotation name, then the reader will silently ignore it :(


Source: while developing PHP-DI, I tested both. The SimpleAnnotationReader is better for me because I want it to only read my @Inject annotation. When using AnnotationReader, I got errors if there was any unknown annotation in the file (for example @expectedException from PHPUnit).

See also this issue.

like image 171
Matthieu Napoli Avatar answered Jan 04 '23 17:01

Matthieu Napoli