AnnotationReader和SimpleAnnotationReader有什么区别?

Ben*_*min 3 doctrine-orm

学说的注解库定义了两个注释的读者,都实现了Reader接口:

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

有人知道他们之间的区别吗?

我唯一的提示是上的docblock SimpleAnnotationReader

该注释阅读器旨在用于您可以完全控制所有可用注释的项目中。

Mat*_*oli 5

  • AnnotationReader会试着去阅读所有注释。

您可以给它一个排除的注释列表,例如@param@return忽略。这可能是一个问题,因为很难排除不需要的任何注释。

  • SimpleAnnotationReader可以只加载您注册它的注解。

代码示例:

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

如果文件中有任何未知注释,我将不会收到错误消息。

但是有一个陷阱:如果用户在注释名称中输入了错字,那么读者将默默地忽略它:(


资料来源:在开发PHP-DI时,我对两者进行了测试。这SimpleAnnotationReader对我来说更好,因为我希望它仅读取我的@Inject注释。使用时AnnotationReader,如果文件中存在未知注释(例如@expectedException来自PHPUnit),则会出错。

另请参见此问题