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

Dependency Injection Component v4.0 - SymfonyCon Cluj 2017

Dependency Injection Component v4.0 - SymfonyCon Cluj 2017

The Dependency Injection component is one of the central pieces of any Symfony applications since version 2.0. Starting this winter, it has gained many new features that were needed to build the new Symfony 4 experience around Flex. While used extensively in core, can you leverage them in your own apps? Can you create a lazy iterator in Yaml? A scoped service locator? Configure auto-configuration? During this talk, I'll tell you about the new tags, interfaces or annotations that allow building powerful services. The goal: making your apps always more expressive, thus maintainable.

6baa34bc1e5c347b1003f6abe8691de1?s=128

Nicolas Grekas

November 17, 2017
Tweet

Transcript

  1. Dependency Injection v4

  2. @nicolasgrekas SensioLabs

  3. Symfony 4? By the end of November 2017 Will requirePHP

    7.1 You canuse v3.4 meanwhile @nicolasgrekas
  4. The Symfony Core Team Wish! • Semantic Versioning • The

    Backward Compatibility Promise • @trigger_error(…, E_USER_DEPRECATED); • etc. @nicolasgrekas
  5. Symfony 4 spirit Automate your config By defining your own

    conventions and the exceptions to the rules @nicolasgrekas
  6. services: _defaults: autowire: true autoconfigure: true public: false App\: resource:

    ../src config/services.yaml @nicolasgrekas
  7. services: _defaults: autowire: true autoconfigure: true public: false App\: resource:

    ../src Every class in src/ is potentially a service @nicolasgrekas
  8. services: _defaults: autowire: true autoconfigure: true public: false App\: resource:

    ../src But every unused service should be cleaned up @nicolasgrekas
  9. services: _defaults: autowire: true autoconfigure: true public: false App\: resource:

    ../src Every class extending Command should be tagged with « console.command », etc. @nicolasgrekas
  10. services: _defaults: autowire: true autoconfigure: true public: false App\: resource:

    ../src Every service needing a LoggerInterface should be provided with the default logger, etc. @nicolasgrekas
  11. services: _defaults: autowire: true autoconfigure: true public: false App\: resource:

    ../src Every service needing a LoggerInterface should be provided with the default logger, etc. @nicolasgrekas The default logger is the service that is named LoggerInterface (it used to be the one that implements it, but that was a foot gun)
  12. DI v4 1/3 - FQCN service ids (case sensitive ids

    and parameters) - Private all the things (applies to Symfony core+bundles!) - Autoconfiguration by interfaces - Autowiring by service identifier @nicolasgrekas
  13. What if I want my own (extra) conventions? @nicolasgrekas

  14. services: _defaults: autowire: true autoconfigure: true public: false App\: resource:

    ../src config/services.yaml @nicolasgrekas Defaults apply to all services in this very file only
  15. @nicolasgrekas Custom autowiring (re)configuration? services: #... App\MyFooInterface: alias: App\MyFoo

  16. @nicolasgrekas Partial wiring? Named args to the rescue services: #...

    App\Foo: arguments: $defaultLocale: %kernel.locale%
  17. @nicolasgrekas Custom autoconfiguration? services: _defaults: autowire: true autoconfigure: true public:

    false _instanceof: App\MyFooInterface: tags: [foo] App\: resource: ../src
  18. services: _defaults: autowire: true autoconfigure: true public: false bind: $defaultLocale:

    %kernel.locale% App\: resource: ../src @nicolasgrekas Custom binding for a named argument?
  19. services: #... App\FooConsomer: arguments: - !tagged foo @nicolasgrekas Inject all

    services with the « foo » tag?
  20. services: #... _instanceof: App\MyFooInterface: tags: [foo] App\FooConsomer: [!tagged foo] @nicolasgrekas

    Inject all services with the « foo » tag?
  21. services: #... App\FooConsomer: arguments: - '%env(file:API_KEY_FILE)%' @nicolasgrekas Read a secret

    from a file?
  22. DI v4 2/3 - File-local defaults - By-name and by-type

    arguments - By-name and by-type bindings - !tagged lazy-collections - Allow processing env vars @nicolasgrekas
  23. What about performances? @nicolasgrekas

  24. Hey, Symfony uses a compiled container! @nicolasgrekas

  25. Lazyness everywhere • On-demand service instantiation • Service iterators •

    On-demand service factories • Service locators @nicolasgrekas
  26. What about performance? • Short lived processes vs • Long

    lived processes @nicolasgrekas
  27. Long lived kernel challenges • Stateless services done for core,

    fix your services • Resettable services <tag name="kernel.reset" method="reset" /> • Resilient services community work pending @nicolasgrekas
  28. #app/config/config.yaml parameters: container.autowiring.strict_mode: true container.dumper.inline_class_loader: true @nicolasgrekas Pro-tip when migrating

    to 3.4 Opt-in for Symfony 4 behavior
  29. Symfony 4 wish Keep you at coding as long as

    possibe @nicolasgrekas
  30. DI v4 3/3 - Inject services as arguments into actions

    - ServiceSubscriberInterface (<3 for service locators) - PHP configuration - @required - Kernel implements CompilerPassInterface - Resettable services @nicolasgrekas
  31. Symfony 4 Summary It scales with you and your needs,

    up to the sky • Built for containers • Scales to any app (RIP Silex) • Automates all the things • Lower entry bar • Higher dev efficiency • Performance ++ @nicolasgrekas
  32. Enjoy Thank you @nicolasgrekas joind.in/22877