symfony的annotations功能是在哪兒實現的?

來源:互聯網
上載者:User
symfony的annotations功能(路由、資料驗證、ORM等地方用到的)是在哪兒實現的?
我問的是代碼在哪兒,不是如何?的。實現方式我估計是分析 tokens以及php的反射api。是用的doctrine的annotations組件實現的嗎?

回複內容:

symfony的annotations功能(路由、資料驗證、ORM等地方用到的)是在哪兒實現的?
我問的是代碼在哪兒,不是如何?的。實現方式我估計是分析tokens以及php的反射api。是用的doctrine的annotations組件實現的嗎?

annotation直接實現在(或者說常用annotation定義在)這個bundle裡:
https://symfony.com/doc/master/bundles/SensioFrameworkExtraBundle/index.html
不過這個bundle有Doctrine\Common依賴:
https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/composer.json
所以八九不離十,底層是Doctrine的系統,沒有理由用了Doctrine還自己去實現一遍的。
另一個依賴是Framework Core,本身是不帶Annotation支援的。

一、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"

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,這裡有一篇國外的文章專門講這個(雖然是幾年前的,但是內容沒有過時)

Symfony2 & Doctrine Common: creating powerful annotations

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.