$30 off During Our Annual Pro Sale. View Details »

DIとLaravel

polidog
November 23, 2018

 DIとLaravel

polidog

November 23, 2018
Tweet

More Decks by polidog

Other Decks in Programming

Transcript

  1. DIͱLaravel
    @polidog

    View Slide

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

    View Slide

  3. Dependency Injectionʁ

    View Slide

  4. 1 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 {

    View Slide

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

    View Slide

  6. View Slide

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

    View Slide

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

    View Slide

  9. 1 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 {

    View Slide

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

    View Slide

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

    View Slide

  12. Service Container

    View Slide

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

    View Slide

  14. app()

    View Slide

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

    View Slide

  16. 1 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 {

    View Slide

  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

    View Slide

  18. View Slide

  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

    View Slide

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

    View Slide

  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 }

    View Slide

  22. Interface

    View Slide

  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

    View Slide

  24. View Slide

  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 }

    View Slide

  26. bind?

    View Slide

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

    View Slide

  28. make?

    View Slide

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

    View Slide

  30. ?>

    View Slide