Symfony 4 - Dependency Injection reloaded - SfPot Lille 2018

6baa34bc1e5c347b1003f6abe8691de1?s=47 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.

6baa34bc1e5c347b1003f6abe8691de1?s=128

Nicolas Grekas

February 06, 2018
Tweet

Transcript

  1. 4.

    The Symfony Core Team Wish! • Semantic Versioning • The

    Backward Compatibility Promise • @trigger_error(…, E_USER_DEPRECATED);
  2. 10.

    > composer create-project symfony/skeleton demo > cd demo > composer

    require console twig annotations > composer require debug profiler server
  3. 11.

    > composer create-project symfony/skeleton demo > cd demo > composer

    require twig annotations doctrine > composer require debug profiler server
  4. 15.

    @nicolasgrekas Symfony 4 spirit Automate your config By defining your

    own conventions and the exceptions to the rules
  5. 18.

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

    ../src Every class in src/ is potentially a service @nicolasgrekas
  6. 19.

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

    ../src But every unused service should be cleaned up @nicolasgrekas
  7. 20.

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

    ../src Every class extending Command should be tagged with « console.command », etc. @nicolasgrekas
  8. 21.

    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. 22.

    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. 23.

    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. 25.

    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. 27.

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

    App\Foo: arguments: $defaultLocale: %kernel.locale%
  13. 28.

    @nicolasgrekas Custom autoconfiguration? services: _defaults: autowire: true autoconfigure: true public:

    false _instanceof: App\MyFooInterface: tags: [foo] App\: resource: ../src
  14. 29.

    services: _defaults: autowire: true autoconfigure: true public: false bind: $defaultLocale:

    %kernel.locale% App\: resource: ../src @nicolasgrekas Custom binding for a named argument?
  15. 33.

    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. 36.

    DI v4 3/3 - Inject services as arguments into actions

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