5.5リリース直前 Laravel Container 使い方振り返り

5.5リリース直前 Laravel Container 使い方振り返り

17d4ef53b432ebf7c566fd6a11345570?s=128

yuuki takezawa

July 31, 2017
Tweet

Transcript

  1. 5.5௚લʂ Container ৼΓฦΓ yuuki takezawa / ytake

  2. Author • https://github.com/ytake/Laravel-Couchbase • https://github.com/ytake/Laravel-Aspect • https://github.com/ytake/Laravel.Smarty

  3. Container!

  4. class HogeFugaPiyo { public function nanika() { \App::make(‘aaa’); app(‘bbb’); }

    }
  5. ݁߹౓ߴ͠ μϝઈର

  6. class HogeFugaPiyo { public function __construct(Application $app) { $this->app =

    $app; } }
  7. Application ຊମ͸ඞཁͳ͍Ͱ͢ΑͶ

  8. class Hoge { public $int = 1; }

  9. class HogeFugaPiyo { public function __construct(Hoge $hoge) { $this->hoge =

    $hoge; } }
  10. Πϯελϯεͷੜ੒ํ๏Λࢦఆ

  11. // Hogeͱॻ͍ͯ͋ͬͨΒHogeΛ͍ͩ͘͞ $this->app->bind(Hoge::class, function () { return new Hoge; });

    $this->app->bind(Hoge::class); // singletonͰ͍ͩ͘͞ $this->app->singleton(Hoge::class);
  12. Πϯελϯεͷੜ੒ํ๏Λࢦఆ Ҿ਺͕ඞཁͳ৔߹

  13. class Fuga { private $arg; public function __construct($arg) { $this->arg

    = $arg; } }
  14. $this->app->bind(Fuga::class, function () { return new Fuga(1); });

  15. Πϯελϯεͷੜ੒ํ๏Λࢦఆ ࠞ߹

  16. class Piyo { private $hoge; private $fuga; public function __construct(Hoge

    $hoge, Fuga $fuga) { $this->hoge = $hoge; $this->fuga = $fuga; } }
  17. $this->app->bind(Hoge::class, function () { return new Hoge; }); $this->app->bind(Fuga::class, function

    () { return new Fuga(1); });
  18. // ৔߹ʹΑͬͯ͸͜Μͳͷ΋ $this->app->bind( Fuga::class, function (Application $app) { return new

    Fuga($app->make(‘nanika’)); });
  19. Πϯελϯεੜ੒࣌ʹ ಛఆͷԿ͔Λ͍ͨ͠

  20. class HogeFugaPiyo { private $name = ‘’; public function __construct(Hoge

    $hoge) { $this->hoge = $hoge; } public function setName(string $name) { $this->name = $name; } }
  21. $this->app->extend( HogeFugaPiyo::class, function (HogeFugaPiyo $hoge) { $hoge->setName(“hoge”); return $hoge; });

    * ଞʹ΋͋Γ·͢
  22. ಛఆͷΫϥεΛܧঝͨ͠Ϋϥεͷ Πϯελϯε͕ੜ੒͞Εͨ࣌ʹ ͳʹ͔͍ͨ͠

  23. abstract class AbstractHoge { private $name = ‘abstract’; public function

    setName(string $name) { $this->name = $name; } }
  24. class Hoge extends AbstractHoge { }

  25. $this->app->resolving( AbstractHoge::class, function (AbstractHoge $abstractHoge) { $abstractHoge->setName(‘aaaaa’); return $abstractHoge; });

  26. ૄ݁߹ʹ͍ͨ͠

  27. // constructorʹinterfaceΛॻ͖·͠ΐ͏ $this->app->bind(HogeInterface::class, Hoge::class);

  28. Կ͔ͷ৚݅Ͱฦ٫͞ΕΔ ΠϯελϯεΛมߋ͍ͨ͠

  29. use Illuminate\Support\Manager; class StringManager extends Manager { protected $driver =

    ‘hoge’; public function getDefaultDriver() { return $this->driver; } protected function createHogeDriver() { return ‘hoge dayo’; } protected function createFugaDriver() { return ‘fuga dayo’; } }
  30. // constructorͰManagerΫϥε ܕએݴ $this->app->bind( StringManager::class, function(Application $app) { return new

    StringManager($app); });
  31. protected $manager; public function __construct(StringManager $manager) { $this->manager = $manager;

    } public function __invoke($id) { $manager = $this->manager->driver(‘hoge’); if($id === 1) { $manager = $this->manager->driver(‘fuga’); } dd($manager); }
  32. SOLIDͳAppΛʂ