Upgrade to Pro — share decks privately, control downloads, hide ads and more …

BEAR.Sunday (2018)

BEAR.Sunday (2018)

BEAR.Sunday is a resource orientated framework with a REST centered architecture, implementing Dependency Injection and Aspect Orientated Programming' at its core. #phperKaigi

Akihito Koriyama

March 10, 2018
Tweet

More Decks by Akihito Koriyama

Other Decks in Technology

Transcript

  1. A resource orientated framework
    using the DI /AOP/REST Triangle

    View Slide

  2. View Slide

  3. BEAR.Sunday is an application framework.
    But it offers no libraries.
    (We have good stuff)

    View Slide

  4. ͦͷ୅ΘΓʹ̏ͭͷΦϒδΣΫτϑϨʔϜϫʔΫ
    DI
    AOP
    REST

    Instead, it offers three object frameworks.

    View Slide

  5. What is a framework ?

    View Slide

  6. ΤϯυϢʔβίʔυʹର͢ΔҰ࿈ͷઃܭ੍໿
    ”imposes a set of design constraints on end-user code.”

    View Slide

  7. Dependency Injection

    View Slide

  8. ఆٛ
    DI͸ૄ݁߹ͷίʔσΟϯάΛՄೳʹ͢Διϑ
    τ΢ΤΞσβΠϯݪଇͱύλʔϯͷηοτ

    “Dependency Injection is a set of software design principles and patterns that
    enable you to develop loosely coupled code.”

    View Slide

  9. ݪଇ
    ࣮૷Ͱ͸ͳ͘ɺ

    ΠϯλʔϑΣΠεʹରͯ͠ϓϩάϥϜ͢Δ

    Kent Beckand, Ward Cunningham 1987
    Gang of Four 1994
    “Program to an interface, not an implementation.”

    View Slide

  10. View Slide

  11. GET SHIT DONE !
    .. OK ?
    maintenance ?
    upgrade ?

    View Slide

  12. View Slide

  13. Ϧείϑͷஔ׵ݪଇ
    Liskov substitution principle

    View Slide

  14. Decorator Pattern
    UPS

    View Slide

  15. Composite Pattern

    View Slide

  16. Null Object Pattern

    View Slide

  17. Adapter Pattern

    View Slide

  18. View Slide

  19. DIP: ґଘؔ܎ٯసͷݪଇ
    • ίʔυ͸ಉ౳Ҏ্ͷϨϕϧͷந৅ʹґଘ͢Δ
    • ԼҐϨϕϧͷৄࡉʹґଘ͠ͳ͍
    • Code should depend on things that are at the same or
    higher level of abstraction
    • High level policy should not depend on low level details
    Dependency inversion principle

    View Slide

  20. “ґଘੑٯసݪଇ͸ΦϒδΣΫτࢦ޲ٕज़ͷརӹͷࠜװΛͳ͢΋ͷɻ

    ࠶ར༻ΛՄೳʹ͢ΔϑϨʔϜϫʔΫͷ։ൃʹ͸ͦͷݪଇద༻͕ඞਢ”
    Robert Martin (a.k.a. Uncle Bob)
    “The principle of dependency inversion is at the
    root of many of the benefits claimed for object-
    oriented technology.
    Its proper application is necessary for the creation
    of reusable frameworks”

    View Slide

  21. View Slide

  22. View Slide

  23. ந৅ʹґଘ্ͨ͠Ґίʔυ
    ᶃΠϯλʔϑΣΠεͰड͚औΓ
    ᶄϓϩύςΟʹ୅ೖ
    ᶅಡΈऔΓઐ༻
    Higher-level code depends on abstraction
    passed through interface
    set to the property
    read-only

    View Slide

  24. ந৅ʹ۩৅Λଋറ
    bind abstraction to concretion

    View Slide

  25. ΦϒδΣΫτάϥϑੜ੒
    building object graph

    View Slide

  26. Billing

    Service
    Processor
    depends
    API
    Client
    Db
    Logger
    String lib
    Cal
    ?
    ?
    ΦϒδΣΫτάϥϑ
    object graph

    View Slide

  27. Anti Pattern

    Dependency Lookup

    View Slide

  28. Anti Pattern
    “MODE”
    ͍ͭ͘΋ͷύε
    खଓ͖Λίʔυ
    code “procedures” multiple execution path

    View Slide

  29. ੜ੒ͱར༻͕෼཭ͨ͠DIύλʔϯ
    Compile
    Runtime
    ߏ଄Λίʔυ/ γϯάϧύε
    DI pattern
    code “structure” / single execution path

    View Slide


  30. Ͳ͜ͰΦϒδΣΫτάϥϑΛੜ੒͢Δ͔ʁ
    Where should we compose object graphs?

    View Slide

  31. DI good practice

    ίʔυ͸ՄೳͳݶΓΠϯδΣΫλʔΛ௚઀ѻΘͳ͍ɻ

    ͦͷ୅ΘΓɺΞϓϦέʔγϣϯͷΤϯτϦʔϙΠϯτͰ̍ͭͷ
    ϧʔτΦϒδΣΫτΛੜ੒ͯ͠ΞϓϦέʔγϣϯΛىಈ͢Δ
    “Your code should deal directly with the Injector as little as possible. 

    Instead, you want to bootstrap your application by injecting one root object.”

    View Slide

  32. Application = One Root Object

    View Slide

  33. Application class

    View Slide

  34. Application Script

    View Slide

  35. Root Object

    View Slide

  36. ͦΕͧΕͷґଘ͸อ࣋ͯ͠Δ͔อ࣋͞Ε͍ͯΔ͔
    Dependencies have other dependencies
    Each object either contains or belongs to.

    View Slide

  37. Live demo

    View Slide

  38. View Slide

  39. print_o($obj);

    View Slide

  40. ڊେɻ͔̍ͭ͠͠ͷม਺ͱͯ͠อଘՄೳɻ
    Huge, but can be stored one single value $app
    ϦΫΤετΛ௒͑ͯDI͕࠶ར༻Ͱ͖Δ
    Injection is reused beyond requests.

    View Slide

  41. ϥϯλΠϜίϯύΠϧ΋ඞཁ
    • Ͳͷϖʔδ͔·ͩ෼͔Βͳ͍
    • PDOΦϒδΣΫτ΍Ϋϩʔδϟ͸γϦΞϥΠζͰ
    ͖ͳ͍
    Still, We need runtime compile
    No sure which page will be load
    Not everything can be serialized

    View Slide

  42. Dependency Injection Compiler
    Compile

    View Slide

  43. Instance script

    View Slide

  44. • ֦ுੑ
    • ฒߦ։ൃ
    • ϝϯςφϯε
    • ςελϏϦςΟ
    • ΦϒδΣΫτͱར༻ͷੜ੒ͷ෼཭
    • ΦϒδΣΫτσϦόϦʔ
    DIͷར఺
    Extensibility
    Maintainability
    Parallel development
    Testability
    Clear separation of object instantiation
    and object usage
    Object delivery
    DI benefit

    View Slide

  45. 1st framework: DI Framework
    • DSLͱΞϊςʔγϣϯΛ࢖ͬͨDIϑϨʔϜϫʔΫ
    • ૄ݁߹
    • ίϯύΠϧͱϥϯλΠϜͷ۠ผ
    • ΞϓϦέʔγϣϯ͸୯ҰͷΦϒδΣΫτ
    • CodegenͰߴ଎
    loosely coupled
    compile time and runtime
    application root object
    DI framework with DSL w/ annotation
    fast

    View Slide

  46. “Ray”

    View Slide

  47. Aspect Oriented Programming

    View Slide

  48. What is AOP?
    Cache
    Log
    Auth
    ԣஅతؔ৺ࣄΛ෼཭Մೳʹ͢Δ͜ͱʹΑΓ

    ϞδϡϥϦςΟʔΛ૿Ճͤ͞Δ͜ͱΛ໨తͱͨ͠

    ϓϩάϥϛϯάύϥμΠϜ
    A programming paradigm that aims to increase modularity
    by allowing the separation of cross-cutting concerns

    View Slide

  49. ॲཧ
    $a = microtime(true)
    microtime(true) - $a
    logic

    View Slide

  50. ॲཧ
    BEGIN
    COMMIT

    View Slide

  51. ॲཧ
    try
    catch

    View Slide

  52. ॲཧ
    cache check
    save cache

    View Slide

  53. ॲཧ
    cache check
    save cache
    ॲཧ
    ॲཧ

    View Slide

  54. ຊ࣭తؔ৺
    ԣஅతؔ৺

    View Slide

  55. core concern
    cross cutting concern

    View Slide

  56. Domain Logic
    Application Logic

    View Slide

  57. Aspect Pattern
    before
    after
    around

    View Slide

  58. Rock Concert Example

    View Slide

  59. Gregor Kiczales

    View Slide

  60. View Slide

  61. View Slide

  62. View Slide

  63. ϝιου࣮ߦ
    Caller Method
    method invocation

    View Slide

  64. Πϯλʔηϓτ
    Caller Method
    Interceptor
    intercepting method

    View Slide

  65. ෳ਺ͷΠϯλʔηϓτ
    C I I I M
    multiple interception

    View Slide

  66. Hit
    Caller Method
    Cache
    Caller Method
    Cache
    Miss
    Ωϟογϡ͕͋Ε͹ϝιου͸ίʔϧ͞Εͳ͍
    If the cache is warm the method is never called.

    View Slide

  67. MethodInterceptor

    View Slide

  68. Original method or next interceptor
    Cache Hit

    View Slide

  69. Bind The Interceptor with Matcher

    View Slide

  70. API
    Client

    View Slide

  71. API Log
    Client Valid Auth

    View Slide

  72. API Log
    !7BMJE
    BENJO %&-&5&
    Client Valid Auth

    View Slide

  73. MVC, Is 3 enough ?

    View Slide

  74. ՄมϨΠϠϦϯά
    Method
    Cache
    Form
    Auth
    Validation
    laying by context

    View Slide

  75. PHPͰͷAOP࣮૷ํ๏
    •Proxy (1.x)
    •Codegen (2.x)
    How to implement AOP in PHP

    View Slide

  76. ܧঝΫϥεΛಈతʹੜ੒ͨ͠ܕ҆શͳΠϯλʔηϓτ
    Under the hood: Method interception sub class is created n order enable this interception and keep type safety.

    View Slide

  77. 2nd framework: Aspect Oriented Framework
    • AOPΞϥΠΞϯε
    • ίϯςΩετʹΑΔϨΠϠϦϯά
    • ܕ҆શ
    • CodegenͰߴ଎
    AOP Alliance
    Laying by context
    Type safety
    Fast

    View Slide

  78. Representational State Transfer

    View Slide

  79. ϩΠɾϑΟʔϧσΟϯά

    View Slide

  80. View Slide

  81. View Slide

  82. View Slide

  83. )551

    View Slide

  84. $POTPMF

    View Slide

  85. #&"34VOEBZ

    View Slide

  86. 1)1

    View Slide

  87. 1)1
    $POTPMF
    )551
    #&"3

    View Slide

  88. Resource-Method-Representation

    View Slide

  89. View Slide

  90. View Slide

  91. View Slide

  92. @Embed

    View Slide

  93. HAL: Hypertext Application Language
    content-type: application/hal+json

    View Slide

  94. Resource Graph
    User
    Profile
    @Embed
    Job
    Best
    Friend
    Office
    School

    View Slide

  95. Inject Resource Parameter

    View Slide

  96. Method base cache update

    View Slide

  97. 304 Not Modified

    View Slide

  98. Content-Negotiation

    View Slide

  99. OPTION method

    View Slide

  100. JsonSchema Validation

    View Slide

  101. View Slide

  102. Hypermedia

    View Slide

  103. Payment
    hyperlink
    Hypermedia API
    Order

    View Slide

  104. The key of success of web
    •URI
    •Unified Interface
    •Hyperlink

    View Slide

  105. API
    APIۦಈ։ൃ
    DB Mobil
    e
    Web
    API
    Cloud
    Moc
    k
    URI
    API
    • API is hub
    • API is core value
    API driven development

    View Slide

  106. View Slide

  107. 3rd framework: REST Framework
    • RESTfulͳϦιʔείϯςφ
    • HTTPඪ४
    • ࠶ར༻ੑ
    • ڧྗͳΩϟογϡ
    RESTful container
    HTTP standard
    Reusability
    Cache-friendly

    View Slide

  108. Performance
    • annotation ? dependency injection ? 

    method interception ? DSL ? named parameter ?
    • ߴ଎
    • ΦϒδΣΫτάϥϑΩϟογϡ
    • Codegen
    Fast
    Object graph cache

    View Slide

  109. Abstraction frameworks
    • DSL
    • Annotation
    • URI
    • Interface
    • Aspects
    • Hypermedia

    View Slide

  110. Connecting frameworks
    • DI - ΦϒδΣΫτΛ݁Ϳ
    • AOP - Domain LogicͱApplication LogicΛ݁Ϳ
    • REST -ϦιʔεΛ݁Ϳ
    Connecting objects
    Connecting domain logic and application logic
    Connecting resources

    View Slide

  111. View Slide

  112. View Slide

  113. AOP (Gregor Kiczales) DI (Martin Fowler) REST (Roy Fielding)
    OOP (Allan Kay) Annotation (Anders Hejlsberg) Guice (Bob Lee)

    View Slide

  114. Why BEAR.Sunday ?
    • RESTfullΫϦʔϯΞʔΩςΫνϟ
    • APIۦಈ։ൃ
    • ࠶ར༻ੑ
    • ύϑΥʔϚϯε
    • ޙํޓ׵
    RESTful clean architecture
    API driven development
    Reusability
    Performance
    NO BC break

    View Slide

  115. Contributors

    View Slide

  116. View Slide

  117. View Slide

  118. View Slide

  119. @koriym
    @BEARSunday

    View Slide