symfony components ipc 2009
TRANSCRIPT
-
8/17/2019 Symfony Components Ipc 2009
1/108
Symfony Components
What's in for you?
Fabien Potencier
-
8/17/2019 Symfony Components Ipc 2009
2/108
Fabien Potencier
• Serial entrepreneur and developer by passion
•
Founder of Sensio (in 1998)
–
A services and consulting companyspecialized in Web technologies
and Internet marketing (France and USA)–
70 people
–
Open-Source specialists
– Big corporate customers
–
Consulting, training, development, web design, … and more– Sponsor of a lot of Open-Source projects
like symfony and Doctrine
-
8/17/2019 Symfony Components Ipc 2009
3/108
Fabien Potencier
• Creator and lead developer of symfony…
•
and creator and lead developer of some more OSS:
–
symfony components
–
Swift Mailer : Powerful component based mailing library for PHP
–
Twig : Fexible, fast, and secure template language for PHP– Pirum : Simple PEAR Channel Server Manager
– Sismo : PHP continuous integration server
– Lime : Easy to use unit testing library for PHP
–
Twitto : A web framework in a tweet
– Twittee : A Dependency Injection Container in a tweet
–
Pimple : A small PHP 5.3 dependency injection container
-
8/17/2019 Symfony Components Ipc 2009
4/108
Fabien Potencier
•
Read my technical blog: http://fabien.potencier.org/
•
Follow me on Twitter: @fabpot
• Fork my code on Github: http://github.com/fabpot/
-
8/17/2019 Symfony Components Ipc 2009
5/108
How many of you use
symfony?
-
8/17/2019 Symfony Components Ipc 2009
6/108
symfony
•
Full-stack framework (MVC architecture)
•
symfony provides the infrastructure/tools needed for 95% of the web
projects
• Open-Source (MIT License) since 2005
• Based on
–
11 years of Sensio experience building websites for its customers
–
Existing Open-Source projects
-
8/17/2019 Symfony Components Ipc 2009
7/108
But wait,
symfony is a monolithic
framework, right?
-
8/17/2019 Symfony Components Ipc 2009
8/108
A bit of history
symfony 1.0 (January 2007) started as a glue between existingOpen-Source libraries
-
8/17/2019 Symfony Components Ipc 2009
9/108
A bit of history
symfony 1.1 (June 2008) was a big refactoring of the code base
–
Decoupled the main component: like Forms, Routing, Cache, YAML, ORMs, …
platform
sfEventDispatcher
sfRequest sfResponsesfRouting sfUser
sfStorage sfCache
sfI18N
sfForm
sfLogger
sfDatabase
sfValidator sfWidget
sfYAML
sfCoreAutoload
sfOutputEscaper
-
8/17/2019 Symfony Components Ipc 2009
10/108
The symfony (1.2/1.3/1.4) MVCframework is based on a set of
cohesive but decoupled classes,the symfony components
-
8/17/2019 Symfony Components Ipc 2009
11/108
Symfony Components
•
Announced in May 2009
•
Standalone components
•
Packaged individually
•
No dependencies
•
Release cycle independent of Symfony, the framework
-
8/17/2019 Symfony Components Ipc 2009
12/108
Symfony Components
Dedicated website for each component (with code and documentation)http://components.symfony-project.org/
Dedicated Subversion and Git repository
http://svn.symfony-project.com/components/
http://github.com/fabpot
-
8/17/2019 Symfony Components Ipc 2009
13/108
Symfony Components
They have been migrated to PHP 5.3
http://svn.symfony-project.com/branches/2.0/lib/Symfony/Components
-
8/17/2019 Symfony Components Ipc 2009
14/108
Symfony Components
Each “old” PHP 5.2 symfony component has been branched
and a 1.0 version will be released soon
http://svn.symfony-project.com/components/
-
8/17/2019 Symfony Components Ipc 2009
15/108
Symfony Components
•
Extracted from symfony 1
– Event Dispatcher
– YAML
–
Output Escaper
•
Written from scratch for Symfony 2
– Dependency Injection Container
– Request Handler
–
Templating• We don’t want to duplicate eff ort done by Zend Framework
-
8/17/2019 Symfony Components Ipc 2009
16/108
-
8/17/2019 Symfony Components Ipc 2009
17/108
-
8/17/2019 Symfony Components Ipc 2009
18/108
-
8/17/2019 Symfony Components Ipc 2009
19/108
-
8/17/2019 Symfony Components Ipc 2009
20/108
-
8/17/2019 Symfony Components Ipc 2009
21/108
-
8/17/2019 Symfony Components Ipc 2009
22/108
-
8/17/2019 Symfony Components Ipc 2009
23/108
-
8/17/2019 Symfony Components Ipc 2009
24/108
-
8/17/2019 Symfony Components Ipc 2009
25/108
-
8/17/2019 Symfony Components Ipc 2009
26/108
-
8/17/2019 Symfony Components Ipc 2009
27/108
-
8/17/2019 Symfony Components Ipc 2009
28/108
-
8/17/2019 Symfony Components Ipc 2009
29/108
-
8/17/2019 Symfony Components Ipc 2009
30/108
-
8/17/2019 Symfony Components Ipc 2009
31/108
-
8/17/2019 Symfony Components Ipc 2009
32/108
-
8/17/2019 Symfony Components Ipc 2009
33/108
-
8/17/2019 Symfony Components Ipc 2009
34/108
-
8/17/2019 Symfony Components Ipc 2009
35/108
-
8/17/2019 Symfony Components Ipc 2009
36/108
-
8/17/2019 Symfony Components Ipc 2009
37/108
-
8/17/2019 Symfony Components Ipc 2009
38/108
-
8/17/2019 Symfony Components Ipc 2009
39/108
-
8/17/2019 Symfony Components Ipc 2009
40/108
-
8/17/2019 Symfony Components Ipc 2009
41/108
Symfony\Components\Templating
Symfony Templating
-
8/17/2019 Symfony Components Ipc 2009
42/108
Symfony Templating
•
Thin layer on top of PHP
that adds some template-oriented features
• Can work with Template Engines (Smarty, Twig, …)
New Templating Framework
-
8/17/2019 Symfony Components Ipc 2009
43/108
New Templating Framework
•
4 sub-components
–
Template Engine
–
Template Renderers
–
Template Loaders– Template Storages
-
8/17/2019 Symfony Components Ipc 2009
44/108
-
8/17/2019 Symfony Components Ipc 2009
45/108
-
8/17/2019 Symfony Components Ipc 2009
46/108
-
8/17/2019 Symfony Components Ipc 2009
47/108
-
8/17/2019 Symfony Components Ipc 2009
48/108
-
8/17/2019 Symfony Components Ipc 2009
49/108
use Symfony\Components\Templating\Engine;
use Symfony\Components\Templating\Loader\FilesystemLoader;
$loader = new FilesystemLoader(
'/path/to/templates/%name%.php'
);
$t = new Engine($loader);
echo $t->render('index', array('name' => 'Fabien'));
Template Loaders
-
8/17/2019 Symfony Components Ipc 2009
50/108
p
•
No assumption about where and how templates are to be found
–
Filesystem
– Database
–
Memory, …
•
Built-in loaders: FilesystemLoader, ChainLoader, CacheLoader•
Template names are « logical » names:
$loader = new FilesystemLoader('/path/to/templates/%name%.php');
Template Renderers
-
8/17/2019 Symfony Components Ipc 2009
51/108
p
•
No assumption about the format of the templates
•
Template names are prexed with the renderer name:
– index == php:index
– user:index
$t = new Engine($loader, array('user' => new ProjectTemplateRenderer(),'php' => new PhpRenderer(),
));
Template Embedding
-
8/17/2019 Symfony Components Ipc 2009
52/108
p g
Hello
Template Inheritance
-
8/17/2019 Symfony Components Ipc 2009
53/108
p
Hello
Template Slots
-
8/17/2019 Symfony Components Ipc 2009
54/108
Hello World!
Template Multiple Inheritance
-
8/17/2019 Symfony Components Ipc 2009
55/108
A layout can be decorated by another layout
Each layout can override slots
-
8/17/2019 Symfony Components Ipc 2009
56/108
Templating
A CMS example
-
8/17/2019 Symfony Components Ipc 2009
57/108
http://fabien.potencier.org/talk/33/php-barcelona-symfony-2-0-on-PHP-5-3?position=76
-
8/17/2019 Symfony Components Ipc 2009
58/108
Symfony Components
Dependency Injection
-
8/17/2019 Symfony Components Ipc 2009
59/108
Symfony\Components\DependencyInjection
-
8/17/2019 Symfony Components Ipc 2009
60/108
« Dependency Injection is where components
are given their dependencies through their
constructors, methods, or directly into elds. »
http://www.picocontainer.org/injection.html
-
8/17/2019 Symfony Components Ipc 2009
61/108
class Message{public function __construct()
{$this->output = new Output();
}}
class Message{public function __construct(OutputInterface $output){$this->output = $output;
}}
DI Hello World example
-
8/17/2019 Symfony Components Ipc 2009
62/108
class Message{public function __construct(OutputInterface $output, array $options){$this->output = $output;
$this->options = array_merge(array('with_newline' => false), $options);}
public function say($msg){
$this->output->render($msg.($this->options['with_newline'] ? "\n" : ''));
}}
DI Hello World example
-
8/17/2019 Symfony Components Ipc 2009
63/108
interface OutputInterface
{ public function render($msg);}
class Output implements OutputInterface{
public function render($msg){echo $msg;
}}
class FancyOutput implements OutputInterface{
public function render($msg){echo sprintf("\033[33m%s\033[0m", $msg);
}}
DI Hello World example
-
8/17/2019 Symfony Components Ipc 2009
64/108
$output = new FancyOutput();$message = new Message($output, array('with_newline' => true));
$message->say('Hello World');
-
8/17/2019 Symfony Components Ipc 2009
65/108
A DI container facilitates
objects description and object relationships,
congures and instantiates objects
DI Container Hello World example
-
8/17/2019 Symfony Components Ipc 2009
66/108
use Symfony\Components\DependencyInjection\Builder;use Symfony\Components\DependencyInjection\Reference;
$container = new Builder();
$container->register('output', 'FancyOutput');
$container->register('message', 'Message')->setArguments(array(new Reference('output'), array('with_newline' => true)))
;
$container->message->say('Hello World!');
$ $ t i
-
8/17/2019 Symfony Components Ipc 2009
67/108
Get the conguration for the message service
The Message constructor must be given an output service
Get the output object from the container
Create a Message object by passing the constructor arguments
$message = $container->message;
-
8/17/2019 Symfony Components Ipc 2009
68/108
$message = $container->message;
is roughly equivalent to
$output = new FancyOutput();$message = new Message($output, array('with_newline' => true));
$container = new Builder();
$container->register('output', 'FancyOutput');$container->register('message', 'Message')->
( ( f (' ') (' i h li ' )))
-
8/17/2019 Symfony Components Ipc 2009
69/108
setArguments(array(new Reference('output'), array('with_newline' => true)));
$container->message->say('Hello World!');
true
$container = new Builder();$loader = new XmlFileLoader($container);$loader->load('services.xml');
PHP
XML
XML is validated
against an XSD
$container = new Builder();
$container->register('output', 'FancyOutput');$container->
register('message' 'Message')->
-
8/17/2019 Symfony Components Ipc 2009
70/108
register( message , Message )->setArguments(array(new sfServiceReference('output'), array('with_newline' => true)))
;
$container->message->say('Hello World!');
services:output: { class: FancyOutput }message:class: Message
arguments:- @output- { with_newline: true }
$container = new Builder();$loader = new YamlFileLoader($container);$loader->load('services.yml');
PHP
YAML
FancyOutput
" "
-
8/17/2019 Symfony Components Ipc 2009
71/108
true
%message.options%
$container = new Builder();
$loader = new XmlFileLoader($container);$loader->load('services.xml');
-
8/17/2019 Symfony Components Ipc 2009
72/108
%message.options%
FancyOutput
true
$container = new Builder();$loader = new FileXmlFileLoader($container);$loader->load('services.xml');
-
8/17/2019 Symfony Components Ipc 2009
73/108
%message.options%
parameters:
output.class: FancyOutput
message.options: { with_newline: true }
$container = new Builder();
$loader = new XmlFileLoader($container);$loader->load('services.xml');
Loaders & Dumpers
-
8/17/2019 Symfony Components Ipc 2009
74/108
•
IniFileLoader
•
XmlFileLoader
•
YamlFileLoader
•
XmlDumper
•
YamlDumper
•
PhpDumper
•
GraphvizDumper
Make your container
VERY fast
use Symfony\Components\DependencyInjection\Builder;
-
8/17/2019 Symfony Components Ipc 2009
75/108
use Symfony\Components\DependencyInjection\Builder;use Symfony\Components\DependencyInjection\Reference;use Symfony\Components\DependencyInjection\Dumper\XmlDumper;use Symfony\Components\DependencyInjection\Dumper\YamlDumper;use Symfony\Components\DependencyInjection\Dumper\PhpDumper;use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;use Symfony\Components\DependencyInjection\Loader\YamlFileLoader;
$container = new Builder();
$container->register('output', 'FancyOutput');
$container-> register('message', 'Message')-> setArguments(array(new Reference('output'), array('with_newline' => true)))
;
$dumper = new XmlDumper($container);
-
8/17/2019 Symfony Components Ipc 2009
76/108
file_put_contents(__DIR__.'/container.xml', $dumper->dump());
$loader = new XmlFileLoader($container);$loader->load(__DIR__.'/container.xml');
$dumper = new YamlDumper($container);
file_put_contents(__DIR__.'/container.yml', $dumper->dump());
$loader = new YamlFileLoader($container);$loader->load(__DIR__.'/container.yml');
$dumper = new PhpDumper($container);echo $dumper->dump();
use Symfony\Components\DependencyInjection\Container;use Symfony\Components\DependencyInjection\Reference;use Symfony\Components\DependencyInjection\Parameter;
l P j tS i C t i t d C t i
-
8/17/2019 Symfony Components Ipc 2009
77/108
class ProjectServiceContainer extends Container{
protected $shared = array();
protected function getOutputService(){if (isset($this->shared['output'])) return $this->shared['output'];
$instance = new FancyOutput();
return $this->shared['output'] = $instance;}
protected function getMessageService(){if (isset($this->shared['message'])) return $this->shared['message'];
$instance = new Message($this->getService('output'), array('with_newline' => true));
return $this->shared['message'] = $instance;}
}
-
8/17/2019 Symfony Components Ipc 2009
78/108
use Symfony\Components\DependencyInjection\Dumper\GraphvizDumper;
$dumper = new GraphvizDumper($container);echo $dumper->dump();
-
8/17/2019 Symfony Components Ipc 2009
79/108
digraph sc {ratio="compress"node [fontsize="11" fontname="Arial" shape="record"];edge [fontsize="9" fontname="Arial" color="grey" arrowhead="open" arrowsize="0.5"];
node_output [label="output\nFancyOutput\n", shape=record, fillcolor="#eeeeee", style="filled"];node_message [label="message\nMessage\n", shape=record, fillcolor="#eeeeee", style="filled"];node_service_container [label="service_container\nSymfony\\Components\\DependencyInjection\\Builder
\n", shape=record, fillcolor="#9999ff", style="filled"];node_message -> node_output [label="" style="filled"];
}
-
8/17/2019 Symfony Components Ipc 2009
80/108
-
8/17/2019 Symfony Components Ipc 2009
81/108
-
8/17/2019 Symfony Components Ipc 2009
82/108
Symfony Components
Output Escaper
f
-
8/17/2019 Symfony Components Ipc 2009
83/108
Symfony\Components\OutputEscaper
Output Escaper
P id t ti XSS t ti f t l t
-
8/17/2019 Symfony Components Ipc 2009
84/108
•
Provides automatic XSS protection for your templates
•
By wrapping template variables• Works for
– strings
–
arrays
–
objects• properties
• methods
•
__call(), __get(), …
• Iterators, Coutables, …
•
…
• Works for deep method calls
-
8/17/2019 Symfony Components Ipc 2009
85/108
use Symfony\Components\OutputEscaper\Escaper;
$title = 'Foo
';echo Escaper::escape(ESC_SPECIALCHARS, $title);
use Symfony\Components\OutputEscaper\Escaper;
-
8/17/2019 Symfony Components Ipc 2009
86/108
$article = array('title' => 'Foo
','author' => array('name' => 'Fabien
',)
);
$article = Escaper::escape(ESC_SPECIALCHARS, $article);
echo $article['title']."\n";
echo $article['author']['name']."\n";
class Article{protected $title;protected $author;
-
8/17/2019 Symfony Components Ipc 2009
87/108
public $full_title;
public function __construct($title, Author $author){$this->title = $title;$this->full_title = $title;$this->author = $author;
}
public function getTitle() { return $this->title; }public function getAuthor() { return $this->author; }public function __get($key) { return $this->$key; }public function __call($method, $arguments){return $this->{'get'.$method}();
}}
public property
public method
public method returning
another object
magic __get()magic __call()
-
8/17/2019 Symfony Components Ipc 2009
88/108
class Author{protected $name;
public function __construct($name) { $this->name = $name; }public function getName() { return $this->name; }
}
use Symfony\Components\OutputEscaper\Escaper;
-
8/17/2019 Symfony Components Ipc 2009
89/108
$article = new Article(
'foo
',new Author('Fabien
'));
$article = Escaper::escape(ESC_SPECIALCHARS, $article);
echo $article->getTitle()."\n";echo $article->getAuthor()->getName()."\n";echo $article->full_title."\n";
echo $article->title."\n";echo $article->title()."\n";
Escaping Strategy
-
8/17/2019 Symfony Components Ipc 2009
90/108
echo $article->getHtmlContent(ESC_RAW);
echo $article->getTitle(ESC_JS);
explicitly ask
for raw data
change the default
escaping strategy
with Matthew Weier O’Pheinney
symfony-live.com
-
8/17/2019 Symfony Components Ipc 2009
91/108
I will reveal the first alpha release of Symfony 2.0!
with Matthew Weier O’Pheinney
symfony-live.com
-
8/17/2019 Symfony Components Ipc 2009
92/108
… with Matthew Weier O'Phinney as a special guest
-
8/17/2019 Symfony Components Ipc 2009
93/108
Thank you!
My slides on slideshare.com/fabpotwith a bonus: How to use Symfony Components with ZF!
Sensio S.A.
92 98 boulevard Victor Hugo
-
8/17/2019 Symfony Components Ipc 2009
94/108
92-98, boulevard Victor Hugo
92 115 Clichy Cedex
FRANCE
Tél. : +33 1 40 99 80 80
ContactFabien Potencier
fabien.potencier at sensio.com
http://www.sensiolabs.com/
http://www.symfony-project.org/http://fabien.potencier.org/
BONUS
-
8/17/2019 Symfony Components Ipc 2009
95/108
How to use Symfony Components
within a Zend Framework project?
B O N U S
Symfony components in a ZF project
• Symfony Components can make your ZF application better
-
8/17/2019 Symfony Components Ipc 2009
96/108
Symfony Components can make your ZF application better
–
More congurable: symfony YAML– More exible: symfony Event Dispatcher
–
Faster: symfony Dependency Injection
– More secure: symfony Output Escaper
•
… and more fun of course
Symfony Components in a ZF project
• The examples in this presentation are just to get you started faster
-
8/17/2019 Symfony Components Ipc 2009
97/108
The examples in this presentation are just to get you started faster
• So, be creative with them. They open all kind of opportunities for your
next Zend Framework project
•
And please, give me feedback, and tell me what you do with theSymfony Components
-
8/17/2019 Symfony Components Ipc 2009
98/108
Symfony Components
Event Dispatcher
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{public function run(){require once '/path/to/sfEventDispatcher php';
-
8/17/2019 Symfony Components Ipc 2009
99/108
require_once /path/to/sfEventDispatcher.php ;
$dispatcher = new sfEventDispatcher();
$event = new sfEvent(null, 'bootstrap.prerun');$dispatcher->notify($event);
parent::run();
$event = new sfevent(null, 'bootstrap.postrun');$dispatcher->notify($event);
}
}
-
8/17/2019 Symfony Components Ipc 2009
100/108
Symfony Components
Dependency Injection
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{public function getContainer(){if (null === $this->_container){
$this->setContainer($this-> initContainer());
-
8/17/2019 Symfony Components Ipc 2009
101/108
Example from Ben Eberlei (he rocks!): http://www.whitewashing.de/blog/articles/118
$ ($ _ ());}
return $this->_container;}
protected function _initContainer(){require_once '/path/to/sfServiceContainerAutoloader.php';
sfServiceContainerAutoloader::register();
$container = new sfServiceContainerBuilder();
$loader = new sfServiceContainerLoaderFileXml($container);$loader->load(dirname(__FILE__).'/configs/resources.xml');
return $container;}}
foobarZend Mail
-
8/17/2019 Symfony Components Ipc 2009
102/108
Zend_Mail
smtp.gmail.com
login%mailer.username%
%mailer.password%ssl465
parameters:mailer.username: foomailer.password: barmailer.class: Zend_Mail
i
-
8/17/2019 Symfony Components Ipc 2009
103/108
services:
mail.transport:class: Zend_Mail_Transport_Smtparguments: [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]]
class GuestbookController extends Zend_Controller_Action{
public function indexAction(){$ tb k D f lt M d l G tb k()
-
8/17/2019 Symfony Components Ipc 2009
104/108
$guestbook = new Default_Model_Guestbook();
$this->view->entries = $guestbook->fetchAll();
$container = $this->getInvokeArg('bootstrap')->getContainer();$mailer = $container->mailer;
}}
Dependency Injection Container
•
By default in ZF, new resources can be added to the container but cannot
-
8/17/2019 Symfony Components Ipc 2009
105/108
be lazy-loaded–
All resources used by Zend_Application are loaded on every request
•
By using symfony Service Container, the resources are lazy-loaded
– Instances and their dependencies are created the rst time you get them
•
Interesting for resources like DB
-
8/17/2019 Symfony Components Ipc 2009
106/108
Symfony Components
Output Escaper
require_once '/path/to/sfOutputEscaperAutoloader.php';
sfOutputEscaperAutoloader::register();
class My_View extends Zend_View{
public function set($key $val)
-
8/17/2019 Symfony Components Ipc 2009
107/108
public function __set($key, $val)
{ if ('_' === substr($key, 0, 1)){// throw an exception
}
$this->$key = sfOutputEscaper::escape(array($this, 'escape'), $val);}
}
-
8/17/2019 Symfony Components Ipc 2009
108/108
p p