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
3
1k
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
Content editing in Symfony
tgalopin
3
510
Symfony UX: a new JavaScript ecosystem for Symfony
tgalopin
4
3.6k
[SymfonyLive Paris 2020] Pragmatic frontend for Symfony developers
tgalopin
2
940
SymfonyInsight: How to setup quality processes in Symfony apps
tgalopin
2
340
Symfony 5 - AFUP Day Lille 2020
tgalopin
1
700
Pragmatic frontend for Symfony developers
tgalopin
2
880
Demystifying React for Symfony developers
tgalopin
3
560
Symfony 5
tgalopin
1
590
Demystifying React, Redux, JSX and Webpack
tgalopin
0
700
Other Decks in Programming
See All in Programming
Git Rebase
bkuhlmann
10
1.2k
TypeScript 4.9のas const satisfiesが便利
tonkotsuboy_com
9
2.3k
TSX First な Zero-Runtime SSG potato4d/dodai とその仕組み / owned static site generator #kyotojs
potato4d
0
330
Azure Functionsをサクッと開発、サクッとデプロイ/vscodeconf2023-baba
nina01
1
340
良質な技術記事を量産する秘訣 / #MeetsPro
jnchito
11
3.6k
Milestoner
bkuhlmann
1
250
Circuit⚡
monaapk
0
200
T3 Stack and TypeScript ecosystem
quramy
3
770
Ruby Pattern Matching
bkuhlmann
0
610
Swift Observation
shiz
4
290
Enumを自動で網羅的にテストしてみた
estie
0
1.3k
Prácticas de Seguridad en Kubernetes
pablokbs
0
130
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
396
63k
The Invisible Side of Design
smashingmag
292
48k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
22
1.7k
Designing with Data
zakiwarfel
91
4.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
24
4.6k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
31
20k
The Power of CSS Pseudo Elements
geoffreycrofte
52
4.3k
Design by the Numbers
sachag
271
18k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
318
19k
The Cult of Friendly URLs
andyhume
69
5.1k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
2
400
What the flash - Photography Introduction
edds
64
10k
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 ▪
[email protected]