MidwestPHP Symfony2 Internals

6,149 views

Published on

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
6,149
On SlideShare
0
From Embeds
0
Number of Embeds
84
Actions
Shares
0
Downloads
16
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

MidwestPHP Symfony2 Internals

  1. 1. Symfony2 Internals Raúl Fraile
  2. 2. Who am I?
  3. 3. Raúl Fraile • Software developer at • I live in Spain • PHP 5.3 Zend Certified Engineer • Symfony Certified Developer • Symfony2 lover ❤ • LadybugPHP: Simple and extensible PHP dumper • Future dad ☺ @raulfraile
  4. 4. Should I care aboutSymfony2 internals?
  5. 5. Yes!
  6. 6. http://xkcd.com/676/
  7. 7. Topics
  8. 8. 1. Symfony2 components2. Composer3. Autoloading & PSR-04. Request & Response5. Demo 16. Symfony2 full-stack framework7. Events8. Demo 2
  9. 9. Components
  10. 10. Set of decoupled and standalone libraries
  11. 11. Implement common features needed to develop websites
  12. 12. The foundation of theSymfony2 full-stack framework
  13. 13. BrowserKit Finder Security ClassLoader Form Serializer Config HttpFoundation Templating Console HttpKernel Translation CssSelector Locale ValidatorDependencyInjection Process Yaml DomCrawler OptionsResolver EventDispatcher Routing
  14. 14. BrowserKit Finder Security ClassLoader Form Serializer Config HttpFoundation Templating Console HttpKernel Translation CssSelector Locale ValidatorDependencyInjection Process Yaml DomCrawler OptionsResolver PropertyAccess 2.2.0 EventDispatcher Routing StopWatch
  15. 15. Who’s using them?
  16. 16. Components demo...
  17. 17. ... after some concepts
  18. 18. Composer
  19. 19. Composer is a dependency manager for PHP
  20. 20. { "require": { "symfony/http-foundation": "v2.2.0", "doctrine/orm": ">=2.2.3", }}
  21. 21. $ composer install
  22. 22. Autoload
  23. 23. Triggered whenever we need a class or interface not already defined
  24. 24. Fully Qualified Name (FQN) File
  25. 25. Fully Qualified Name (FQN)∖Doctrine∖DBAL∖Driver File
  26. 26. Fully Qualified Name (FQN) ∖Doctrine∖DBAL∖Driver[lib_path]/Doctrine/DBAL/Driver.php File
  27. 27. Fully Qualified Name (FQN) ∖Doctrine∖DBAL∖Driver ∖Twig_Lexer[lib_path]/Doctrine/DBAL/Driver.php File
  28. 28. Fully Qualified Name (FQN) ∖Doctrine∖DBAL∖Driver ∖Twig_Lexer[lib_path]/Doctrine/DBAL/Driver.php[lib_path]/Twig/Lexer.php File
  29. 29. bool spl_autoload_register( [callable $autoload_function, [bool $throw = true, [bool $prepend = false]]])
  30. 30. <?php include_once(__DIR__./MyLoader.php);include_once(__DIR__./vendor/Twitter/Loader.php); spl_autoload_register(array(MyLoader, autoload));spl_autoload_register(array(Loader, autoload));$tweet = new Tweet(); // vendor/Twitter/Tweet.php$book = new MyBook(); // classes/My/Book.php
  31. 31. <?php include_once(__DIR__./MyLoader.php);include_once(__DIR__./vendor/Twitter/Loader.php); spl_autoload_register(array(MyLoader, autoload));spl_autoload_register(array(Loader, autoload));$tweet = new Tweet(); // vendor/Twitter/Tweet.php$book = new MyBook(); // classes/My/Book.php new Tweet()
  32. 32. <?php include_once(__DIR__./MyLoader.php);include_once(__DIR__./vendor/Twitter/Loader.php); spl_autoload_register(array(MyLoader, autoload));spl_autoload_register(array(Loader, autoload));$tweet = new Tweet(); // vendor/Twitter/Tweet.php$book = new MyBook(); // classes/My/Book.php new Tweet() MyLoader::autoload(‘Tweet’)
  33. 33. <?php include_once(__DIR__./MyLoader.php);include_once(__DIR__./vendor/Twitter/Loader.php); spl_autoload_register(array(MyLoader, autoload));spl_autoload_register(array(Loader, autoload));$tweet = new Tweet(); // vendor/Twitter/Tweet.php$book = new MyBook(); // classes/My/Book.php new Tweet() MyLoader::autoload(‘Tweet’) Loader::autoload(‘Tweet’)
  34. 34. <?php include_once(__DIR__./MyLoader.php);include_once(__DIR__./vendor/Twitter/Loader.php); spl_autoload_register(array(MyLoader, autoload));spl_autoload_register(array(Loader, autoload));$tweet = new Tweet(); // vendor/Twitter/Tweet.php$book = new MyBook(); // classes/My/Book.php new Tweet() MyLoader::autoload(‘Tweet’) Loader::autoload(‘Tweet’) include __DIR__.’/vendor/Twitter/Tweet.php’
  35. 35. This is a mess...
  36. 36. ... we need a standard!
  37. 37. PSR-0
  38. 38. F.I.GFramework Interop Group
  39. 39. In summary:1) ‘/’ and ‘_’ are converted to DIRECTORY_SEPARATOR2) Suffixed with .php
  40. 40. Time for the demo!
  41. 41. Request
  42. 42. HttpFoundation ComponentObject Oriented abstraction of an HTTP request
  43. 43. http://www.ietf.org/rfc/rfc2616.txtRequest = Request-Line *(header CRLF) CRLF [ message-body ]
  44. 44. http://www.ietf.org/rfc/rfc2616.txt Request-Line
  45. 45.  =
  46. 46.  Method
  47. 47.  SP
  48. 48.  
  49. 49.  
  50. 50.  
  51. 51.  
  52. 52.  
  53. 53.  
  54. 54.  
  55. 55.  
  56. 56.  
  57. 57.  
  58. 58.  
  59. 59.  
  60. 60.  
  61. 61.  
  62. 62.  
  63. 63.  
  64. 64.  
  65. 65.  
  66. 66.  
  67. 67.  
  68. 68.  Request-URI
  69. 69.  SP
  70. 70.  
  71. 71.  
  72. 72.  
  73. 73.  
  74. 74.  
  75. 75.  
  76. 76.  
  77. 77.  
  78. 78.  
  79. 79.  
  80. 80.  
  81. 81.  
  82. 82.  
  83. 83.  
  84. 84.  
  85. 85.  
  86. 86.  
  87. 87.  
  88. 88.  
  89. 89.  
  90. 90.  HTTP-Version
  91. 91.  CRLFRequest = Request-Line *(header CRLF) CRLF [ message-body ]
  92. 92. http://www.ietf.org/rfc/rfc2616.txt Request-Line
  93. 93.  =
  94. 94.  Method
  95. 95.  SP
  96. 96.  
  97. 97.  
  98. 98.  
  99. 99.  
  100. 100.  
  101. 101.  
  102. 102.  
  103. 103.  
  104. 104.  
  105. 105.  
  106. 106.  
  107. 107.  
  108. 108.  
  109. 109.  
  110. 110.  
  111. 111.  
  112. 112.  
  113. 113.  
  114. 114.  
  115. 115.  
  116. 116.  Request-URI
  117. 117.  SP
  118. 118.  
  119. 119.  
  120. 120.  
  121. 121.  
  122. 122.  
  123. 123.  
  124. 124.  
  125. 125.  
  126. 126.  
  127. 127.  
  128. 128.  
  129. 129.  
  130. 130.  
  131. 131.  
  132. 132.  
  133. 133.  
  134. 134.  
  135. 135.  
  136. 136.  
  137. 137.  
  138. 138.  HTTP-Version
  139. 139.  CRLF Request = Request-Line *(header CRLF) CRLF [ message-body ]General
  140. 140.  headers:
  141. 141.  Cache-Control,
  142. 142.  Transfer-Encoding,
  143. 143.  Via...Request
  144. 144.  headers:
  145. 145.  Accept-Encoding,
  146. 146.  Accept-Language,
  147. 147.  If-Match...Entity
  148. 148.  headers:
  149. 149.  Content-Encoding,
  150. 150.  Content-Language,
  151. 151.  Expires...
  152. 152. Hypertext Transfer ProtocolGET /index.php HTTP/1.1Host: test.comAccept-Language:en;q=0.8Accept-Encoding:gzipUser-Agent: Mozilla/5.0
  153. 153. Hypertext Transfer Protocol $_GETGET /index.php HTTP/1.1 $_POSTHost: test.comAccept-Language:en;q=0.8 $_COOKIEAccept-Encoding:gzipUser-Agent: Mozilla/5.0 $_FILES $_SERVER
  154. 154. Hypertext Transfer Protocol $_GET query requestGET /index.php HTTP/1.1 $_POST cookiesHost: test.com filesAccept-Language:en;q=0.8 $_COOKIE serverAccept-Encoding:gzip headersUser-Agent: Mozilla/5.0 $_FILES getHost getClientIp $_SERVER ...
  155. 155. Response
  156. 156. HttpFoundation ComponentObject Oriented abstraction of an HTTP response
  157. 157. http://www.ietf.org/rfc/rfc2616.txtResponse = Status-Line *(header CRLF) CRLF [ message-body ]
  158. 158. http://www.ietf.org/rfc/rfc2616.txt Status-Line
  159. 159.  =
  160. 160.  HTTP-Version
  161. 161.  SP
  162. 162.  
  163. 163.  
  164. 164.  
  165. 165.  
  166. 166.  
  167. 167.  
  168. 168.  
  169. 169.  
  170. 170.  
  171. 171.  
  172. 172.  
  173. 173.  
  174. 174.  
  175. 175.  
  176. 176.  
  177. 177.  
  178. 178.  
  179. 179.  
  180. 180.  Status-Code
  181. 181.  SP
  182. 182.  
  183. 183.  
  184. 184.  
  185. 185.  
  186. 186.  
  187. 187.  
  188. 188.  
  189. 189.  
  190. 190.  
  191. 191.  
  192. 192.  
  193. 193.  
  194. 194.  
  195. 195.  
  196. 196.  
  197. 197.  
  198. 198.  
  199. 199.  
  200. 200.  Reason-Phrase
  201. 201.  CRLFResponse = Status-Line *(header CRLF) CRLF [ message-body ]
  202. 202. http://www.ietf.org/rfc/rfc2616.txt Status-Line
  203. 203.  =
  204. 204.  HTTP-Version
  205. 205.  SP
  206. 206.  
  207. 207.  
  208. 208.  
  209. 209.  
  210. 210.  
  211. 211.  
  212. 212.  
  213. 213.  
  214. 214.  
  215. 215.  
  216. 216.  
  217. 217.  
  218. 218.  
  219. 219.  
  220. 220.  
  221. 221.  
  222. 222.  
  223. 223.  
  224. 224.  Status-Code
  225. 225.  SP
  226. 226.  
  227. 227.  
  228. 228.  
  229. 229.  
  230. 230.  
  231. 231.  
  232. 232.  
  233. 233.  
  234. 234.  
  235. 235.  
  236. 236.  
  237. 237.  
  238. 238.  
  239. 239.  
  240. 240.  
  241. 241.  
  242. 242.  
  243. 243.  
  244. 244.  Reason-Phrase
  245. 245.  CRLF Response = Status-Line *(header CRLF) CRLF [ message-body ]General
  246. 246.  headers:
  247. 247.  Cache-Control,
  248. 248.  Pragma,
  249. 249.  Via...Response
  250. 250.  headers:
  251. 251.  ETag,
  252. 252.  Vary,
  253. 253.  WWW-Authenticate...Entity
  254. 254.  headers:
  255. 255.  Content-Encoding,
  256. 256.  Content-Language,
  257. 257.  Expires...
  258. 258. Headers Version ContentStatus codeStatus text Charset ...
  259. 259. Hypertext Transfer Protocol Headers HTTP/1.1 200 OK Version Content-type: text/html Date:Sun, 3 Mar 2013 07:10:42 GMT ContentStatus code !DOCTYPE HTMLStatus text html lang=es head Charset meta charset=utf-8 ... ...
  260. 260. Symfony2 full-stack framework
  261. 261. Components Vendors + ➕ Bundles Bridges--------------------- Full-stack
  262. 262.  framework
  263. 263. Vendors
  264. 264. Project dependencies (third party libraries)
  265. 265. Doctrine, Twig, Assetic,Monolog and SwiftMailer
  266. 266. Doctrine, Twig, Assetic,Monolog and SwiftMailer don’t
  267. 267.  reinvent
  268. 268.  the
  269. 269.  wheel, these
  270. 270.  libraries
  271. 271.  are
  272. 272.  awesome!
  273. 273. Bundles
  274. 274. “A bundle is a directory that hasa well-defined structure and can host anything from classes to controllers and web resources.” Source: symfony.com
  275. 275. FrameworkBundle SecurityBundle TwigBundleWebProfilerBundle AsseticBundle MonologBundleSwiftmailerBundle
  276. 276. Bridges
  277. 277. Extend components and libraries to be used in Symfony2
  278. 278. Example: The Twig Bridgeadds useful functions to use forms, routing, security and translations.
  279. 279. Example: The Twig Bridge adds useful functions to use forms, routing, security andform_enctype translations.form_widgetform_errorsform_labelform_rowform_restcsrf_token
  280. 280. Example: The Twig Bridge adds useful functions to use forms, routing, security andform_enctype url translations.form_widget pathform_errorsform_labelform_rowform_restcsrf_token
  281. 281. Example: The Twig Bridge adds useful functions to use forms, routing, security andform_enctype url translations. is_grantedform_widget pathform_errorsform_labelform_rowform_restcsrf_token
  282. 282. Example: The Twig Bridge adds useful functions to use forms, routing, security andform_enctype url translations. is_grantedform_widget path transform_errors transchoiceform_labelform_rowform_restcsrf_token
  283. 283. Events
  284. 284. EventDispatcher Component
  285. 285. Mediator pattern:decouple a Producer from a Consumer ConsumerProducer Consumer Consumer
  286. 286. Mediator pattern:decouple a Producer from a Consumer ConsumerProducer Mediator Consumer Consumer
  287. 287. Mediator pattern:decouple a Producer from a Consumer ConsumerProducer Mediator Consumer Consumer addListener() dispatch()
  288. 288. Mediator pattern:decouple a Producer from a Consumer ConsumerProducer Mediator Consumer Consumer addListener() dispatch() http://goo.gl/Fr16Z Mediators - Programming With Anthony
  289. 289. kernel.request
  290. 290. Dispatched as soon as the request arrives
  291. 291. If any listener return aResponse object, all other listeners wont be called.
  292. 292. Used by FrameworkBundle topopulate the _controller value
  293. 293. kernel.controller
  294. 294. Once the controller isresolved, this event is dispatched
  295. 295. kernel.view
  296. 296. Called only if the controllerdoes not return a Response
  297. 297. Goal: build a Responseobject from the return value of the Controller
  298. 298. kernel.response
  299. 299. Allow to modify or replace theResponse object after its creation
  300. 300. Allow to modify or replace theResponse object after its creation i.e.
  301. 301.  adding
  302. 302.  the
  303. 303.  Google
  304. 304.  Analytics
  305. 305.  tracker
  306. 306.  code?
  307. 307. Allow to modify or replace theResponse object after its creation i.e.
  308. 308.  adding
  309. 309.  the
  310. 310.  Google
  311. 311.  Analytics
  312. 312.  tracker
  313. 313.  code? You
  314. 314.  could...
  315. 315.  but
  316. 316.  there
  317. 317.  are
  318. 318.  better
  319. 319.  ways
  320. 320.  ☺
  321. 321. kernel.terminate
  322. 322. Called once the Response has been sent
  323. 323. Used to run expensivepost-response jobs
  324. 324. Used to run expensivepost-response jobs i.e.
  325. 325.  sending
  326. 326.  emails,
  327. 327.  processing
  328. 328.  data...
  329. 329. kernel.exception
  330. 330. Last chance to convert an Exception into a Response object
  331. 331. Events demo
  332. 332. Thank you! Questions?https://joind.in/8225
  333. 333. Photos:http://www.flickr.com/photos/wavetraced/384824309/

×