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

Symfony 4 - Dependency Injection reloaded - SfP...

Nicolas Grekas
February 06, 2018

Symfony 4 - Dependency Injection reloaded - SfPot Lille 2018

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

February 06, 2018
Tweet

More Decks by Nicolas Grekas

Other Decks in Technology

Transcript

  1. The Symfony Core Team Wish! • Semantic Versioning • The

    Backward Compatibility Promise • @trigger_error(…, E_USER_DEPRECATED);
  2. > composer create-project symfony/skeleton demo > cd demo > composer

    require console twig annotations > composer require debug profiler server
  3. > composer create-project symfony/skeleton demo > cd demo > composer

    require twig annotations doctrine > composer require debug profiler server
  4. @nicolasgrekas Symfony 4 spirit Automate your config By defining your

    own conventions and the exceptions to the rules
  5. services: _defaults: autowire: true autoconfigure: true public: false App\: resource:

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

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

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

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

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

    %kernel.locale% App\: resource: ../src @nicolasgrekas Custom binding for a named argument?
  15. 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
  16. DI v4 3/3 - Inject services as arguments into actions

    - ServiceSubscriberInterface (<3 for service locators) - PHP configuration - @required - Kernel implements CompilerPassInterface - Resettable services @nicolasgrekas