Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Plongée dans l'injection de dépendances de Symfony
Titouan Galopin
April 27, 2019
Programming
2
950
Plongée dans l'injection de dépendances de Symfony
Titouan Galopin
April 27, 2019
Tweet
Share
More Decks by Titouan Galopin
See All by Titouan Galopin
Symfony UX: a new JavaScript ecosystem for Symfony
tgalopin
3
3.4k
[SymfonyLive Paris 2020] Pragmatic frontend for Symfony developers
tgalopin
1
860
SymfonyInsight: How to setup quality processes in Symfony apps
tgalopin
2
330
Symfony 5 - AFUP Day Lille 2020
tgalopin
1
670
Pragmatic frontend for Symfony developers
tgalopin
2
840
Demystifying React for Symfony developers
tgalopin
3
490
Symfony 5
tgalopin
1
560
Demystifying React, Redux, JSX and Webpack
tgalopin
0
700
Développer un client riche React dans un projet Symfony 4
tgalopin
2
620
Other Decks in Programming
See All in Programming
Untangling Coroutine Testing (Android Makers 2022)
zsmb
0
400
How to get satisfaction from ungrateful work: A journey into updating Kotlin
syrinet
0
120
Kotlin 最新動向2022 #tfcon #techfeed
ntaro
1
840
Microsoft Teams の 会議アプリ開発のはじめかた / How to start Microsoft Teams app development
karamem0
0
1.4k
2022 Android Training
mixi_engineers
1
630
Enterprise Angular: Frontend Moduliths with Nx and Standalone Components @jax2022
manfredsteyer
PRO
0
290
Reinventing the wheel ... as a service
mariofusco
3
220
Named Document って何?
harunakano
0
250
Modern Web Apps with Spring Boot, Angular & TypeScript
toedter
12
14k
Blazor WebAssembly – Dynamische Formulare und Inhalte in Aktion
patrickjahr
0
150
mrubyを1300円のボードで動かそう
yuuu
0
180
【PHPerKaigi2022】Mongo に溜まった約1.6億件の記事データを BigQuery へ …
userkazun
0
100
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
181
15k
Designing for Performance
lara
596
63k
A Modern Web Designer's Workflow
chriscoyier
689
180k
Git: the NoSQL Database
bkeepers
PRO
415
59k
Ruby is Unlike a Banana
tanoku
91
9.2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
224
49k
GitHub's CSS Performance
jonrohan
1020
410k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
7
1k
YesSQL, Process and Tooling at Scale
rocio
157
12k
Practical Orchestrator
shlominoach
178
8.6k
Typedesign – Prime Four
hannesfritz
33
1.3k
Scaling GitHub
holman
451
140k
Transcript
Plongée dans l'injection de dépendances de Symfony Avril 2019 SymfonyLive
Tunis
Titouan Galopin @tgalopin 2 insight.symfony.com
3 Une question m’obnubile …
4 Pourquoi l’architecture logicielle est-elle importante ?
5 Qu’est-ce qui différencie du code bien conçu et du
code mal conçu ?
6 La gestion du changement
7 Une architecture logicielle de qualité, c’est une architecture qui
accompagne le changement
8 Changement ... Dans le temps Dans l’environnement
9 Comment concevoir une architecture qui accompagne le changement ?
10 Créer des classes qui se partagent les responsabilités
11 Elles interagissent pour constituer l’application
12 Elles sont modifiables indépendamment les unes des autres
13 Mailer Persister RegistrationManager Swiftmailer Twig Doctrine
14 Mailer Persister RegistrationManager Swiftmailer Twig Doctrine
15 Mailer Persister RegistrationManager Swiftmailer Twig Doctrine Contrats (= Interfaces)
16 interface PersisterInterface { /* ... */ } interface MailerInterface
{ /* ... */ } class RegistrationManager { private $persister; private $mailer; public function __construct( PersisterInterface $persister, MailerInterface $mailer ) { $this->persister = $persister; $this->mailer = $mailer; } public function register() {/* ... */} }
17 C’est l’injection de dépendances
18 Encapsuler des objets dans d’autres pour créer des comportements
complexes
19 Faciliter le changement par l’utilisation de contrats
20 Mais créer un objet devient plus compliqué
21 $manager = new RegistrationManager( new Persister( new Doctrine(/* ...
*/) ), new Mailer( new Twig(/* ... */), new SwiftMailer(/* ... */) ) );
22 Symfony DependencyInjection
23 Objectif : permettre la création aisé d’instances de classes
24 Configurer ce graphe de dépendances entre classes
25 interface PersisterInterface { /* ... */ } interface MailerInterface
{ /* ... */ } class RegistrationManager { private $persister; private $mailer; public function __construct( PersisterInterface $persister, MailerInterface $mailer ) { $this->persister = $persister; $this->mailer = $mailer; } public function register() {/* ... */} }
26 $manager = new RegistrationManager( new Persister( new Doctrine(/* ...
*/) ), new Mailer( new Twig(/* ... */), new SwiftMailer(/* ... */) ) ); Objectif
27 Fonctionnement général 1 Configuration 2 Compilation 3 Utilisation
28 1. Configuration services.yaml
29 Mailer Persister RegistrationManager Swiftmailer Twig Doctrine
30 services: # ... App\Persister: arguments: ['@doctrine'] App\Mailer: arguments: ['@twig',
'@swiftmailer] App\RegistrationManager: arguments: ['@App\Persister', '@App\Mailer']
31 Depuis Symfony 4 : Autodiscovery Autowiring
32 services: _defaults: autowire: true App\: resource: '../src/*'
33 services: _defaults: autowire: true App\: resource: '../src/*' Autodiscovery :
importe toutes les classes dans src/ en tant que noeuds du graphe
34 services: _defaults: autowire: true App\: resource: '../src/*' Autodiscovery :
importe toutes les classes dans src/ en tant que noeuds du graphe Autowiring : créé les liens entre les noeuds du graphe automatiquement
35 Mailer Persister RegistrationManager Swiftmailer Twig Doctrine Autodiscovery Autowiring
36 2. Compilation
37 On a maintenant un graphe de dépendances
38 On sait donc créer une instance de classe
39 Mais passer par le graphe directement, c’est lent
40 Compiler le graphe de dépendances = Le transformer en
PHP
41
42 Nouvelle possibilité : modifier le graphe entre la configuration
et l’utilisation
43 CompilerPass = Modification du graphe au moment de la
compilation
44 3. Utilisation
45 Transparente !
46 Les points d’entrée de votre application (contrôleurs, commandes, …)
font partie du graphe !
47 class RegistrationController extends AbstractController { /** * @Route("/register", name="register")
*/ public function register(RegistrationManager $manager) { // ... $manager->register($user); // ... } }
48 Mailer Persister RegistrationManager Swiftmailer Twig Doctrine RegistrationController
49 Beaucoup d’autres fonctionnalités Tags, bindings, named autowiring aliases, autoconfiguration,
factories, decorators, ...
50 Contribuez ! symfony.com/support => Slack Pinguez Nicolas Grekas
51 Merci ! Venez me voir au stand SymfonyInsight !
▪ Twitter : @titouangalopin ▪ Github : @tgalopin ▪ titouan.galopin@symfony.com