Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency Injection in Symfony2 and Drupal

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →
Peter
August 24, 2013

Dependency Injection in Symfony2 and Drupal

My presentation for Drupal Camp Moscow 2013

Peter

August 24, 2013

Transcript

39 slides
  1. About me • Web-developer since 2006 • EvercodeLab co-founder and

    CTO • Ruby Php Python, over 9000 frameworks • Genius Billionaire Playboy Philanthropist
  2. Bad policeman class Notifier { private $mailer; public function __construct()

    { $this->mailer = new Mailer(); } public function notify() { ... $this->mailer->send($from, $to, $msg); ... } }
  3. Good policeman class Notifier { private $mailer; public function __construct(Mailer

    $m) { $this->mailer = $m; } public function notify() { ... $this->mailer->send($from, $to, $msg); ... } } $mailer = new Mailer(); $notifier = new Notifier($mailer);
  4. Superman class Notifier { private $mailer; public function __construct(MailerInterface $m)

    { $this->mailer = $m; } public function notify() { ... $this->mailer->send($from, $to, $msg); ... } } $mailer = new Mailer(); $notifier = new Notifier($mailer); http://www.flickr.com/photos/gkyc/with/43902217/
  5. Property injection class Notifier { public $mailer; ... } $mailer

    = new Mailer(); $notifier = new Notifier($mailer); $notifier->mailer = $mailer;
  6. Setter injection class Notifier { private $mailer; public function setMailer($mailer)

    { $this->mailer = $mailer; } } $mailer = new Mailer(); $notifier = new Notifier($mailer); $notifier->setMailer($mailer);
  7. «Do not instantiate the dependencies explicitly in your class. Instead,

    declaratively express dependencies in your class definition.» MSDN
  8. What does user do twig.extension.text: class: Twig_Extensions_Extension_Text tags: - {

    name: twig.extension } app.user.validator.unique: class: App\UserBundle\Validator\UniqueValidator arguments: [@app.manager.orm_user_manager] tags: - { name: validator.constraint_validator, alias:app.user.validator.unique } app.listener.router: class: App\DefaultBundle\Listener\RouterListener tags: - { name: kernel.event_listener, event: kernel.request } arguments: [@router]
  9. What does component do • Construct objects graph • Instantiatiate

    services • Application logic and infrastructure logic are kept separately • Cache dependencies
  10. To be or not to be • Database • Cache

    • Mailer • Logger • Model • Controller(?)
  11. Scopes app.form.handler.registration: class: App\UserBundle\Form\Handler\FormHandler arguments: [@fos_user.registration.form, @request] scope: request app.listener.location:

    class: App\DefaultBundle\Listener\LocationListener arguments: [@request, @session, @security.context, @router] scope: request
  12. • Been loaded service cannot be redefined • Low coupling

    • Type Hinting • Localization for debug and testing • Caching
  13. HttpKernel /** * Boots the current kernel. * @api */

    public function boot() { // init container $this->initializeContainer(); foreach ($this->getBundles() as $bundle) { $bundle->setContainer($this->container); $bundle->boot(); } $this->booted = true; }

More Decks by Peter

Other Decks in Programming