Doctrine Common 注解库定义了 2 个注解阅读器,它们都实现了Reader接口:
Doctrine\Common\Annotations\AnnotationReaderDoctrine\Common\Annotations\SimpleAnnotationReader
有人知道它们之间的区别吗?
我唯一的提示是文档块SimpleAnnotationReader:
此注释阅读器旨在用于您可以完全控制所有可用注释的项目。
Doctrine Common 注解库定义了 2 个注解阅读器,它们都实现了Reader接口:
Doctrine\Common\Annotations\AnnotationReaderDoctrine\Common\Annotations\SimpleAnnotationReader有人知道它们之间的区别吗?
我唯一的提示是文档块SimpleAnnotationReader:
此注释阅读器旨在用于您可以完全控制所有可用注释的项目。
你可以给它一个排除的注释列表,比如@param和@return忽略。这可能是一个问题,因为很难排除您不想要的任何可能的注释。
代码示例:
AnnotationRegistry::registerAutoloadNamespace('My\Annotation', 'dir/');
$this->annotationReader = new SimpleAnnotationReader();
$this->annotationReader->addNamespace('My\Annotation');
如果文件中有任何未知的注释,我不会得到错误。
但是有一个问题:如果用户在注释名称中输入错误,那么读者会默默地忽略它:(
资料来源:在开发 PHP-DI 时,我对两者都进行了测试。这SimpleAnnotationReader对我来说更好,因为我希望它只阅读我的@Inject注释。使用 时AnnotationReader,如果文件中有任何未知注释(例如@expectedException来自 PHPUnit),我会收到错误消息。
另请参阅此问题。
看起来两者之间最重要的区别在Doctrine 代码中进行了解释:可以SimpleAnnotationDriver处理未导入的注释(给定默认命名空间),同时AnnotationReader需要正确导入的注释。
显然,简单的注释阅读器将尝试阅读他将在 doc 块中看到的所有注释,而 AnnotationReader 可以处理要忽略的注释列表(如@param 或@return)。