Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
DIとLaravel
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
polidog
PRO
November 23, 2018
Programming
450
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
DIとLaravel
polidog
PRO
November 23, 2018
More Decks by polidog
See All by polidog
AIがコードを書く時代の ジェネレーティブプログラミング
polidog
PRO
3
1.1k
小さな開発会社を作った理由(再)
polidog
PRO
0
270
React Server ComponentsでAPI不要の開発体験
polidog
PRO
1
830
小さな開発会社が受託開発に力を入れる理由
polidog
PRO
0
520
小さな開発会社を作った理由
polidog
PRO
1
3.6k
小さな開発会社がWebサービスを作る理由
polidog
PRO
1
850
トランザクションスクリプトは何がダメなのか?
polidog
PRO
2
4k
React初心者がHooksを理解するためにコードを読んでみた
polidog
PRO
0
620
object-mapperを作ってる話
polidog
PRO
0
410
Other Decks in Programming
See All in Programming
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
400
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.4k
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
560
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
730
DynamoDBには集計系のクエリがないけどなんとかしたい
musan
1
180
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
170
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
170
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
120
Contextとはなにか
chiroruxx
1
360
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.2k
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4.5k
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
220
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
41
2.6k
What's in a price? How to price your products and services
michaelherold
247
13k
Mobile First: as difficult as doing things right
swwweet
225
10k
How to build a perfect <img>
jonoalderson
1
5.7k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
210
WENDY [Excerpt]
tessaabrams
11
38k
How to train your dragon (web standard)
notwaldorf
97
6.7k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
RailsConf 2023
tenderlove
30
1.5k
Crafting Experiences
bethany
1
190
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
Transcript
DIͱLaravel @polidog
ࣗݾհ • @polidog • ੩Ԭग़ • ౦ژͰSymfonyॻ͍ͯ͝൧৯͍ͯ·͢ • SymfonyϢʔβʔձͷਓ •
Symfony͖Ͱ͢
Dependency Injectionʁ
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 {
ίϯϙʔωϯτؒͷґଘؔ ΛϓϩάϥϜͷιʔείʔυ ͔Βഉআ͠ɺ֎෦ͷઃఆϑΝ ΠϧͳͲͰೖͰ͖ΔΑ͏ʹ ͢ΔιϑτΣΞύλʔϯ (wikipedia)
None
ར༻͢ΔΦϒδΣΫτͷੜ ʹؔ͢ΔࣝΛอ࣋͠ͳ͍
ίϯετϥΫλͷҾͰ ґଘ͢ΔΦϒδΣΫτΛࢦఆ ͢Δ
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 {
ͦͯ͠֎෦͔ΒͦͷΦϒδΣ ΫτΛͯ͠Β͏
ར༻͢ΔΦϒδΣΫτͷੜ ʹؔ͢ΔࣝΛอ࣋͠ͳ͍ࣄ
Service Container
αʔϏείϯςφͱʁ • ΦϒδΣΫτͷੜखॱͷཧ • ੜͨ͠ΦϒδΣΫτͷཧ
app()
࣮ࡍʹDIΛମݧͯ͠ΈΔ
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 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
None
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
Binding Primitives https://laravel.com/docs/5.7/container
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 }
Interface
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
None
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 }
bind?
ΦϒδΣΫτͷੜखॱΛొ ͢Δ
make?
࣮ࡍʹΦϒδΣΫτΛੜ͢ Δ
?>