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

More Decks by Akihito Koriyama

Other Decks in Technology


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

  2. None
  3. BEAR.Sunday is an application framework. But it offers no libraries.

    (We have good stuff)
  4. ͦͷ୅ΘΓʹ̏ͭͷΦϒδΣΫτϑϨʔϜϫʔΫ DI AOP REST
 Instead, it offers three object frameworks.

  5. What is a framework ?

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

  7. Dependency Injection

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

    design principles and patterns that enable you to develop loosely coupled code.”
  9. ݪଇ ࣮૷Ͱ͸ͳ͘ɺ
 Kent Beckand, Ward Cunningham 1987 Gang of

    Four 1994 “Program to an interface, not an implementation.”
  10. None
  11. GET SHIT DONE ! .. OK ? maintenance ? upgrade

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

  14. Decorator Pattern UPS

  15. Composite Pattern

  16. Null Object Pattern

  17. Adapter Pattern

  18. None
  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
  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”
  21. None
  22. None
  23. ந৅ʹґଘ্ͨ͠Ґίʔυ ᶃΠϯλʔϑΣΠεͰड͚औΓ ᶄϓϩύςΟʹ୅ೖ ᶅಡΈऔΓઐ༻ Higher-level code depends on abstraction passed

    through interface set to the property read-only
  24. ந৅ʹ۩৅Λଋറ bind abstraction to concretion

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

  26. Billing
 Service Processor depends API Client Db Logger String lib

    Cal ? ? ΦϒδΣΫτάϥϑ object graph
  27. Anti Pattern
 Dependency Lookup

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

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

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

  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.”
  32. Application = One Root Object

  33. Application class

  34. Application Script

  35. Root Object

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

    belongs to.
  37. Live demo

  38. None
  39. print_o($obj);

  40. ڊେɻ͔̍ͭ͠͠ͷม਺ͱͯ͠อଘՄೳɻ Huge, but can be stored one single value $app

    ϦΫΤετΛ௒͑ͯDI͕࠶ར༻Ͱ͖Δ Injection is reused beyond requests.
  41. ϥϯλΠϜίϯύΠϧ΋ඞཁ • Ͳͷϖʔδ͔·ͩ෼͔Βͳ͍ • PDOΦϒδΣΫτ΍Ϋϩʔδϟ͸γϦΞϥΠζͰ ͖ͳ͍ Still, We need runtime

    compile No sure which page will be load Not everything can be serialized
  42. Dependency Injection Compiler Compile

  43. Instance script

  44. • ֦ுੑ • ฒߦ։ൃ • ϝϯςφϯε • ςελϏϦςΟ • ΦϒδΣΫτͱར༻ͷੜ੒ͷ෼཭

    • ΦϒδΣΫτσϦόϦʔ DIͷར఺ Extensibility Maintainability Parallel development Testability Clear separation of object instantiation and object usage Object delivery DI benefit
  45. 1st framework: DI Framework • DSLͱΞϊςʔγϣϯΛ࢖ͬͨDIϑϨʔϜϫʔΫ • ૄ݁߹ • ίϯύΠϧͱϥϯλΠϜͷ۠ผ

    • ΞϓϦέʔγϣϯ͸୯ҰͷΦϒδΣΫτ • CodegenͰߴ଎ loosely coupled compile time and runtime application root object DI framework with DSL w/ annotation fast
  46. “Ray”

  47. Aspect Oriented Programming

  48. What is AOP? Cache Log Auth ԣஅతؔ৺ࣄΛ෼཭Մೳʹ͢Δ͜ͱʹΑΓ
 ϓϩάϥϛϯάύϥμΠϜ A

    programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns
  49. ॲཧ $a = microtime(true) microtime(true) - $a logic


  51. ॲཧ try catch

  52. ॲཧ cache check save cache

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

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

  55. core concern cross cutting concern

  56. Domain Logic Application Logic

  57. Aspect Pattern before after around

  58. Rock Concert Example

  59. Gregor Kiczales

  60. None
  61. None
  62. None
  63. ϝιου࣮ߦ Caller Method method invocation

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

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

  66. Hit Caller Method Cache Caller Method Cache Miss Ωϟογϡ͕͋Ε͹ϝιου͸ίʔϧ͞Εͳ͍ If

    the cache is warm the method is never called.
  67. MethodInterceptor

  68. Original method or next interceptor Cache Hit

  69. Bind The Interceptor with Matcher

  70. API Client

  71. API Log Client Valid Auth

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

  73. MVC, Is 3 enough ?

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

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

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

    n order enable this interception and keep type safety.
  77. 2nd framework: Aspect Oriented Framework • AOPΞϥΠΞϯε • ίϯςΩετʹΑΔϨΠϠϦϯά •

    ܕ҆શ • CodegenͰߴ଎ AOP Alliance Laying by context Type safety Fast
  78. Representational State Transfer

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

  80. None
  81. None
  82. None
  83. )551

  84. $POTPMF

  85. #&"34VOEBZ

  86. 1)1

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

  88. Resource-Method-Representation

  89. None
  90. None
  91. None
  92. @Embed

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

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

  95. Inject Resource Parameter

  96. Method base cache update

  97. 304 Not Modified

  98. Content-Negotiation

  99. OPTION method

  100. JsonSchema Validation

  101. None
  102. Hypermedia

  103. Payment hyperlink Hypermedia API Order

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

  105. API APIۦಈ։ൃ DB Mobil e Web API Cloud Moc k

    URI API • API is hub • API is core value API driven development
  106. None
  107. 3rd framework: REST Framework • RESTfulͳϦιʔείϯςφ • HTTPඪ४ • ࠶ར༻ੑ

    • ڧྗͳΩϟογϡ RESTful container HTTP standard Reusability Cache-friendly
  108. Performance • annotation ? dependency injection ? 
 method interception

    ? DSL ? named parameter ? • ߴ଎ • ΦϒδΣΫτάϥϑΩϟογϡ • Codegen Fast Object graph cache
  109. Abstraction frameworks • DSL • Annotation • URI • Interface

    • Aspects • Hypermedia
  110. Connecting frameworks • DI - ΦϒδΣΫτΛ݁Ϳ • AOP - Domain

    LogicͱApplication LogicΛ݁Ϳ • REST -ϦιʔεΛ݁Ϳ Connecting objects Connecting domain logic and application logic Connecting resources
  111. None
  112. None
  113. AOP (Gregor Kiczales) DI (Martin Fowler) REST (Roy Fielding) OOP

    (Allan Kay) Annotation (Anders Hejlsberg) Guice (Bob Lee)
  114. Why BEAR.Sunday ? • RESTfullΫϦʔϯΞʔΩςΫνϟ • APIۦಈ։ൃ • ࠶ར༻ੑ •

    ύϑΥʔϚϯε • ޙํޓ׵ RESTful clean architecture API driven development Reusability Performance NO BC break
  115. Contributors

  116. None
  117. None
  118. None
  119. @koriym @BEARSunday