php实现annotation,symfony的annotations功能是在哪儿实现的?

一、Symfony的annotation的实现使用了该组件Doctrine\Common\Annotations

1.每一个annotation都有一个相对应的类

1)以Route[1]为例,如下,其是[2]的一个子类:

[1]Sensio\Bundle\FrameworkExtraBundle\Configration\Route

[2]Symfony\Component\Routing\Annotation\Route

2)其他,比如Method[3],Security[4]等均[5]的子类

[3]Sensio\Bundle\FrameworkExtraBundle\Configuration\Method

[4]Sensio\Bundle\FrameworkExtraBundle\Configuration\Security

[5]Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationAnnotation

2.通过运行如下图中的命令发现,Symfony框架与annotation相关的服务只有一个 - "annotation_reader"

931dafd4485554bc2afcf3da1a861409.png

Symfony通过这一服务,其实是"Doctrine\Common\Annotations\CachedReader",把每个annotation转换为annotation类(对象),然后再获取相应的各个参数,进行处理

比如,Route相对应的有一个类是"Symfony\Component\Routing\Loader\AnnotationClassLoader",这个类的load方法通过使用"annotation_reader"和PHP的反射API(ReflectionClass)对控制器对象和annotation类(Route)进行处理,最终返回RouteCollection对象

3.如上所述,实现方式的确用到了PHP的反射API(参考"Symfony\Component\Routing\Loader\AnnotationClassLoader")

4.通过使用"annotation_reader"这一服务和PHP的反射API(ReflectionClass),开发者甚至可以自己写自己的annotation类和ClassLoader,这里有一篇国外的文章专门讲这个(虽然是几年前的,但是内容没有过时)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值