Save 37% off PRO during our Black Friday Sale! »

DIとLaravel

80b7d87c10fcdb6a0cba4f8b69e933bc?s=47 polidog
November 23, 2018

 DIとLaravel

80b7d87c10fcdb6a0cba4f8b69e933bc?s=128

polidog

November 23, 2018
Tweet

Transcript

  1. DIͱLaravel @polidog

  2. ࣗݾ঺հ • @polidog • ੩Ԭग़਎ • ౦ژͰSymfonyॻ͍ͯ͝൧৯΂͍ͯ·͢ • SymfonyϢʔβʔձͷਓ •

    Symfony޷͖Ͱ͢
  3. Dependency Injectionʁ

  4. 1 <?php 2 3 namespace App\Http\Controllers; 4 5 6 use

    App\Executor; 7 8 class Index extends Controller 9 { 10 /** 11 * @var Executor 12 */ 13 private $executor; 14 15 /** 16 * IndexController constructor. 17 * @param Executor $executor 18 */ 19 public function __construct(Executor $executor) 20 { 21 $this->executor = $executor; 22 } 23 24 public function __invoke() 25 {
  5. ίϯϙʔωϯτؒͷґଘؔ܎ ΛϓϩάϥϜͷιʔείʔυ ͔Βഉআ͠ɺ֎෦ͷઃఆϑΝ ΠϧͳͲͰ஫ೖͰ͖ΔΑ͏ʹ ͢Διϑτ΢ΣΞύλʔϯ (wikipedia)

  6. None
  7. ར༻͢ΔΦϒδΣΫτͷੜ੒ ʹؔ͢Δ஌ࣝΛอ࣋͠ͳ͍

  8. ίϯετϥΫλͷҾ਺Ͱ ґଘ͢ΔΦϒδΣΫτΛࢦఆ ͢Δ

  9. 1 <?php 2 3 namespace App\Http\Controllers; 4 5 6 use

    App\Executor; 7 8 class Index extends Controller 9 { 10 /** 11 * @var Executor 12 */ 13 private $executor; 14 15 /** 16 * IndexController constructor. 17 * @param Executor $executor 18 */ 19 public function __construct(Executor $executor) 20 { 21 $this->executor = $executor; 22 } 23 24 public function __invoke() 25 {
  10. ͦͯ͠֎෦͔ΒͦͷΦϒδΣ ΫτΛ౉ͯ͠΋Β͏

  11. ར༻͢ΔΦϒδΣΫτͷੜ੒ ʹؔ͢Δ஌ࣝΛอ࣋͠ͳ͍ࣄ

  12. Service Container

  13. αʔϏείϯςφͱ͸ʁ • ΦϒδΣΫτͷੜ੒खॱͷ؅ཧ • ੜ੒ͨ͠ΦϒδΣΫτͷ؅ཧ

  14. app()

  15. ࣮ࡍʹDIΛମݧͯ͠ΈΔ

  16. 1 <?php 2 3 namespace App\Http\Controllers; 4 5 6 use

    App\Executor; 7 8 class Index extends Controller 9 { 10 /** 11 * @var Executor 12 */ 13 private $executor; 14 15 /** 16 * IndexController constructor. 17 * @param Executor $executor 18 */ 19 public function __construct(Executor $executor) 20 { 21 $this->executor = $executor; 22 } 23 24 public function __invoke() 25 {
  17. 5 6 class Executor 7 { 8 /** 9 *

    @var Processor 10 */ 11 private $output; 12 13 /** 14 * @var string 15 */ 16 private $name; 17 18 /** 19 * Executor constructor. 20 * @param Processor $output 21 * @param string $name 22 */ 23 public function __construct(Processor $output, string $name) 24 { 25 $this->output = $output; 26 $this->name = $name; 27 } 28 29 public function run() : string
  18. None
  19. 5 6 class Executor 7 { 8 /** 9 *

    @var Processor 10 */ 11 private $output; 12 13 /** 14 * @var string 15 */ 16 private $name; 17 18 /** 19 * Executor constructor. 20 * @param Processor $output 21 * @param string $name 22 */ 23 public function __construct(Processor $output, string $name) 24 { 25 $this->output = $output; 26 $this->name = $name; 27 } 28 29 public function run() : string
  20. Binding Primitives https://laravel.com/docs/5.7/container

  21. 5 use App\Executor; 6 use Illuminate\Support\ServiceProvider; 7 8 class AppServiceProvider

    extends ServiceProvider 9 { 10 /** 11 * Bootstrap any application services. 12 * 13 * @return void 14 */ 15 public function boot() 16 { 17 // 18 } 19 20 /** 21 * Register any application services. 22 * 23 * @return void 24 */ 25 public function register() 26 { 27 $this->app->when(Executor::class) 28 ->needs('$name') 29 ->give(config('app.name')); 30 }
  22. Interface

  23. 5 6 use App\ExecutorInterface; 7 8 class Index extends Controller

    9 { 10 /** 11 * @var ExecutorInterface 12 */ 13 private $executor; 14 15 /** 16 * Index constructor. 17 * @param ExecutorInterface $executor 18 */ 19 public function __construct(ExecutorInterface $executor) 20 { 21 $this->executor = $executor; 22 } 23 24 public function __invoke() 25 { 26 return view('index', [ 27 'message' => $this->executor->run() 28 ]); 29 } 30
  24. None
  25. 9 class AppServiceProvider extends ServiceProvider 10 { 11 /** 12

    * Bootstrap any application services. 13 * 14 * @return void 15 */ 16 public function boot() 17 { 18 // 19 } 20 21 /** 22 * Register any application services. 23 * 24 * @return void 25 */ 26 public function register() 27 { 28 $this->app->bind(ExecutorInterface::class, Executor::class); 29 $this->app->when(Executor::class) 30 ->needs('$name') 31 ->give(config('app.name')); 32 } 33 }
  26. bind?

  27. ΦϒδΣΫτͷੜ੒खॱΛొ ࿥͢Δ

  28. make?

  29. ࣮ࡍʹΦϒδΣΫτΛੜ੒͢ Δ

  30. ?>