MidwestPHP Symfony2 Internals

102
Symfony2 Internals Raúl Fraile

Transcript of MidwestPHP Symfony2 Internals

Page 1: MidwestPHP Symfony2 Internals

Symfony2 InternalsRaúl Fraile

Page 2: MidwestPHP Symfony2 Internals

Who am I?

Page 3: MidwestPHP Symfony2 Internals

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

Page 4: MidwestPHP Symfony2 Internals

Should I care aboutSymfony2 internals?

Page 5: MidwestPHP Symfony2 Internals
Page 6: MidwestPHP Symfony2 Internals

Yes!

Page 7: MidwestPHP Symfony2 Internals
Page 9: MidwestPHP Symfony2 Internals

Topics

Page 10: MidwestPHP Symfony2 Internals

1. Symfony2 components2. Composer3. Autoloading & PSR-04. Request & Response5. Demo 16. Symfony2 full-stack framework7. Events8. Demo 2

Page 11: MidwestPHP Symfony2 Internals

Components

Page 12: MidwestPHP Symfony2 Internals

Set of decoupled and standalone libraries

Page 13: MidwestPHP Symfony2 Internals

Implement common features needed to develop websites

Page 14: MidwestPHP Symfony2 Internals

The foundation of the Symfony2 full-stack framework

Page 15: MidwestPHP Symfony2 Internals

BrowserKit

ClassLoader

Config

Console

CssSelector

DependencyInjection

DomCrawler

EventDispatcher

Finder

Form

HttpFoundation

HttpKernel

Locale

Process

Routing

Security

Serializer

Templating

Translation

Validator

Yaml

OptionsResolver

Page 16: MidwestPHP Symfony2 Internals

BrowserKit

ClassLoader

Config

Console

CssSelector

DependencyInjection

DomCrawler

EventDispatcher

Finder

Form

HttpFoundation

HttpKernel

Locale

Process

Routing

Security

Serializer

Templating

Translation

Validator

Yaml

StopWatch

PropertyAccess

2.2.0OptionsResolver

Page 17: MidwestPHP Symfony2 Internals

Who’s using them?

Page 18: MidwestPHP Symfony2 Internals

Components demo...

Page 19: MidwestPHP Symfony2 Internals

... after some concepts

Page 20: MidwestPHP Symfony2 Internals

Composer

Page 21: MidwestPHP Symfony2 Internals
Page 22: MidwestPHP Symfony2 Internals

Composer is a dependency manager for PHP

Page 23: MidwestPHP Symfony2 Internals

{ "require": { "symfony/http-foundation": "v2.2.0", "doctrine/orm": ">=2.2.3", }}

Page 24: MidwestPHP Symfony2 Internals

$ composer install

Page 25: MidwestPHP Symfony2 Internals

Autoload

Page 26: MidwestPHP Symfony2 Internals

Triggered whenever we need a class or interface not already defined

Page 27: MidwestPHP Symfony2 Internals

Fully Quali!ed Name (FQN)

File

Page 28: MidwestPHP Symfony2 Internals

Fully Quali!ed Name (FQN)

File

∖Doctrine∖DBAL∖Driver

Page 29: MidwestPHP Symfony2 Internals

Fully Quali!ed Name (FQN)

File

∖Doctrine∖DBAL∖Driver

[lib_path]/Doctrine/DBAL/Driver.php

Page 30: MidwestPHP Symfony2 Internals

Fully Quali!ed Name (FQN)

File

∖Doctrine∖DBAL∖Driver

∖Twig_Lexer

[lib_path]/Doctrine/DBAL/Driver.php

Page 31: MidwestPHP Symfony2 Internals

Fully Quali!ed Name (FQN)

File

∖Doctrine∖DBAL∖Driver

∖Twig_Lexer

[lib_path]/Doctrine/DBAL/Driver.php

[lib_path]/Twig/Lexer.php

Page 32: MidwestPHP Symfony2 Internals

bool spl_autoload_register( [callable $autoload_function, [bool $throw = true, [bool $prepend = false]]])

Page 33: MidwestPHP Symfony2 Internals

<?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

Page 34: MidwestPHP Symfony2 Internals

<?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()

Page 35: MidwestPHP Symfony2 Internals

<?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’)

Page 36: MidwestPHP Symfony2 Internals

<?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’)

Page 37: MidwestPHP Symfony2 Internals

<?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’

Page 38: MidwestPHP Symfony2 Internals

This is a mess...

Page 39: MidwestPHP Symfony2 Internals

... we need a standard!

Page 40: MidwestPHP Symfony2 Internals

PSR-0

Page 41: MidwestPHP Symfony2 Internals

F.I.G

Framework Interop Group

Page 42: MidwestPHP Symfony2 Internals

In summary:

1) ‘/’ and ‘_’ are converted to DIRECTORY_SEPARATOR2) Suffixed with .php

Page 43: MidwestPHP Symfony2 Internals

Time for the demo!

Page 44: MidwestPHP Symfony2 Internals

Request

Page 45: MidwestPHP Symfony2 Internals

HttpFoundation Component

Object Oriented abstraction of an HTTP request

Page 46: MidwestPHP Symfony2 Internals

Request = Request-Line *(header CRLF) CRLF [ message-body ]

http://www.ietf.org/rfc/rfc2616.txt

Page 47: MidwestPHP Symfony2 Internals

Request = Request-Line *(header CRLF) CRLF [ message-body ]

http://www.ietf.org/rfc/rfc2616.txtRequest-Line����������� ������������������  =����������� ������������������  Method����������� ������������������  SP����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  Request-URI����������� ������������������  SP����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  HTTP-Version����������� ������������������  CRLF

Page 48: MidwestPHP Symfony2 Internals

Request = Request-Line *(header CRLF) CRLF [ message-body ]

http://www.ietf.org/rfc/rfc2616.txtRequest-Line����������� ������������������  =����������� ������������������  Method����������� ������������������  SP����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  Request-URI����������� ������������������  SP����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  HTTP-Version����������� ������������������  CRLF

General����������� ������������������  headers:����������� ������������������  Cache-Control,����������� ������������������  Transfer-Encoding,����������� ������������������  Via...Request����������� ������������������  headers:����������� ������������������  Accept-Encoding,����������� ������������������  Accept-Language,����������� ������������������  If-Match...Entity����������� ������������������  headers:����������� ������������������  Content-Encoding,����������� ������������������  Content-Language,����������� ������������������  Expires...

Page 49: MidwestPHP Symfony2 Internals

GET /index.php HTTP/1.1Host: test.comAccept-Language:en;q=0.8Accept-Encoding:gzipUser-Agent: Mozilla/5.0

Hypertext Transfer Protocol

Page 50: MidwestPHP Symfony2 Internals

GET /index.php HTTP/1.1Host: test.comAccept-Language:en;q=0.8Accept-Encoding:gzipUser-Agent: Mozilla/5.0

Hypertext Transfer Protocol

$_GET

$_POST

$_COOKIE

$_FILES

$_SERVER

Page 51: MidwestPHP Symfony2 Internals

GET /index.php HTTP/1.1Host: test.comAccept-Language:en;q=0.8Accept-Encoding:gzipUser-Agent: Mozilla/5.0

Hypertext Transfer Protocol

$_GET

$_POST

$_COOKIE

$_FILES

$_SERVER

query request cookies

filesserver

headersgetHost

getClientIp...

Page 52: MidwestPHP Symfony2 Internals

Response

Page 53: MidwestPHP Symfony2 Internals

HttpFoundation Component

Object Oriented abstraction of an HTTP response

Page 54: MidwestPHP Symfony2 Internals

Response = Status-Line *(header CRLF) CRLF [ message-body ]

http://www.ietf.org/rfc/rfc2616.txt

Page 55: MidwestPHP Symfony2 Internals

Response = Status-Line *(header CRLF) CRLF [ message-body ]

http://www.ietf.org/rfc/rfc2616.txtStatus-Line����������� ������������������  =����������� ������������������  HTTP-Version����������� ������������������  SP����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  Status-Code����������� ������������������  SP����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  Reason-Phrase����������� ������������������  CRLF

Page 56: MidwestPHP Symfony2 Internals

Response = Status-Line *(header CRLF) CRLF [ message-body ]

http://www.ietf.org/rfc/rfc2616.txtStatus-Line����������� ������������������  =����������� ������������������  HTTP-Version����������� ������������������  SP����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  Status-Code����������� ������������������  SP����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  Reason-Phrase����������� ������������������  CRLF

General����������� ������������������  headers:����������� ������������������  Cache-Control,����������� ������������������  Pragma,����������� ������������������  Via...Response����������� ������������������  headers:����������� ������������������  ETag,����������� ������������������  Vary,����������� ������������������  WWW-Authenticate...Entity����������� ������������������  headers:����������� ������������������  Content-Encoding,����������� ������������������  Content-Language,����������� ������������������  Expires...

Page 57: MidwestPHP Symfony2 Internals

Headers Version

Content

Status code

Status text

Charset...

Page 58: MidwestPHP Symfony2 Internals

HTTP/1.1 200 OKContent-type: text/htmlDate:Sun, 3 Mar 2013 07:10:42 GMT

<!DOCTYPE HTML><html lang="es"> <head> <meta charset="utf-8"> ...

Hypertext Transfer Protocol

Headers Version

Content

Status code

Status text

Charset...

Page 59: MidwestPHP Symfony2 Internals

Symfony2 full-stack framework

Page 60: MidwestPHP Symfony2 Internals

ComponentsVendors➕BundlesBridges

---------------------Full-stack����������� ������������������  framework

+

Page 61: MidwestPHP Symfony2 Internals

Vendors

Page 62: MidwestPHP Symfony2 Internals

Project dependencies (third party libraries)

Page 63: MidwestPHP Symfony2 Internals

Doctrine, Twig, Assetic, Monolog and SwiftMailer

Page 64: MidwestPHP Symfony2 Internals

Doctrine, Twig, Assetic, Monolog and SwiftMailer

don’t����������� ������������������  reinvent����������� ������������������  the����������� ������������������  wheel,these����������� ������������������  libraries����������� ������������������  are����������� ������������������  awesome!

Page 65: MidwestPHP Symfony2 Internals

Bundles

Page 66: MidwestPHP Symfony2 Internals

“A bundle is a directory that has a well-defined structure and can

host anything from classes to controllers and web resources.”

Source: symfony.com

Page 67: MidwestPHP Symfony2 Internals

FrameworkBundle

SecurityBundle

TwigBundle

WebProfilerBundle

AsseticBundle

MonologBundle

SwiftmailerBundle

Page 68: MidwestPHP Symfony2 Internals

Bridges

Page 69: MidwestPHP Symfony2 Internals

Extend components and libraries to be used in Symfony2

Page 70: MidwestPHP Symfony2 Internals

Example: The Twig Bridge adds useful functions to use forms, routing, security and

translations.

Page 71: MidwestPHP Symfony2 Internals

Example: The Twig Bridge adds useful functions to use forms, routing, security and

translations.form_enctypeform_widgetform_errorsform_labelform_rowform_restcsrf_token

Page 72: MidwestPHP Symfony2 Internals

Example: The Twig Bridge adds useful functions to use forms, routing, security and

translations.form_enctypeform_widgetform_errorsform_labelform_rowform_restcsrf_token

urlpath

Page 73: MidwestPHP Symfony2 Internals

Example: The Twig Bridge adds useful functions to use forms, routing, security and

translations.form_enctypeform_widgetform_errorsform_labelform_rowform_restcsrf_token

urlpath

is_granted

Page 74: MidwestPHP Symfony2 Internals

Example: The Twig Bridge adds useful functions to use forms, routing, security and

translations.form_enctypeform_widgetform_errorsform_labelform_rowform_restcsrf_token

urlpath

is_granted

transtranschoice

Page 75: MidwestPHP Symfony2 Internals

Events

Page 76: MidwestPHP Symfony2 Internals

EventDispatcher Component

Page 77: MidwestPHP Symfony2 Internals

Mediator pattern: decouple a Producer from a

Consumer

Producer

Consumer

Consumer

Consumer

Page 78: MidwestPHP Symfony2 Internals

Mediator pattern: decouple a Producer from a

Consumer

Producer

Consumer

Consumer

Consumer

Mediator

Page 79: MidwestPHP Symfony2 Internals

Mediator pattern: decouple a Producer from a

Consumer

Producer

Consumer

Consumer

Consumer

Mediator

addListener()dispatch()

Page 80: MidwestPHP Symfony2 Internals

Mediator pattern: decouple a Producer from a

Consumer

Producer

Consumer

Consumer

Consumer

Mediator

addListener()dispatch()

http://goo.gl/Fr16ZMediators - Programming With Anthony

Page 81: MidwestPHP Symfony2 Internals

kernel.request

Page 82: MidwestPHP Symfony2 Internals

Dispatched as soon as the request arrives

Page 83: MidwestPHP Symfony2 Internals

If any listener return a Response object, all other

listeners won't be called.

Page 84: MidwestPHP Symfony2 Internals

Used by FrameworkBundle to populate the _controller value

Page 85: MidwestPHP Symfony2 Internals

kernel.controller

Page 86: MidwestPHP Symfony2 Internals

Once the controller is resolved, this event is dispatched

Page 87: MidwestPHP Symfony2 Internals

kernel.view

Page 88: MidwestPHP Symfony2 Internals

Called only if the controller does not return a Response

Page 89: MidwestPHP Symfony2 Internals

Goal: build a Response object from the return value

of the Controller

Page 90: MidwestPHP Symfony2 Internals

kernel.response

Page 91: MidwestPHP Symfony2 Internals

Allow to modify or replace the Response object after its creation

Page 92: MidwestPHP Symfony2 Internals

Allow to modify or replace the Response object after its creation

i.e.����������� ������������������  adding����������� ������������������  the����������� ������������������  Google����������� ������������������  Analytics����������� ������������������  tracker����������� ������������������  code?

Page 93: MidwestPHP Symfony2 Internals

Allow to modify or replace the Response object after its creation

i.e.����������� ������������������  adding����������� ������������������  the����������� ������������������  Google����������� ������������������  Analytics����������� ������������������  tracker����������� ������������������  code?

You����������� ������������������  could...����������� ������������������  but����������� ������������������  there����������� ������������������  are����������� ������������������  better����������� ������������������  ways����������� ������������������  ☺

Page 94: MidwestPHP Symfony2 Internals

kernel.terminate

Page 95: MidwestPHP Symfony2 Internals

Called once the Response has been sent

Page 96: MidwestPHP Symfony2 Internals

Used to run expensive post-response jobs

Page 97: MidwestPHP Symfony2 Internals

Used to run expensive post-response jobs

i.e.����������� ������������������  sending����������� ������������������  emails,����������� ������������������  processing����������� ������������������  data...

Page 98: MidwestPHP Symfony2 Internals

kernel.exception

Page 99: MidwestPHP Symfony2 Internals

Last chance to convert an Exception into a Response object

Page 100: MidwestPHP Symfony2 Internals

Events demo

Page 101: MidwestPHP Symfony2 Internals

Thank you!Questions?

https://joind.in/8225

Page 102: MidwestPHP Symfony2 Internals

http://www.flickr.com/photos/wavetraced/384824309/

Photos: