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 full-size slide

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

    View full-size slide

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

    Instead, it offers three object frameworks.

    View full-size slide

  4. What is a framework ?

    View full-size slide

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

    View full-size slide

  6. Dependency Injection

    View full-size slide

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

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

    View full-size slide

  8. ݪଇ
    ࣮૷Ͱ͸ͳ͘ɺ

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

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  11. Decorator Pattern
    UPS

    View full-size slide

  12. Composite Pattern

    View full-size slide

  13. Null Object Pattern

    View full-size slide

  14. Adapter Pattern

    View full-size slide

  15. 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 full-size slide

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

    ࠶ར༻ΛՄೳʹ͢ΔϑϨʔϜϫʔΫͷ։ൃʹ͸ͦͷݪଇద༻͕ඞਢ”
    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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  20. Billing

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

    View full-size slide

  21. Anti Pattern

    Dependency Lookup

    View full-size slide

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

    View full-size slide

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

    View full-size slide


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

    View full-size slide

  25. 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 full-size slide

  26. Application = One Root Object

    View full-size slide

  27. Application class

    View full-size slide

  28. Application Script

    View full-size slide

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

    View full-size slide

  30. print_o($obj);

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. Dependency Injection Compiler
    Compile

    View full-size slide

  34. Instance script

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  37. Aspect Oriented Programming

    View full-size slide

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

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

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

    View full-size slide

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

    View full-size slide

  40. ॲཧ
    BEGIN
    COMMIT

    View full-size slide

  41. ॲཧ
    try
    catch

    View full-size slide

  42. ॲཧ
    cache check
    save cache

    View full-size slide

  43. ॲཧ
    cache check
    save cache
    ॲཧ
    ॲཧ

    View full-size slide

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

    View full-size slide

  45. core concern
    cross cutting concern

    View full-size slide

  46. Domain Logic
    Application Logic

    View full-size slide

  47. Aspect Pattern
    before
    after
    around

    View full-size slide

  48. Rock Concert Example

    View full-size slide

  49. Gregor Kiczales

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  54. MethodInterceptor

    View full-size slide

  55. Original method or next interceptor
    Cache Hit

    View full-size slide

  56. Bind The Interceptor with Matcher

    View full-size slide

  57. API Log
    Client Valid Auth

    View full-size slide

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

    View full-size slide

  59. MVC, Is 3 enough ?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  64. Representational State Transfer

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  67. Resource-Method-Representation

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  70. Inject Resource Parameter

    View full-size slide

  71. Method base cache update

    View full-size slide

  72. 304 Not Modified

    View full-size slide

  73. Content-Negotiation

    View full-size slide

  74. OPTION method

    View full-size slide

  75. JsonSchema Validation

    View full-size slide

  76. Payment
    hyperlink
    Hypermedia API
    Order

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  80. Performance
    • annotation ? dependency injection ? 

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  85. Contributors

    View full-size slide

  86. @koriym
    @BEARSunday

    View full-size slide