Pro Yearly is on sale from $80 to $50! »

IoC container - Confoo Montreal 2018

IoC container - Confoo Montreal 2018

Did you know your IoC container can do a whole lot more than just constructor injection? Besides that it is actually packed with features. Inflectors, resolving callbacks, aliasing, method invocation to name a few. In this talk you will learn how to leverage the power of a great container and service providers to write better, loosely coupled code. Well designed code put together by your IoC container will make your applications SOLID, modular, lean and decoupled from the framework!

39eb3f3d313b13f05534e496285040b8?s=128

Hannes Van De Vreken

March 07, 2018
Tweet

Transcript

  1. IOC container @hannesvdvreken #confoo

  2. Hi, my name is Hannes.

  3. !

  4. None
  5. None
  6. IoC Container

  7. I O C

  8. International O C

  9. International Olympic C

  10. International Olympic Committee

  11. International Olympic Committee

  12. IoC Container

  13. IoC Container

  14. WHAT IS IOC? IoC == Inversion of Control

  15. WHAT IS IOC? class Service { public function __construct() {

    init(); } public function init() { $config = config(‘ftpsettings…’); $this->ftp = new FTP(…); } public function calculate(); }
  16. WHAT IS IOC? $service = $container->get(Service::class); $service->calculate();

  17. But first: Design patterns WHAT IS IOC?

  18. class Service { public function calculate() { // Get stuff

    from FTP. // Do calculations. // Return result. } } WHAT IS IOC?
  19. class Service { public function __construct(Ftp $ftp) { } public

    function calculate() { // Get stuff from FTP. // Do calculations. // Return result. } } WHAT IS IOC?
  20. class Service { public function __construct(Ftp $ftp) { } public

    function calculate() { // Get stuff from FTP. // Add caching to prevent hitting FTP over and over again // Do calculations. // Return result. } } WHAT IS IOC?
  21. class Service { public function __construct(Ftp $ftp) { } public

    function calculate() { // Get stuff from FTP. // Add caching to prevent hitting FTP over and over again // Log that FTP is accessed. // Do calculations. // Return result. } } WHAT IS IOC?
  22. class Service { public function __construct(Ftp $ftp) { } public

    function calculate() { // Get stuff from FTP. // Add caching to prevent hitting FTP over and over again // Log that FTP is accessed. // Do calculations. // Return result. } } WHAT IS IOC?
  23. class FtpStorage { public function __construct(Ftp $ftp) { $this->ftp =

    $ftp; } public function get($key) { // Get it. // Cache it. // Log it. // Return it. } } WHAT IS IOC?
  24. class Service { public function __construct(FtpStorage $storage) {
 $this->storage =

    $storage; } public function calculate() { $value = $this->storage->get($key); return $calculate($value); } } WHAT IS IOC?
  25. interface Storage { public function get($key); } WHAT IS IOC?

  26. class FtpStorage implements Storage { public function __construct(Ftp $ftp) {

    $this->ftp = $ftp; } public function get($key) { … } } WHAT IS IOC?
  27. class Service { public function __construct(Storage $storage) {
 $this->storage =

    $storage; } } WHAT IS IOC?
  28. class CacheDecorator implements Storage { public function __construct(Cache $cache, Storage

    $storage) {…} public function get($key) { return $this->cache->remember($key, 60, function () { return $this->storage->get($key); }); } } WHAT IS IOC?
  29. class LogDecorator implements Storage { public function __construct(LoggerInterface $log, Storage

    $storage) {…} public function get($key) { $this->log->info('Retrieved value for '.$key); return $this->storage->get($key); } } WHAT IS IOC?
  30. class FtpStorage implements Storage { public function __construct(Ftp $ftp) {

    $this->ftp = $ftp; } public function get($key) { // Get it and return it. } } WHAT IS IOC?
  31. FtpStorage Service depends on depends on Ftp WHAT IS IOC?

  32. FtpStorage implements <interface> Storage Service depends on depends on Ftp

    WHAT IS IOC?
  33. FtpStorage <interface> Storage CacheDecorator Service LogDecorator S3Storage WHAT IS IOC?

    implements depends on
  34. WHAT IS IOC? FtpStorage Service depends on depends on Ftp

    FtpStorage implements <interface> Storage Service depends on depends on Ftp
  35. Dependency inversion: Interface + implementation Dependency injection: Injecting `Storage` into

    `Service` Inversion of Control: The container builds your objects and calls them WHAT IS IOC?
  36. new Service($ftp); WHAT IS IOC?

  37. new Service( new CacheDecorator( $cache, new LogDecorator( $logger, new FtpStorage(

    new Ftp($settings) ) ) ) ); WHAT IS IOC?
  38. $container->get(Service::class); 1) Make FtpStorage 2) Wrap it with LogDecorator 3)

    Wrap LogDecorator instance with CacheDecorator 4) New up Service with CacheDecorator instance. WHAT IS IOC?
  39. Inversion of Control IoC container to the rescue WHAT IS

    IOC?
  40. Let the IoC container help you with Composition WHAT IS

    IOC?
  41. symfony/dependency-injection illuminate/container aura/di zendframework/zend-di pimple/pimple league/container … container-interop/container-interop psr/container (PSR-11)

    CONSTRUCTOR INJECTION
  42. None
  43. PSR-11 namespace Interop\Container; use Psr\Container\ContainerInterface as PsrContainer; interface ContainerInterface extends

    PsrContainer { … }
  44. PSR-11 namespace Psr\Container; interface ContainerInterface { public function get($id); public

    function has($id); }
  45. Using the container

  46. CONSTRUCTOR INJECTION $service = $container->get(Service::class);

  47. Automatic object resolution

  48. class SendInvoice { public function __construct(Mailer $mailer) { $this->mailer =

    $mailer; } public function listen(InvoiceGeneratedEvent $event) { … } } CONSTRUCTOR INJECTION
  49. Constructor injection CONSTRUCTOR INJECTION

  50. $listener = $container->get(SendInvoice::class); CONSTRUCTOR INJECTION

  51. // League $container = new Container(); $container->delegate(new ReflectionContainer()); $listener =

    $container->get(SendInvoice::class); CONSTRUCTOR INJECTION
  52. Reflection CONSTRUCTOR INJECTION

  53. (new \ReflectionClass($class)) ->getConstructor() ->getParameters(); CONSTRUCTOR INJECTION

  54. Recursively CONSTRUCTOR INJECTION

  55. Auto wiring: - Symfony - Zend - Laravel - league/container

    - … CONSTRUCTOR INJECTION
  56. But what if our code is SOLID… The container can’t

    construct interfaces? CONSTRUCTOR INJECTION
  57. Helping the container (not standardized)

  58. Situation 1: When <<interface>> is type hinted, resolve <<class>> instead

    REGISTRATION - BINDING
  59. Solution: Aliasing. REGISTRATION - BINDING

  60. $container->add(MailerInterface::class, function () { return $container->get(Mailer::class); }); REGISTRATION - BINDING

  61. // Symfony $container->setAlias(MailerInterface::class, Mailer::class); // Laravel $container->alias(MailerInterface::class, Mailer::class); REGISTRATION -

    BINDING
  62. Situation 2: When <<class>> has non-type hinted constructor arguments. REGISTRATION

    - BINDING
  63. Solution: Define dependencies. REGISTRATION - BINDING

  64. // League, et al. $container->add(Mailer::class, function () { return new

    Mailer($username, $password); }); REGISTRATION - BINDING
  65. // Symfony $container->register(Mailer::class) ->addArgument($username) ->addArgument($password); REGISTRATION - BINDING

  66. Situation 3: When <<class>> is type hinted, always return the

    exact same object. REGISTRATION - BINDING
  67. Example: Global state in a - Translator (user’s locale) -

    Entity Manager - Event dispatcher REGISTRATION - BINDING
  68. Solution: Sharing an instance. REGISTRATION - BINDING

  69. // League $container->share(EntityManagerInterface::class, function () { … }); // Laravel

    $container->singleton(EntityManagerInterface::class, function () { … }); // Symfony $container->register()->setShared(true); REGISTRATION - BINDING
  70. Situation 4: When different classes need a different object of

    the same interface. REGISTRATION - BINDING
  71. class PhotosController { public function __construct(FilesystemInterface $photos) { } }

    class FilesController { public function __construct(FilesystemInterface $files) { } } REGISTRATION - BINDING
  72. Solution: Contextual binding. REGISTRATION - BINDING

  73. // Laravel $container->bind('fs.photos', function () {}); $container->bind('fs.files', function () {});

    REGISTRATION - BINDING
  74. // Laravel $container ->when(PhotosController::class) ->needs(FilesystemInterface::class) ->give('fs.photos'); REGISTRATION - CONTEXTUAL BINDING

  75. Situation 5: When resolving <<interface>>, do some setup. REGISTRATION -

    BINDING
  76. Example 1: When resolving the EntityManager, register some ModelListeners. REGISTRATION

    - BINDING
  77. Example 2: interface EnvironmentAware { public function setEnv($env); } trait

    EnvironmentAware { public function setEnv($env) { $this->env = $env; } } REGISTRATION - CONTAINER EVENTS
  78. Solution: Container events / inflection / hooks / … REGISTRATION

    - BINDING
  79. // League $container->inflector(EntityManager::class, function (EntityManager $em) { $em->getConfiguration() ->getEntityListenerResolver() ->register(new

    ModelListener()); } ); REGISTRATION - BINDING
  80. // Laravel $container->resolving(function (EnvironmentAware $object) { $object->setEnv(getenv('APP_ENV')); }); REGISTRATION -

    CONTAINER EVENTS
  81. Also useful for: - Quickly injecting stuff REGISTRATION - CONTAINER

    EVENTS
  82. interface CommandBusAware trait CommandBusAware interface EventDispatcherAware trait EventDispatcherAware REGISTRATION -

    CONTAINER EVENTS
  83. Also useful for: - Quickly injecting stuff REGISTRATION - CONTAINER

    EVENTS
  84. Also useful for: - Quickly injecting stuff - Injecting soft

    dependencies REGISTRATION - CONTAINER EVENTS
  85. $container—>resolving(function (Mailer $mailer) { $mailer->setQueueResolver(function () { return $container->make(Queue::class); });

    ); REGISTRATION - CONTAINER EVENTS
  86. Also useful for: - Quickly injecting stuff - Injecting soft

    dependencies REGISTRATION - CONTAINER EVENTS
  87. Also useful for: - Quickly injecting stuff - Injecting soft

    dependencies - Setting configurable values REGISTRATION - CONTAINER EVENTS
  88. $container->resolving(function (Validator) { $fileTypes = $container->get('config')->get(…) $validator->setAllowedFileTypes($fileTypes); ); REGISTRATION -

    CONTAINER EVENTS
  89. Where to put all this? REGISTRATION

  90. Symfony: - Configuration files - Factories REGISTRATION

  91. container-interop/service-provider Similar: - league/container - illuminate/container REGISTRATION

  92. Using a container

  93. Retrieving objects CALLING THE CONTAINER

  94. // Psr\Container\ContainerInterface $container->has('fs'); $container->get('fs'); CALLING THE CONTAINER

  95. Retrieving objects, with arguments CALLING THE CONTAINER

  96. // League $container->add('fs', function ($args) { // Use $args[0] to

    see what folder to use. }); $container->get('fs', ['images']); CALLING THE CONTAINER
  97. // League $container->add('fs.images', function () { return $container->get('fs', ['images']); });

    CALLING THE CONTAINER
  98. Calling functions, with resolved params CALLING THE CONTAINER

  99. // League & Laravel $container->call($closure); $container->call(function (Mailer $mailer) {}); $container->call([$listener,

    'handle']); $container->call('Listener::handle'); CALLING THE CONTAINER
  100. (new ReflectionFunction($callback)) ->getParameters(); CALLING THE CONTAINER

  101. // Laravel $func = $container->wrap('Listener@handle'); $func(); CALLING THE CONTAINER

  102. public function wrap($callback) { return function () { $this->call($callback); };

    } CALLING THE CONTAINER
  103. Tagging CALLING THE CONTAINER

  104. Beware: Tagging in Symfony != Tagging in Laravel CALLING THE

    CONTAINER
  105. // Laravel $container->tag('fs.images', 'filesystem'); $container->tag('fs.pdfs', 'filesystem'); $filesystems = $container->tagged('filesystem'); foreach

    ($filesystems as $filesystem) { … } CALLING THE CONTAINER
  106. Preferably only in - Factories - Event Dispatchers - Router

    - Command Bus - … CALLING THE CONTAINER
  107. Bonus

  108. Solving circular dependencies BONUS - CIRCULAR DEPENDENCIES

  109. $mailer = new Mailer(QueueInterface $queue); $queue = new Queue(MailerInterface $mailer);

    BONUS - CIRCULAR DEPENDENCIES
  110. $mailer = new Mailer(); $queue = new Queue(MailerInterface $mailer); $mailer->setQueueResolver($callable);

    BONUS CIRCULAR DEPENDENCIES
  111. $queue = $container->get(QueueInterface::class); // League $container->inflector(Mailer::class, function () { $mailer->setQueueResolver(function

    () { return $container->get(QueueInterface::class); }); }); BONUS CIRCULAR DEPENDENCIES
  112. Environment aware injection $container->bind(‘fs.photos’::class, function () { if (getenv(‘APP_ENV’) ===

    ‘integration’) { // Fill in memory adapter // with integration testing files
 } else { // Normal connection to S3. } }); BONUS - CIRCULAR DEPENDENCIES
  113. recap: Reflection Aliasing Soft dependencies Using container

  114. Thank you! @hannesvdvreken #confoo

  115. Time for questions. @hannesvdvreken #confoo

  116. None
  117. • https://getstream.io/try-the-api REFERENCES