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

DIとLaravel

polidog
November 23, 2018

 DIとLaravel

polidog

November 23, 2018
Tweet

More Decks by polidog

Other Decks in Programming

Transcript

  1. 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 {
  2. 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 {
  3. 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 {
  4. 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
  5. 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
  6. 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 }
  7. 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
  8. 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 }
  9. ?>