Welcome to the Symfony2 World - FOSDEM 2013

49
Daniel Kucharski @inspiran_be Lukas Smith @lsmith Welcome to the Symfony2 World Saturday, February 2, 13

description

Symfony2 is more then just another PHP framework. It's backed by a community of people working together to bring innovative solutions to solve customer needs. This talk will give you an introducing to Symfony2 describing commonly used components. It will further discuss the ecosystem around Symfony2 with projects such a Symfony CMF and Vespolina.

Transcript of Welcome to the Symfony2 World - FOSDEM 2013

Page 1: Welcome to the Symfony2 World - FOSDEM 2013

Daniel Kucharski @inspiran_be

Lukas Smith @lsmith

Welcome to the Symfony2 World

Saturday, February 2, 13

Page 2: Welcome to the Symfony2 World - FOSDEM 2013

• Developer at Liip AG in Switzerland

• PHP 5.3 co-release-manager,

• Contributing to Doctrine, Symfony, PHPCR ..

Speakers

Lukas Kahwe Smith

Saturday, February 2, 13

Page 3: Welcome to the Symfony2 World - FOSDEM 2013

• SAP Business Consultant by day

• PHP enthusiast at night since 2000

• Co-founder of the Vespolina Ecommerce Project

Speakers

Daniel Kucharski

Saturday, February 2, 13

Page 4: Welcome to the Symfony2 World - FOSDEM 2013

• Symfony2 is a set of standalone, decoupled and cohesive PHP

• Symfony2 is a full-stack framework

• MIT licensed (permissive, GPL compatible)

• Compliant with , PSR-1, PSR-2, PSR-3

• https://github.com/symfony/symfony

reusable

What is Symfony2?

components

also

PSR-0

Saturday, February 2, 13

Page 5: Welcome to the Symfony2 World - FOSDEM 2013

• Approaching

• Most popular PHP project on github.com

• First stable release in July 2011

• LTS release with in May 2013

• Used on many big sites: ted.com, lockerz.com, opensky.com, exercise.com, wetter.de, stern.de ..

What is Symfony2?

700 contributors

5 year support

Saturday, February 2, 13

Page 6: Welcome to the Symfony2 World - FOSDEM 2013

Components

Saturday, February 2, 13

Page 7: Welcome to the Symfony2 World - FOSDEM 2013

LocaleOptionsResolver

ProcessPropertyAccess

RoutingSecuritySerializerStopwatchTemplatingTranslationValidator

Yaml

BrowserKitClassLoader

ConfigConsole

CssSelectorDomCrawler

DependencyInjectionEventDispatcher

FilesystemFinderForm

HttpFoundationHttpKernel

Saturday, February 2, 13

Page 8: Welcome to the Symfony2 World - FOSDEM 2013

• Easily parse and write YAML files

• Implements most of the YAML 1.2 specuse Symfony\Component\Yaml\Parser;

$yaml = new Parser();

$string = file_get_contents('foo.yml')$data = $yaml->parse($string);

# data, inlining, indenting, invalid types, objects$string = $yaml->dump($data, 2, 4, false, true);file_put_contents('bar.yml', $string);

Yaml

Saturday, February 2, 13

Page 9: Welcome to the Symfony2 World - FOSDEM 2013

• OO API for HTTP Request + Response

• OO API for Session incl. “flash messages”

• Supports response

$request = Request::create('/?foo=bar', 'GET');echo $request->getPathInfo();

$resp = JsonResponse::create($data, 200);

$hdrs = array('Content-Type' => 'text/plain');$resp = new StreamingResponse($callback, 404, $hdrs);$resp->send();

Http Foundation

streaming

Saturday, February 2, 13

Page 10: Welcome to the Symfony2 World - FOSDEM 2013

• PHP level CGI interface

• Eases integration between Symfony2, Silex, Drupal8, ezPublish5, Laravel4 .. applications

HTTP Kernel

interface HttpKernelInterface{ const MASTER_REQUEST = 1; const SUB_REQUEST = 2;

public function handle( Request $request, $type = self::MASTER_REQUEST, $catch = true );}

Saturday, February 2, 13

Page 11: Welcome to the Symfony2 World - FOSDEM 2013

use Symfony\Component\HttpFoundation\Request;

// env: prod, debug: false$kernel = new AppKernel('prod', false);

// wrap the kernel for ESI enabled HTTP caching$kernel = new AppCache($kernel);

// create the request from the super globals$request = Request::createFromGlobals();

// convert request into a response$response = $kernel->handle($request);

// send response content to the client$response->send();

// event fired after browser has received response $kernel->terminate($request, $response);

HTTP Kernel

Saturday, February 2, 13

Page 12: Welcome to the Symfony2 World - FOSDEM 2013

DoctrineAsseticGuzzle

PHPUnitBehat

GoutteJackalope

..

Drupal 8ezPublish 5Laravel 4phpBB 3.5

PPISilex

shopware..

Component usageFrameworks / Applications Libraries / Components

Saturday, February 2, 13

Page 13: Welcome to the Symfony2 World - FOSDEM 2013

• , MVC framework 2nd

• Very extensible due to processing and

• Inspired by Django, Spring, Ruby on Rails

• HTTP Caching / / Reversed proxy

• Extensive documentation + debugging tools

Symfony2 Framework

HTTP framework 1st

dependency injectionevent based request

ESI

Saturday, February 2, 13

Page 14: Welcome to the Symfony2 World - FOSDEM 2013

• Selection of bundles, a default directory / file structure and default configuration

• Symfony Standard edition

• Symfony CMF Standard edition

• Sonata standard edition

• KNP Rad Edition

Distributions

php composer.phar create-project symfony/framework-standard-edition your-dir

Saturday, February 2, 13

Page 15: Welcome to the Symfony2 World - FOSDEM 2013

What is a Bundle?

Ideally, the glue code necessary to

a with the symfony2 framework (ie. configure

the Dependency Injection Container) plus any required

and

minimal connect

library

controller routes

Saturday, February 2, 13

Page 16: Welcome to the Symfony2 World - FOSDEM 2013

FrameworkBundleMonologBundleAsseticBundle

WebProfilerBundleTwigBundle

SwiftmailerBundleSecurityBundle

..

Core bundles

Saturday, February 2, 13

Page 17: Welcome to the Symfony2 World - FOSDEM 2013

KNPbundles.com

Saturday, February 2, 13

Page 18: Welcome to the Symfony2 World - FOSDEM 2013

FOSUserBundleSonataAdminBundle

FOSRestBundleStofDoctrineExtensionBundle

KnpMenuBundle..

NelmioApiDocBundleJMSDebuggingBundle

LiipCacheControlBundleKnpRadBundle

..

Popular bundles

Saturday, February 2, 13

Page 19: Welcome to the Symfony2 World - FOSDEM 2013

AsseticMonolog

Doctrine / PropelTwig

SwiftmailerPHPUnit

ImagineBehat / PHPSpec / Atoum

Buzz / GuzzleGoutte

KnpMenuJMS Serializer

The ecosystem

Core Dependencies Popular Dependencies

Saturday, February 2, 13

Page 20: Welcome to the Symfony2 World - FOSDEM 2013

Ecosystem

Saturday, February 2, 13

Page 21: Welcome to the Symfony2 World - FOSDEM 2013

Composer

Saturday, February 2, 13

Page 22: Welcome to the Symfony2 World - FOSDEM 2013

curl -s https://getcomposer.org/installer | phpphp composer.phar installphp composer.phar require acme/lib:1.1.* --no-updatephp composer.phar update acme/lib

Composer

• Dependency manager for PHP

• Based on SUSE Linux dependency resolver

• Register on

• Setup your own package index with Satis

packagist.org

Saturday, February 2, 13

Page 23: Welcome to the Symfony2 World - FOSDEM 2013

Twig

Saturday, February 2, 13

Page 24: Welcome to the Symfony2 World - FOSDEM 2013

• Template language implemented in PHP

• Fast: Compiles to PHP and Javascript

• Template inheritance

• Secure: sandbox mode, output escaping

• Easily create new tags, filters and functions

• Many community extensions are available

Twig

Saturday, February 2, 13

Page 25: Welcome to the Symfony2 World - FOSDEM 2013

{% extends "base.html" %}

{% block title %}Index{% endblock %}{% block head %} {{ parent() }} <style type="text/css"> .important { color: #336699; } </style>{% endblock %}{% block content %} {% if users|length > 0 %} <ul> {% for user in users %} <li>{{ user.username|e }}</li> {% endfor %} </ul>{% endif %}{% endblock %}

Twig

Saturday, February 2, 13

Page 26: Welcome to the Symfony2 World - FOSDEM 2013

• Cleverly handle your JS/CSS web assets

• Inspired by the Python webassets lib

• : Combine, Less, Sass ...

• : Minify, YUICompressor, ..

• Provides ways to handle cache busting

• Build assets during development or dump to static files in production

Assetic

Assets

Filters

on the fly

Saturday, February 2, 13

Page 27: Welcome to the Symfony2 World - FOSDEM 2013

{% stylesheets '@AcmeFooBundle/Resources/public/css/*' %} <link rel="stylesheet" href="{{ asset_url }}" />{% endstylesheets %}

{% javascripts 'bundles/acme_foo/js/*' filter='yui_js' %} <script src="{{ asset_url }}"></script>{% endjavascripts %}

Assetic

# Assetic Configurationassetic: use_controller: false #java: /usr/bin/java filters: cssrewrite: ~ # closure: # jar: %kernel.root_dir%/java/compiler.jar # yui_css: # jar: %kernel.root_dir%/java/yuicompressor-2.4.2.jar

Saturday, February 2, 13

Page 28: Welcome to the Symfony2 World - FOSDEM 2013

• PSR-3 compliant logging library

• Inspired by the Python logbook lib

• Handlers

• StreamHandler, RotatingFileHandler, NativeMailHandler, MongoDBHandler, ...

• FingersCrossedHandler

• Channels, Formatters, Processors

Monolog

Saturday, February 2, 13

Page 29: Welcome to the Symfony2 World - FOSDEM 2013

// create a log channel$log = new Logger('name');$fileName = 'path/to/your.log';$handler = new StreamHandler($fileName, Logger::WARNING);

// log everything if there is at least one warning or higher$log->pushHandler(new FingersCrossedHandler($handler);$logger->pushHandler(new FirePHPHandler());

$socketName = 'unix:///var/log/httpd_app_log.socket';$handler = new SocketHandler($socketName);$handler->setPersistent(true);$logger->pushHandler($handler, Logger::DEBUG);

// add records to the log$log->addWarning('Foo');$log->log(Logger::ERROR, 'Bar');

Monolog

Saturday, February 2, 13

Page 30: Welcome to the Symfony2 World - FOSDEM 2013

• Hibernate inspired data mapper API

• ORM Object Relational Mapper (SQL)

• Object Document Mapper (CouchDB, MongoDB, PHPCR, OrientDB, ..)

• DBAL / Migrations / Fixtures

• Annotations, Cache, Inflector, Lexer ..

Doctrine

Saturday, February 2, 13

Page 31: Welcome to the Symfony2 World - FOSDEM 2013

• (De)serializing on steroids

• Handles nested (recursive) object graphs

• Fully leverage XML/JSON/.. features

• for groups, versioning

• Ability to define custom exclusion strategies and events handlers

JMS Serializer

Exclusion strategies

Saturday, February 2, 13

Page 32: Welcome to the Symfony2 World - FOSDEM 2013

class BlogPost{ private $title; private $content; private $date; /* @XmlList(inline = true, entry = "tag") */ private $tags; /* @Groups({"admins"}) * @XmlAttribute * @Since("1.1.0") */ private $version; /* @VirtualProperty * @SerializedName("slug") * @XmlAttribute */ public function getSlug() { .. }

..}

JMS Serializer

Saturday, February 2, 13

Page 33: Welcome to the Symfony2 World - FOSDEM 2013

// sets the group exclusion strategy$serializer->setGroups(array(‘admins’));

// sets the version exclusion strategy// this overrides the group exclusion strategy// need to define your own class to combine the two$serializer->setVersion(‘1.2.0’);

$blogPost = new BlogPost($data);$serializer->serialize($blogPost, 'json');

JMS Serializer

Saturday, February 2, 13

Page 34: Welcome to the Symfony2 World - FOSDEM 2013

Bridges

Saturday, February 2, 13

Page 35: Welcome to the Symfony2 World - FOSDEM 2013

TwigBridgeSwiftmailerBridgeMonologBridgeDoctrineBridgePropel1Bridge

Bridges

Saturday, February 2, 13

Page 36: Welcome to the Symfony2 World - FOSDEM 2013

Applications

Saturday, February 2, 13

Page 37: Welcome to the Symfony2 World - FOSDEM 2013

The Symfony CMF project makes it easier for

to add to applications built with the

Symfony2 framework. Key development principles for the

provided are , ,

and .

Symfony CMF

developers

CMS functionality

sets of bundles scalability usability

documentation testing

Saturday, February 2, 13

Page 38: Welcome to the Symfony2 World - FOSDEM 2013

• Follows vision of the on top of Symfony2, PHPCR, Create.js

• CMF as target audience are CMS builders: Drupal 8, ezPublish 5, Symfony2 devs, ..

• Stable release planned for May 2013

• http://cmf.symfony.com

• Demo at http://cmf.liip.ch

Symfony CMF

decoupled CMS

Saturday, February 2, 13

Page 39: Welcome to the Symfony2 World - FOSDEM 2013

• Decoupled storage, business logic, UI layers

• PHPCR is a storage API for CMS, essentially a tree + versioning enabled document DB

• Symfony2 handles the business logic

• create.js is a glue layer that ties together RDFa, backbone.js, inline edit + JSON-LD

A decoupled CMS

Saturday, February 2, 13

Page 40: Welcome to the Symfony2 World - FOSDEM 2013

• Ecommerce application

• from a single person store

• to a multi national store.

• Decoupled components which can be used independently of Symfony2

• Several components will go stable in Q2

• http://www.vespolina-project.org

Vespolina Project

scalable

Saturday, February 2, 13

Page 41: Welcome to the Symfony2 World - FOSDEM 2013

Vespolina Project

vespolina/vespolinaCore

vespolina/vespolinaProduct

vespolina/vespolinaProductBundle

base classes & interfaces

manager & mappings

defining managers a DI service

Framework agnostic

Saturday, February 2, 13

Page 43: Welcome to the Symfony2 World - FOSDEM 2013

Please rate this talk on

http://joind.in/talk/view/8074

Questions?

Saturday, February 2, 13

Page 44: Welcome to the Symfony2 World - FOSDEM 2013

• HTTP client & framework

• Restful web service clients

• Used by Amazon AWS SDK, Drupal 8, ...

• Uses Symfony Event dispatcher

• Service descriptions

Guzzle

Saturday, February 2, 13

Page 45: Welcome to the Symfony2 World - FOSDEM 2013

use Guzzle\Http\Client;$client = new Client('https://api.github.com');

$request = $client->get('/user')->setAuth('user', 'pass');// Send the request and get the response$response = $request->send();

Guzzle

Saturday, February 2, 13

Page 46: Welcome to the Symfony2 World - FOSDEM 2013

{ "name": "Foo", "apiVersion": "2012-10-14", "baseUrl": "http://api.foo.com", "description": "Foo is an API", "operations": { "GetUsers": { "httpMethod": "GET", "uri": "/users", "summary": "Gets a list of users", "responseClass": "GetUsersOutput" },...

GuzzleService Description

Saturday, February 2, 13

Page 47: Welcome to the Symfony2 World - FOSDEM 2013

Guzzle... "GetUserOutput": { "type": "object", "properties": { "name": { "location": "json", "type": "string" }, "age": { "location": "json", "type": "integer"...

Service Description

Saturday, February 2, 13

Page 48: Welcome to the Symfony2 World - FOSDEM 2013

use Guzzle\Service\Description\ServiceDescription;

$description = ServiceDescription::factory('service_description.json');$client->setDescription($description);

$command = $client->getCommand('GetUser', array('id' => 123));

$responseModel = $client->execute($command);echo $responseModel['age'];

Guzzle

Saturday, February 2, 13

Page 49: Welcome to the Symfony2 World - FOSDEM 2013

• Collection of bundles to quickly create an Admin interface

• Media, News, Notification, Formatter, I18N,

Sonata Project

Saturday, February 2, 13