Slide 1

Slide 1 text

DIͱLaravel @polidog

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Dependency Injectionʁ

Slide 4

Slide 4 text

1 executor = $executor; 22 } 23 24 public function __invoke() 25 {

Slide 5

Slide 5 text

ίϯϙʔωϯτؒͷґଘؔ܎ ΛϓϩάϥϜͷιʔείʔυ ͔Βഉআ͠ɺ֎෦ͷઃఆϑΝ ΠϧͳͲͰ஫ೖͰ͖ΔΑ͏ʹ ͢Διϑτ΢ΣΞύλʔϯ (wikipedia)

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

1 executor = $executor; 22 } 23 24 public function __invoke() 25 {

Slide 10

Slide 10 text

ͦͯ͠֎෦͔ΒͦͷΦϒδΣ ΫτΛ౉ͯ͠΋Β͏

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Service Container

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

app()

Slide 15

Slide 15 text

࣮ࡍʹDIΛମݧͯ͠ΈΔ

Slide 16

Slide 16 text

1 executor = $executor; 22 } 23 24 public function __invoke() 25 {

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Binding Primitives https://laravel.com/docs/5.7/container

Slide 21

Slide 21 text

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 }

Slide 22

Slide 22 text

Interface

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

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 }

Slide 26

Slide 26 text

bind?

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

make?

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

?>