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.

Nicolas Grekas

November 17, 2017
Tweet

More Decks by Nicolas Grekas

Other Decks in Technology

Transcript

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

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

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

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

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

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

    ../src Every class extending Command should be tagged with « console.command », etc. @nicolasgrekas
  7. 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
  8. 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)
  9. 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
  10. 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
  11. @nicolasgrekas Partial wiring? Named args to the rescue services: #...

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

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

    %kernel.locale% App\: resource: ../src @nicolasgrekas Custom binding for a named argument?
  14. 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
  15. Lazyness everywhere • On-demand service instantiation • Service iterators •

    On-demand service factories • Service locators @nicolasgrekas
  16. 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
  17. DI v4 3/3 - Inject services as arguments into actions

    - ServiceSubscriberInterface (<3 for service locators) - PHP configuration - @required - Kernel implements CompilerPassInterface - Resettable services @nicolasgrekas
  18. 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