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

Entenda de uma vez por todas o Container do Lar...

Ravan Scafi
November 17, 2017

Entenda de uma vez por todas o Container do Laravel

Mais do que uma forma de injetar dependências no construtor de classes, o container do Laravel é uma ferramenta poderosa do framework, que você já usa e talvez não conheça. Entenda como o Container auxilia no Dependency Inversion Principle do SOLID, conheça os principais tipos de bindings e, de quebra, aprenda sobre Contracts, Service Providers e Facades.

Ravan Scafi

November 17, 2017
Tweet

More Decks by Ravan Scafi

Other Decks in Programming

Transcript

  1. Ravan Scafi Back-end Developer na Leroy Merlin Brasil Co-organizador do

    Meetup do Laravel SP Evangelista do PHPSP @ravanscafi
  2. <?php namespace App; use Helpers\HtmlPresenter; class ProfitReport { public function

    generateReport($sales) { $monthlyProfit = $this->calculateProfit($sales); $htmlPresenter = new HtmlPresenter(); return $htmlPresenter->present($monthlyProfit); } }
  3. <?php namespace App; use Helpers\HtmlPresenter; class ProfitReport { public function

    generateReport($sales) { $monthlyProfit = $this->calculateProfit($sales); $htmlPresenter = new HtmlPresenter(); return $htmlPresenter->present($monthlyProfit); } } ProfitReport Acoplado ao HtmlPresenter
  4. <?php namespace App; use Helpers\HtmlPresenter; class ProfitReport { private $presenter;

    public function setPresenter(HtmlPresenter $presenter) { $this->presenter = $presenter; } public function generateReport($sales) { $monthlyProfit = $this->calculateProfit($sales); return $this->presenter->present($monthlyProfit); } } Invertendo a dependência com um Setter
  5. <?php namespace App; use Helpers\HtmlPresenter; class ProfitReport { private $presenter;

    public function setPresenter(HtmlPresenter $presenter) { $this->presenter = $presenter; } public function generateReport($sales) { $monthlyProfit = $this->calculateProfit($sales); return $this->presenter->present($monthlyProfit); } } Invertendo a dependência com um Setter
  6. <?php namespace App; use Helpers\HtmlPresenter; class ProfitReport { private $presenter;

    public function setPresenter(HtmlPresenter $presenter) { $this->presenter = $presenter; } public function generateReport($sales) { $monthlyProfit = $this->calculateProfit($sales); return $this->presenter->present($monthlyProfit); } } Invertendo a dependência com um Setter
  7. public function getProfitReport($sales) { $report = new App\ProfitReport(); $htmlPresenter =

    new Helpers\HtmlPresenter(); $report->setPresenter($htmlPresenter); return $report->generateReport($sales); } Utilizando o Report com o Setter:
  8. public function getProfitReport($sales) { $report = new App\ProfitReport(); $htmlPresenter =

    new Helpers\HtmlPresenter(); $report->setPresenter($htmlPresenter); return $report->generateReport($sales); } Utilizando o Report com o Setter:
  9. <?php namespace App; use Helpers\HtmlPresenter; class ProfitReport { private $presenter;

    public function __construct(HtmlPresenter $presenter) { $this->presenter = $presenter; } public function generateReport($sales) { $monthlyProfit = $this->calculateProfit($sales); return $this->presenter->present($monthlyProfit); } } Invertendo a dependência com Injeção no Construtor
  10. <?php namespace App; use Helpers\HtmlPresenter; class ProfitReport { private $presenter;

    public function __construct(HtmlPresenter $presenter) { $this->presenter = $presenter; } public function generateReport($sales) { $monthlyProfit = $this->calculateProfit($sales); return $this->presenter->present($monthlyProfit); } } Invertendo a dependência com Injeção no Construtor
  11. public function getProfitReport($sales) { $htmlPresenter = new Helpers\HtmlPresenter(); $report =

    new App\ProfitReport($htmlPresenter); return $report->generateReport($sales); } Utilizando o Report com Injeção no Construtor:
  12. <?php namespace App; use Helpers\HtmlPresenter; class ProfitReport { private $presenter;

    public function __construct(HtmlPresenter $presenter) { $this->presenter = $presenter; } public function generateReport($sales) { $monthlyProfit = $this->calculateProfit($sales); return $this->presenter->present($monthlyProfit); } }
  13. public function getProfitReport($sales) { $htmlPresenter = new Helpers\HtmlPresenter(); $report =

    new App\ProfitReport($htmlPresenter); return $report->generateReport($sales); } Antes:
  14. <?php namespace App; class ProfitReport { private $presenter; public function

    __construct($presenter) { $this->presenter = $presenter; } public function generateReport($sales) { $monthlyProfit = $this->calculateProfit($sales); return $this->presenter->present($monthlyProfit); } }
  15. Controller Profit Report Html Presenter depende de depende de Json

    Presenter Presenter Interface implementa implementa
  16. <?php namespace Helpers; class HtmlPresenter implements PresenterInterface { public function

    present($data) { return view('reports', compact('data')); } }
  17. <?php namespace App; use Helpers\PresenterInterface; class ProfitReport { private $presenter;

    public function __construct(PresenterInterface $presenter) { $this->presenter = $presenter; } public function generateReport($sales) { $monthlyProfit = $this->calculateProfit($sales); return $this->presenter->present($monthlyProfit); } }
  18. <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class PresenterServiceProvider extends ServiceProvider {

    public function register() { $this->app->bind(Helpers\PresenterInterface::class, function ($app) { return new Helpers\HtmlPresenter(); }); } }
  19. <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class PresenterServiceProvider extends ServiceProvider {

    public function register() { $this->app->singleton(Helpers\PresenterInterface::class, function ($app) { var_dump('fui chamado!'); return new Helpers\HtmlPresenter(); }); } } Container: Singleton
  20. <?php namespace Illuminate\Support\Facades; class Cache extends Facade { protected static

    function getFacadeAccessor() { return 'cache'; } } Cache Facade
  21. Agradecimentos Agradecimentos especiais a todos do SlidesCarnival que fizeram e

    disponibilizaram o template da apresentação gratuitamente.