Slide 1

Slide 1 text

Dependency Injection v4

Slide 2

Slide 2 text

@nicolasgrekas SensioLabs

Slide 3

Slide 3 text

Symfony 4? By the end of November 2017 Will requirePHP 7.1 You canuse v3.4 meanwhile @nicolasgrekas

Slide 4

Slide 4 text

The Symfony Core Team Wish! • Semantic Versioning • The Backward Compatibility Promise • @trigger_error(…, E_USER_DEPRECATED); • etc. @nicolasgrekas

Slide 5

Slide 5 text

Symfony 4 spirit Automate your config By defining your own conventions and the exceptions to the rules @nicolasgrekas

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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)

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

What if I want my own (extra) conventions? @nicolasgrekas

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

@nicolasgrekas Custom autowiring (re)configuration? services: #... App\MyFooInterface: alias: App\MyFoo

Slide 16

Slide 16 text

@nicolasgrekas Partial wiring? Named args to the rescue services: #... App\Foo: arguments: $defaultLocale: %kernel.locale%

Slide 17

Slide 17 text

@nicolasgrekas Custom autoconfiguration? services: _defaults: autowire: true autoconfigure: true public: false _instanceof: App\MyFooInterface: tags: [foo] App\: resource: ../src

Slide 18

Slide 18 text

services: _defaults: autowire: true autoconfigure: true public: false bind: $defaultLocale: %kernel.locale% App\: resource: ../src @nicolasgrekas Custom binding for a named argument?

Slide 19

Slide 19 text

services: #... App\FooConsomer: arguments: - !tagged foo @nicolasgrekas Inject all services with the « foo » tag?

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

services: #... App\FooConsomer: arguments: - '%env(file:API_KEY_FILE)%' @nicolasgrekas Read a secret from a file?

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

What about performances? @nicolasgrekas

Slide 24

Slide 24 text

Hey, Symfony uses a compiled container! @nicolasgrekas

Slide 25

Slide 25 text

Lazyness everywhere • On-demand service instantiation • Service iterators • On-demand service factories • Service locators @nicolasgrekas

Slide 26

Slide 26 text

What about performance? • Short lived processes vs • Long lived processes @nicolasgrekas

Slide 27

Slide 27 text

Long lived kernel challenges • Stateless services done for core, fix your services • Resettable services • Resilient services community work pending @nicolasgrekas

Slide 28

Slide 28 text

#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

Slide 29

Slide 29 text

Symfony 4 wish Keep you at coding as long as possibe @nicolasgrekas

Slide 30

Slide 30 text

DI v4 3/3 - Inject services as arguments into actions - ServiceSubscriberInterface (<3 for service locators) - PHP configuration - @required - Kernel implements CompilerPassInterface - Resettable services @nicolasgrekas

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Enjoy Thank you @nicolasgrekas joind.in/22877