Symfony 2
 Kris Wallsmith
  @kriswallsmith




  April 20, 2010
An evolution of symfony 1.x
•   Same philosophy:


    •   Full stack


    •   Configurable


    •   Testable


    •   Components


•   Brand new foundation
Don’t reinvent the wheel

•   Doctrine

•   PHPUnit

•   SwiftMailer

•   Zend_Cache

•   Zend_Log
PHP 5.3


•   Namespaces


•   Anonymous functions / closures


•   Late static binding
Symfony Components


•   Standalone libraries


•   Each with a specific scope


•   components.symfony-project.org
Symfony Components
•   BrowserKit              •   Process

•   Console                 •   RequestHandler

•   CssSelector             •   Routing

•   DependencyInjection     •   Templating

•   DomCrawler              •   Yaml

•   EventDispatcher

•   OutputEscaper
Dependency Injection


•   A method of supplying an external
    dependency
Dependency Injection
class User
{
  protected $session;

    public function __construct(Session $session)
    {
      $this->session = $session;
    }
}
DI Container


•   A method of organizing dependencies

•   Adds a configuration layer

•   Dependency injection does not require a container!
DI Container
# in config.yml

parameters:
  mailer.username:          foo
  mailer.password:          bar
  mailer.class:             Zend_Mail
  mailer.transport.class:   Zend_Mail_Transport_Smtp

services:
  mail.transport:
    class: %mailer.transport.class%
    arguments:
      - smtp.gmail.com
      - { auth: login, username: %mailer.username%, password: %mailer.password%, ssl: ssl, port: 465 }
    shared: false
  mailer:
    class: %mailer.class%
    calls:
      - [setDefaultTransport, [@mail.transport]]
DI Container

# in config_dev.yml

imports:
  - config.yml

parameters:
  mailer.transport.class: Zend_Mail_Transport_Null
DI Container
use SymfonyComponentsDependencyInjection as DI;
use SymfonyComponentsDependencyInjectionLoader;

$container = new DIContainer();

$loader = new LoaderYamlFileLoader($container);
$loader->load(‘config_dev.yml’);

$mailer = $container->mailer;
Event Dispatcher


•   Implements the observer design pattern


•   Similar to events in JavaScript
Event Dispatcher
use SymfonyComponentsEventDispatcherEvent;

class Article
{
  protected $dispatcher;

    public function __construct($dispatcher)
    {
      $this->dispatcher = $dispatcher;
    }

    public function save()
    {
      // ...
      $event = new Event($this, ‘article.save’);
      $this->dispatcher->notify($event);
    }
}
Event Dispatcher
class Thumbnailer
{
  public function connect($dispatcher)
  {
    $dispatcher->connect(‘article.save’, array(
      $this,
      ‘generateArticleThumbnails’
    ));
  }

    public function generateArticleThumbnails($event)
    {
      // ...
    }
}
The Symfony 2 sandbox

• curl -L http://bit.ly/sf2sbox > sandbox.tgz
• tar xzf sandbox.tgz
• cd sandbox
• chmod a+w hello/cache/ hello/logs
• chmod a+x hello/console
The Symfony 2 sandbox
Live Demo
Follow us on GitHub
•   symfony


•   fabpot


•   jwage


•   kriswallsmith


•   bschussek


•   and 70+ other forks…
symfony-reloaded.org

Symfony 2

  • 1.
    Symfony 2 KrisWallsmith @kriswallsmith April 20, 2010
  • 2.
    An evolution ofsymfony 1.x • Same philosophy: • Full stack • Configurable • Testable • Components • Brand new foundation
  • 3.
    Don’t reinvent thewheel • Doctrine • PHPUnit • SwiftMailer • Zend_Cache • Zend_Log
  • 4.
    PHP 5.3 • Namespaces • Anonymous functions / closures • Late static binding
  • 7.
    Symfony Components • Standalone libraries • Each with a specific scope • components.symfony-project.org
  • 8.
    Symfony Components • BrowserKit • Process • Console • RequestHandler • CssSelector • Routing • DependencyInjection • Templating • DomCrawler • Yaml • EventDispatcher • OutputEscaper
  • 9.
    Dependency Injection • A method of supplying an external dependency
  • 10.
    Dependency Injection class User { protected $session; public function __construct(Session $session) { $this->session = $session; } }
  • 11.
    DI Container • A method of organizing dependencies • Adds a configuration layer • Dependency injection does not require a container!
  • 12.
    DI Container # inconfig.yml parameters: mailer.username: foo mailer.password: bar mailer.class: Zend_Mail mailer.transport.class: Zend_Mail_Transport_Smtp services: mail.transport: class: %mailer.transport.class% arguments: - smtp.gmail.com - { auth: login, username: %mailer.username%, password: %mailer.password%, ssl: ssl, port: 465 } shared: false mailer: class: %mailer.class% calls: - [setDefaultTransport, [@mail.transport]]
  • 13.
    DI Container # inconfig_dev.yml imports: - config.yml parameters: mailer.transport.class: Zend_Mail_Transport_Null
  • 14.
    DI Container use SymfonyComponentsDependencyInjectionas DI; use SymfonyComponentsDependencyInjectionLoader; $container = new DIContainer(); $loader = new LoaderYamlFileLoader($container); $loader->load(‘config_dev.yml’); $mailer = $container->mailer;
  • 15.
    Event Dispatcher • Implements the observer design pattern • Similar to events in JavaScript
  • 16.
    Event Dispatcher use SymfonyComponentsEventDispatcherEvent; classArticle { protected $dispatcher; public function __construct($dispatcher) { $this->dispatcher = $dispatcher; } public function save() { // ... $event = new Event($this, ‘article.save’); $this->dispatcher->notify($event); } }
  • 17.
    Event Dispatcher class Thumbnailer { public function connect($dispatcher) { $dispatcher->connect(‘article.save’, array( $this, ‘generateArticleThumbnails’ )); } public function generateArticleThumbnails($event) { // ... } }
  • 18.
    The Symfony 2sandbox • curl -L http://bit.ly/sf2sbox > sandbox.tgz • tar xzf sandbox.tgz • cd sandbox • chmod a+w hello/cache/ hello/logs • chmod a+x hello/console
  • 19.
  • 20.
  • 21.
    Follow us onGitHub • symfony • fabpot • jwage • kriswallsmith • bschussek • and 70+ other forks…
  • 22.