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

Plongée dans l'injection de dépendances de Symfony

Plongée dans l'injection de dépendances de Symfony

Titouan Galopin

April 27, 2019
Tweet

More Decks by Titouan Galopin

Other Decks in Programming

Transcript

  1. 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() {/* ... */} }
  2. 21 $manager = new RegistrationManager( new Persister( new Doctrine(/* ...

    */) ), new Mailer( new Twig(/* ... */), new SwiftMailer(/* ... */) ) );
  3. 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() {/* ... */} }
  4. 26 $manager = new RegistrationManager( new Persister( new Doctrine(/* ...

    */) ), new Mailer( new Twig(/* ... */), new SwiftMailer(/* ... */) ) ); Objectif
  5. 30 services: # ... App\Persister: arguments: ['@doctrine'] App\Mailer: arguments: ['@twig',

    '@swiftmailer] App\RegistrationManager: arguments: ['@App\Persister', '@App\Mailer']
  6. 33 services: _defaults: autowire: true App\: resource: '../src/*' Autodiscovery :

    importe toutes les classes dans src/ en tant que noeuds du graphe
  7. 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
  8. 41

  9. 47 class RegistrationController extends AbstractController { /** * @Route("/register", name="register")

    */ public function register(RegistrationManager $manager) { // ... $manager->register($user); // ... } }
  10. 51 Merci ! Venez me voir au stand SymfonyInsight !

    ▪ Twitter : @titouangalopin ▪ Github : @tgalopin ▪ [email protected]