• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Desymfony2013.gonzalo123
 

Desymfony2013.gonzalo123

on

  • 5,052 views

Mi charla sobre Dependencias y Contenedores de inyección en la conferencia deSymfony 2013

Mi charla sobre Dependencias y Contenedores de inyección en la conferencia deSymfony 2013

Statistics

Views

Total Views
5,052
Views on SlideShare
920
Embed Views
4,132

Actions

Likes
2
Downloads
39
Comments
0

23 Embeds 4,132

http://gonzalo123.com 3263
http://desymfony.com 351
http://librosweb.es 277
https://twitter.com 129
http://feeds.feedburner.com 38
http://www.desymfony.com 31
http://cloud.feedly.com 8
http://digg.com 7
http://www.newsblur.com 5
http://translate.googleusercontent.com 3
http://desymfony.local 3
http://newsblur.com 2
http://www.365dailyjournal.com 2
https://www.rebelmouse.com 2
http://www.feedspot.com 2
https://www.linkedin.com 2
http://cc.bingj.com 1
http://feedreader.com 1
http://www.linkedin.com 1
http://gonzalo123.com&_=1380786669115 HTTP 1
http://localhost 1
http://librosweb.dev 1
http://feedly.com 1
More...

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Desymfony2013.gonzalo123 Desymfony2013.gonzalo123 Presentation Transcript

    • PHP (83%)JavaScript (10%)Python (1%)VimL (1%)C# (1%)Shell (1%)@gonzalo123http://gonzalo123.comhttps://github.com/gonzalo123http://www.linkedin.com/in/gonzaloayusoHello my name is ...Gonzalo Ayuso(according to github)Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • http://gonzalo123.comhttp://katayunos.comYou can find me at ...Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • Why should we useDependency Injection?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • S Single responsibilityO Open/closed principleL Liskov substitutionI Interface segregationD Dependency inversion principleS.O.L.I.D.Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • IoC and TDDDependency injection is a requirementif you want to write testable code.Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOrder();$pdfGenerator = new PDFGenerator();$pdf = $pdfGenerator->getPDF($orderId);$mailer = new Mailer(mailCredentials...);$mailer->sendPdfToUSer($pdf, $user);}}$order = new Order();$order->generateOrderForUser(Gonzalo);One example. It works but ...Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOrder();$pdfGenerator = new PDFGenerator();$pdf = $pdfGenerator->getPDF($orderId);$mailer = new Mailer(mailCredentials...);$mailer->sendPdfToUSer($pdf, $user);}}$order = new Order();$order->generateOrderForUser(Gonzalo);Be decoupled my friendDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOrder();$pdfGenerator = new PDFGenerator();$pdf = $pdfGenerator->getPDF($orderId);$mailer = new Mailer(mailCredentials...);$mailer->sendPdfToUSer($pdf, $user);}}$order = new Order();$order->generateOrderForUser(Gonzalo);Magic strings are evilDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOrder();$pdfGenerator = new PDFGenerator();$pdf = $pdfGenerator->getPDF($orderId);$mailer = new Mailer(mailCredentials...);$mailer->sendPdfToUSer($pdf, $user);}}$order = new Order();$order->generateOrderForUser(Gonzalo);Can we change anything?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOrder();$pdfGenerator = new PDFGenerator();$pdf = $pdfGenerator->getPDF($orderId);// remember to remove this comment when we deploy//$mailer = new Mailer(mailCredentials...);//$mailer->sendPdfToUSer($pdf, $user);}}$order = new Order();$order->generateOrderForUser(Gonzalo);Can we change anything?Open/Close PrincipleDesymfony 2013. Madrid @gonzalo123 http://gonzalo123.com
    • class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf, Mailer$mailer){$this->backend = $backend;$this->pdf = $pdf;$this->mailer = $mailer;}public function generateOrderForUser($user){$orderId = $this->backend->createOrder();$pdf = $this->pdfGenerator->getPDF($orderId);$this->mailer->sendPdfToUSer($pdf, $user);}}$backend = new BackendOrder(dsn:...);$pdfGenerator = new PDFGenerator();$mailer = new Mailer(mailCredentials...);$order = new Order($backend, $pdfGenerator, $mailer);$order->generateOrderForUser(Gonzalo);Refactor -> DIDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf, Mailer$mailer){$this->backend = $backend;$this->pdf = $pdf;$this->mailer = $mailer;}public function generateOrderForUser($user){$orderId = $this->backend->createOrder();$pdf = $this->pdfGenerator->getPDF($orderId);$this->mailer->sendPdfToUSer($pdf, $user);}}$backend = new BackendOrder(dsn:...);$pdfGenerator = new PDFGenerator();$mailer = new Mailer(mailCredentials...);$order = new Order($backend, $pdfGenerator, $mailer);$order->generateOrderForUser(Gonzalo);Refactor -> DIAndthatsall folks!Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • The Hollywood principle"Dont call us,well call you."Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf,Mailer $mailer){$this->backend = $backend;$this->pdf = $pdf;$this->mailer = $mailer;}public function generateOrderForUser($user){$orderId = $this->backend->createOrder();$pdf = $this->pdfGenerator->getPDF($orderId);$this->mailer->sendPdfToUSer($pdf, $user);}}$backend = new BackendOrder(dsn:...);$pdfGenerator = new PDFGenerator();$mailer = new Mailer(mailCredentials...);$order = new Order($backend, $pdfGenerator, $mailer);$order->generateOrderForUser(Gonzalo);Where is the problem now?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf,Mailer $mailer){$this->backend = $backend;$this->pdf = $pdf;$this->mailer = $mailer;}public function generateOrderForUser($user){$orderId = $this->backend->createOrder();$pdf = $this->pdfGenerator->getPDF($orderId);$this->mailer->sendPdfToUSer($pdf, $user);}}$backend = new BackendOrder(dsn:...);$pdfGenerator = new PDFGenerator();$mailer = new Mailer(mailCredentials...);$order = new Order($backend, $pdfGenerator, $mailer);$order->generateOrderForUser(Gonzalo);What can we do?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • Dependency Injection Containers (DIC)to the rescue!Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • Dependency Injection Containers (DIC)in PHP? Are u crazy?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • Yes! DIC in PHP:Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • <?phpclass Container {protected $s=array();function __set($k, $c) { $this->s[$k]=$c; }function __get($k) { return $this->s[$k]($this); }}TwitteA Dependency Injection Container in a TweetDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • $c = new Container();$c->backend_driver = function () { return dsn:...; };$c->backend = function ($c) {return new BackendOrder($c->backend_driver);};$c->pdfGenerator = function ($c) {return new PDFGenerator();};$c->mailer_credentials = function () { returnmailCredentials...; };$c->mailer = function ($c) {return new Mailer($c->mailer_credentials);};$c->order = function ($c) {return new Order($c->backend, $c->pdfGenerator, $c->mailer);};$order = $c->order;$order->generateOrderForUser(Gonzalo);TwitteA Dependency Injection Container in a TweetDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • $c = new Pimple();$c[backend_driver] = mysql:...;$c[backend] = function ($c) {return new BackendOrder($c[backend_driver]);};$c[pdfGenerator] = function ($c) {return new PDFGenerator();};$c[mailer_credentials] = mailCredentials...;$c[mailer] = function ($c) {return new Mailer($c[mailer_credentials]);};$c[order] = function ($c) {return new Order($c[backend], $c[pdfGenerator], $c[mailer]);};$order = $c[order];$order->generateOrderForUser(Gonzalo);PimpleA Simple Dependency Injection Container for PHP 5.3Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • class MyContainer extends Pimple{public function __construct(){$this[backend_driver] = dsn:...;$c[backend] = function ($this) {return new BackendOrder($this[backend_driver]);};$c[pdfGenerator] = function ($c) {return new PDFGenerator();};$this[mailer_credentials] = mailCredentials...;$this[mailer] = function ($this) {return new Mailer($this[mailer_credentials]);};$this[order] = function ($this) {return new Order($this[backend], $this[pdfGenerator], $this[mailer]);};}}$c = new MyContainer();$order = $c[order];$order->generateOrderForUser(Gonzalo);PimpleA Simple Dependency Injection Container for PHP 5.3Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • composer.json{"require": {"symfony/dependency-injection": "v2.3.0","symfony/yaml": "v2.3.0","symfony/config": "v2.3.0"}}Symfony DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • services.ymlparameters:backend.driver: dsn:...mailer.credentials: mailCredentials...services:backend:class: BackendOrderarguments: [%backend.driver%]pdfGenerator:class: PDFGeneratormailer:class: Mailerarguments: [%mailer.credentials%]order:class: Orderarguments: [@backend, @pdfGenerator,@mailer]Symfony DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • index.phpuse SymfonyComponentDependencyInjectionContainerBuilder;use SymfonyComponentConfigFileLocator;use SymfonyComponentDependencyInjectionLoaderYamlFileLoader;$container = new ContainerBuilder();$loader = new YamlFileLoader($container, new FileLocator(__DIR__));$loader->load(services.yml);$order = $container->get(order);$order->generateOrderForUser(Gonzalo);Symfony DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • ● Symfony2 (SymfonyDependencyInjection)● Zend Framework2 (ZendDi)● Silex (Pimple)● Laravel (IlluminateContainer)Modern frameworks and DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • Usar contenedores o no usarlosUse an IoC container wisely, notpromiscuouslyTo use or not to use DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • Lets code!Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • services.ymlparameters:twig.cache: /path/to/cache/pathtwig.auto_reload: truedb.dsn: sqlite::memorydb.username: usernamedb.password: passwordservices:database:class: PDOarguments: [%db.dsn%, %db.username%, %db.password%]Scaling our services.ymlDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • services.ymlimports:- resource: twig.yml- resource: database.ymltwig.ymlparameters:twig.cache: /path/to/cache/pathtwig.auto_reload: truedatabase.ymlparameters:db.dsn: sqlite::memorydb.username: usernamedb.password: passwordservices:database:class: PDOarguments: [%db.dsn%, %db.username%, %db.password%, ]Scaling our services.ymlDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • require_once __DIR__./vendor/autoload.php;$app = new SilexApplication();$app->get(/hello/{name}, function($name) {return Hello .strtoupper($name);});$app->run();Silex exampleDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • // services.ymlservices:capitalizer:class: Capitalizeruse SymfonyComponentDependencyInjectionContainerBuilder;use SymfonyComponentConfigFileLocator;use SymfonyComponentDependencyInjectionLoaderYamlFileLoader;$container = new ContainerBuilder();$loader = new YamlFileLoader($container, new FileLocator(__DIR__));$loader->load(services.yml);$app = new SilexApplication();$app[container] = $container;$app->get(/hello/{name}, function($name) use($app) {$capitalizer = $app[container]->get(capitalizer);return Hello . $capitalizer->toUpper($name);});$app->run();Silex example with DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • Almost the end ...● Be decoupled my friend● S.O.L.I.D.● DI is good● DIC are tools to help us.● PHP has different DICs● Symfony DIC is veryeasy to implement,configurable and easyto extendDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • Thank you! - ¡Gracias! - ¡Eskerrik asko!Your turn. Questions?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
    • http://gonzalo123.comgonzalo123http://joind.in/talk/view/8843