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

IoC container - Laracon EU 2016

IoC container - Laracon EU 2016

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!

Feedback here: https://joind.in/event/laracon-eu-2016/ioc-container-beyond-constructor-injection

Hannes Van De Vreken

August 23, 2016
Tweet

More Decks by Hannes Van De Vreken

Other Decks in Technology

Transcript

  1. !

  2. WHAT IS IOC? class Service { public function __construct(Ftp $ftp)

    { } public function calculate() { // Get stuff from FTP. // Do calculations. // Return result. } }
  3. WHAT IS IOC? 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. } }
  4. WHAT IS IOC? 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. } }
  5. WHAT IS IOC? 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. } }
  6. WHAT IS IOC? \ class FtpStorage { public function __construct(Ftp

    $ftp) { $this->ftp = $ftp; } public function get($key) { // Get it. // Cache it. // Log it. } }
  7. WHAT IS IOC? \ class FtpStorage implements Storage { public

    function __construct(Ftp $ftp) { $this->ftp = $ftp; } public function get($key) { … } }
  8. WHAT IS IOC? \ 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); }); } }
  9. WHAT IS IOC? \ 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); } }
  10. WHAT IS IOC? \ class FtpStorage implements Storage { public

    function __construct(Ftp $ftp) { $this->ftp = $ftp; } public function get($key) { // Get it and return it. } }
  11. WHAT IS IOC? Dependency inversion: Interface + implementation Dependency injection:

    Injecting `Storage` into `Service` Inversion of Control: The container builds your objects and calls them
  12. WHAT IS IOC? $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.
  13. class SendInvoice { public function __construct(Mailer $mailer) { $this->mailer =

    $mailer; } public function listen(InvoiceGeneratedEvent $event) { … } }
  14. class ServiceProvider { protected $defer = true; public function register()

    {…} public function provides() { return [ Mailer::class, SwiftMailer::class, ]; } }
  15. REGISTRATION - BINDING $this->app->bind(Mailer::class, function () { $mailer = new

    SwiftMailer(); return new LogDecorator($mailer); });
  16. trait EnvironmentAware { public function setEnv($env) { $this->env = $env;

    } private function isEnv($envs): bool { return in_array($this->env, (array) $envs); } }
  17. REGISTRATION - CONTAINER EVENTS Also useful for: - Quickly injecting

    stuff - Injecting soft dependencies - Setting configurable values
  18. BONUS - CIRCULAR DEPENDENCIES $mailer = new Mailer(); $queue =

    new Queue(MailerInterface $mailer); $mailer->setQueueResolver(function () { return $this->app->make('queue'); });