Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Dependency Injection in Symfony2 and Drupal
Search
Peter
August 24, 2013
Programming
95
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Dependency Injection in Symfony2 and Drupal
My presentation for Drupal Camp Moscow 2013
Peter
August 24, 2013
More Decks by Peter
See All by Peter
Ruby microframeworks
toothfairy
3
870
Inversion of control in Symfony 2
toothfairy
3
1.3k
Other Decks in Programming
See All in Programming
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
220
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
1k
contenteditable と日本語入力に向き合う
colorful12
0
200
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
180
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
350
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
110
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
180
カツオ、ご期待ください
suneo3476
0
190
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
390
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
180
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
Featured
See All Featured
Paper Plane (Part 1)
katiecoart
PRO
2
12k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
260
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
330
How to Talk to Developers About Accessibility
jct
2
580
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
2
6.3k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
2
2k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
From π to Pie charts
rasagy
1
400
YesSQL, Process and Tooling at Scale
rocio
174
15k
The Curse of the Amulet
leimatthew05
3
16k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Transcript
Dependency Injection component. What where why? http://www.tv.net.ua/news/na_teleekrane/1050664487-zvezdnye-odessity-v-yetu-pyatnicu-proveryat-svoj-intellekt-v-programme-chto-gde-kogda-zvezdnye-vojny.html Petr Sergeev, EvercodeLab
About me • Web-developer since 2006 • EvercodeLab co-founder and
CTO • Ruby Php Python, over 9000 frameworks • Genius Billionaire Playboy Philanthropist
• Basics • Dependency injection • In Symfony2 • In
Drupal 8
Basics about injecting, good code and other things
Good code is good • Clean • Reusable • Clutter-free
• Testable
Bad policeman class Notifier { private $mailer; public function __construct()
{ $this->mailer = new Mailer(); } public function notify() { ... $this->mailer->send($from, $to, $msg); ... } }
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);
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/
Injection types • Property injection • Contructor injection • Setter
injection
Property injection class Notifier { public $mailer; ... } $mailer
= new Mailer(); $notifier = new Notifier($mailer); $notifier->mailer = $mailer;
Constructor injection class Notifier { private $mailer; public function __construct(Mailer
$m) { $this->mailer = $m; } }
Setter injection class Notifier { private $mailer; public function setMailer($mailer)
{ $this->mailer = $mailer; } } $mailer = new Mailer(); $notifier = new Notifier($mailer); $notifier->setMailer($mailer);
Dependency Injection = Inversion of control http://www.flickr.com/photos/begumf/with/7916700382/
«Do not instantiate the dependencies explicitly in your class. Instead,
declaratively express dependencies in your class definition.» MSDN
How it all goes • Manual injection • Factory •
From container/injector
Dependency Injection in action
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]
What does component do • Construct objects graph • Instantiatiate
services • Application logic and infrastructure logic are kept separately • Cache dependencies
Service Bunch of code responsible some particular global task
Separation of concerns http://www.luminescentphoto.com/blog/2011/04/27/concert-going-with-the-p7000/
To be or not to be • Database • Cache
• Mailer • Logger • Model • Controller(?)
How it works • $container->getService('some_service') • Read services graph •
Instantiate service • ... • Profit
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
Tags app.listener.accountNumber: class: App\UserBundle\Listener\AccountNumberListener calls: - [ setContainer, [ @service_container
] ] tags: - { name: doctrine.event_listener, event: postUpdate }
Pros and cons http://www.flickr.com/photos/jayashree-shankar/with/5180430923/
• Been loaded service cannot be redefined • Low coupling
• Type Hinting • Localization for debug and testing • Caching
• Blackbox • Hard to understand, huh?
None
Symfony 2
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; }
Symfony2 • Compiling • Tagging, event subscribers and listeners •
Bundles
$em = $this->container->get('doctrine.orm.entity_manager');
Drupal 8
Services available • Database • Module handler • Request object
2 ways of usage • Drupal::service(‘db’) • OOP style
What’s that all about
Sources list • Wikipedia • Drupal.org • Symfony.com • Katbailey.github.io
http://www.flickr.com/photos/walkn/with/3526522573/
Thanks @i_feya
[email protected]
Petr Sergeev