MidwestPHP Symfony2 Internals

Post on 10-May-2015

5.624 views 1 download

Tags:

Transcript of MidwestPHP Symfony2 Internals

Symfony2 InternalsRaúl Fraile

Who am I?

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

Should I care aboutSymfony2 internals?

Yes!

Topics

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

Components

Set of decoupled and standalone libraries

Implement common features needed to develop websites

The foundation of the Symfony2 full-stack framework

BrowserKit

ClassLoader

Config

Console

CssSelector

DependencyInjection

DomCrawler

EventDispatcher

Finder

Form

HttpFoundation

HttpKernel

Locale

Process

Routing

Security

Serializer

Templating

Translation

Validator

Yaml

OptionsResolver

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

Who’s using them?

Components demo...

... after some concepts

Composer

Composer is a dependency manager for PHP

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

$ composer install

Autoload

Triggered whenever we need a class or interface not already defined

Fully Quali!ed Name (FQN)

File

Fully Quali!ed Name (FQN)

File

∖Doctrine∖DBAL∖Driver

Fully Quali!ed Name (FQN)

File

∖Doctrine∖DBAL∖Driver

[lib_path]/Doctrine/DBAL/Driver.php

Fully Quali!ed Name (FQN)

File

∖Doctrine∖DBAL∖Driver

∖Twig_Lexer

[lib_path]/Doctrine/DBAL/Driver.php

Fully Quali!ed Name (FQN)

File

∖Doctrine∖DBAL∖Driver

∖Twig_Lexer

[lib_path]/Doctrine/DBAL/Driver.php

[lib_path]/Twig/Lexer.php

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

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

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

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

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

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

This is a mess...

... we need a standard!

PSR-0

F.I.G

Framework Interop Group

In summary:

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

Time for the demo!

Request

HttpFoundation Component

Object Oriented abstraction of an HTTP request

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

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

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

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

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

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

Hypertext Transfer Protocol

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

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

Response

HttpFoundation Component

Object Oriented abstraction of an HTTP response

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

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

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

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

Headers Version

Content

Status code

Status text

Charset...

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

Symfony2 full-stack framework

ComponentsVendors➕BundlesBridges

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

+

Vendors

Project dependencies (third party libraries)

Doctrine, Twig, Assetic, Monolog and SwiftMailer

Doctrine, Twig, Assetic, Monolog and SwiftMailer

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

Bundles

“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

FrameworkBundle

SecurityBundle

TwigBundle

WebProfilerBundle

AsseticBundle

MonologBundle

SwiftmailerBundle

Bridges

Extend components and libraries to be used in Symfony2

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

translations.

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

translations.form_enctypeform_widgetform_errorsform_labelform_rowform_restcsrf_token

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

translations.form_enctypeform_widgetform_errorsform_labelform_rowform_restcsrf_token

urlpath

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

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

Events

EventDispatcher Component

Mediator pattern: decouple a Producer from a

Consumer

Producer

Consumer

Consumer

Consumer

Mediator pattern: decouple a Producer from a

Consumer

Producer

Consumer

Consumer

Consumer

Mediator

Mediator pattern: decouple a Producer from a

Consumer

Producer

Consumer

Consumer

Consumer

Mediator

addListener()dispatch()

Mediator pattern: decouple a Producer from a

Consumer

Producer

Consumer

Consumer

Consumer

Mediator

addListener()dispatch()

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

kernel.request

Dispatched as soon as the request arrives

If any listener return a Response object, all other

listeners won't be called.

Used by FrameworkBundle to populate the _controller value

kernel.controller

Once the controller is resolved, this event is dispatched

kernel.view

Called only if the controller does not return a Response

Goal: build a Response object from the return value

of the Controller

kernel.response

Allow to modify or replace the Response object after its creation

Allow to modify or replace the Response object after its creation

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

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����������� ������������������  ☺

kernel.terminate

Called once the Response has been sent

Used to run expensive post-response jobs

Used to run expensive post-response jobs

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

kernel.exception

Last chance to convert an Exception into a Response object

Events demo

Thank you!Questions?

https://joind.in/8225

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

Photos: