$30 off During Our Annual Pro Sale. View Details »

Symfony 4 - Dependency Injection reloaded - SfPot Lille 2018

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. Code faster with

  2. Spread the Word @nicolasgrekas

  3. Symfony 5? By the end of November 2019 Symfony4.0 requiresPHP

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

    Backward Compatibility Promise • @trigger_error(…, E_USER_DEPRECATED);
  5. What is a Symfony Application? (let’s make something crazy with

    the components) @nicolasgrekas
  6. A Symfony app Uses a compiled container @nicolasgrekas

  7. A Symfony app Uses (offline) cache warmers @nicolasgrekas

  8. A Symfony app Can be configured dynamically via env vars

    @nicolasgrekas
  9. @nicolasgrekas A Symfony app Fits the containers’ world

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

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

    require twig annotations doctrine > composer require debug profiler server
  12. $ composer require symfony/console github.com packagist.org symfony.sh

  13. Symfony Flex Automates your (default) configuration @nicolasgrekas

  14. git add -p git commit @nicolasgrekas

  15. @nicolasgrekas Symfony 4 spirit Automate your config By defining your

    own conventions and the exceptions to the rules
  16. And provide you with great error messages @nicolasgrekas

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

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

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

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

    ../src Every class extending Command should be tagged with « console.command », etc. @nicolasgrekas
  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
  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)
  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
  24. What if I want my own (extra) conventions? @nicolasgrekas

  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
  26. @nicolasgrekas Custom autowiring (re)configuration? services: #... App\MyFooInterface: alias: App\MyFoo Auto-aliased

    if singly implemented!
  27. @nicolasgrekas Partial wiring? Named args to the rescue services: #...

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

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

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

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

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

    from a file?
  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
  34. #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
  35. Symfony 4 wish Keep you at coding as long as

    possibe @nicolasgrekas
  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
  37. Target experience? It just works® Lower the entry bar, enhance

    efficiency of pros
  38. None