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コンテナ
Search
Sota Sugiura
April 20, 2016
Technology
0
4.9k
実践DIコンテナ
DIコンテナを使って役に立った場面をご紹介
PHP BLT#4 (
http://phpblt.connpass.com/event/27655/
) 発表資料
Sota Sugiura
April 20, 2016
Tweet
Share
More Decks by Sota Sugiura
See All by Sota Sugiura
内製したSlack Appで頑張るIncident Response@Waroom Meetup #1 / Incident Response with Slack App in 10X
sota1235
0
1.2k
20220926_セキュリティチームの今_for_Drs._Prime_公開用.pdf
sota1235
0
86
再発防止策を考える技術 / #phpconsen
sota1235
10
3.7k
How to choose the best npm module for your team?
sota1235
9
560
Realtime Database for high traffic production application
sota1235
7
3.9k
Road to migrate JP Web as a microservice
sota1235
4
1.5k
インターフェース再入門 / Think Interface again
sota1235
6
10k
再発防止策を考える技術 #phpconfuk_rej
sota1235
1
1.1k
Update around Firebase #io18
sota1235
3
4.3k
Other Decks in Technology
See All in Technology
[JAWS-UG新潟#20] re:Invent2024 -CloudOperationsアップデートについて-
shintaro_fukatsu
0
120
新機能VPCリソースエンドポイント機能検証から得られた考察
duelist2020jp
0
230
成果を出しながら成長する、アウトプット駆動のキャッチアップ術 / Output-driven catch-up techniques to grow while producing results
aiandrox
0
380
20241220_S3 tablesの使い方を検証してみた
handy
4
690
いまからでも遅くないコンテナ座学
nomu
0
130
TypeScript開発にモジュラーモノリスを持ち込む
sansantech
PRO
2
650
podman_update_2024-12
orimanabu
1
290
どちらを使う?GitHub or Azure DevOps Ver. 24H2
kkamegawa
0
1.2k
C++26 エラー性動作
faithandbrave
2
820
10個のフィルタをAXI4-Streamでつなげてみた
marsee101
0
180
UI State設計とテスト方針
rmakiyama
3
790
あの日俺達が夢見たサーバレスアーキテクチャ/the-serverless-architecture-we-dreamed-of
tomoki10
0
500
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
Thoughts on Productivity
jonyablonski
68
4.4k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Designing for Performance
lara
604
68k
What's in a price? How to price your products and services
michaelherold
244
12k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
BBQ
matthewcrist
85
9.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
910
Site-Speed That Sticks
csswizardry
2
190
Agile that works and the tools we love
rasmusluckow
328
21k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Transcript
࣮ફ%*ίϯςφ PO-BSBWFM !TPUB 1)1#-5
ࣗݾհ w ਿӜᰜଠ 4PUB4VHJVSB w !TPUB w ͪͺʔྺ w
גࣜձࣾΞΠελΠϧ ʘ'PMMPXNFʗ
ΈΜͳʮ%*ศརͩͧʂʯ
ʮͲ͏͍͏෩ʹʜʁʯ
ͯ͜ͱͰ
%*ίϯςφͷΛ͠·͢ ͬͯΔͧͬͯํπΠολʔͯ͠Ͳ͏ͧ Ňŏ﹏ŏ
%*ͱ w %*%FQFOEFODZ*OKFDUJPO w ʮґଘੑͷೖʯͱݴ͏ͱ͔ݴΘͳ͍ͱ͔ w ৄ͍͠આ໌࣌ؒͷ߹্Χοτ ứʆ
%*ίϯςφͱ w %*ύλʔϯͰґଘղܾ͞ΕͨΠϯελϯεΛฦ ͯ͘͠ΕΔγϩϞϊ w ༷ʑͳ࣮͕͋Δ͕ɺ͜͜Ͱ-BSBWFMΛྫʹ
-BSBWFMʹ͓͚Δ %*ίϯςφ
%*$POUBJOFSPO-BSBWFM w ίϯετϥΫλͰλΠϓώϯτ͓͍ͯͨ͠Ϋϥε Λ%*ίϯςφ͕ೖͯ͘͠ΕΔ w ඞཁͳΫϥεʹ͞ΒʹඞཁͳΠϯελϯε͕͋ Ε࠶ؼతʹΠϯελϯεΛೖ͢Δ
%*$POUBJOFSPO-BSBWFM #SFBE
%*$POUBJOFSPO-BSBWFM #SFBE *OFFE #BDPO -FUUVDFBOE5PNBUP public function __construct( Bacon $bacon,
Lettuce $lettuce, Tomato $tomato ) { // }
%*$POUBJOFSPO-BSBWFM %*$POUBJOFS (JWFZPV #SFBE *OFFE #BDPO -FUUVDFBOE5PNBUP #BDPO -FUUVDF 5PNBUP
%*$POUBJOFSPO-BSBWFM %*$POUBJOFS *OFFE4BMU #SFBE #BDPO -FUUVDF 5PNBUP public function __construct(Salt
$salt) { // }
%*$POUBJOFSPO-BSBWFM %*$POUBJOFS (JWFZPV *OFFE4BMU #SFBE #BDPO -FUUVDF 5PNBUP 4BMU
%*$POUBJOFSPO-BSBWFM %*$POUBJOFS /FFE /FFE /FFE #SFBE #BDPO -FUUVDF 5PNBUP 4BMU
/FFE
$MBTT#JOEJOH w %*ίϯςφʹ͋Β͔͡ΊΠϯελϯεΛಥͬࠐ ΊΔ
$MBTT#JOEJOH <?php /* ུ */ /** * */ public function
boot() { $this->app->bind( // ίϯετϥΫλͰλΠϓώϯτ͞ΕΔΫϥε໊ \App\Dinners\PotatoTips::class, function ($app) { // ࣮ࡍʹฦ͞ΕΔΠϯελϯε return new \App\Dinners\Blt; } ); }
$MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS
$MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS $this->app->bind( \App\Dinners\PotatoTips::class, function ($app) {
return new \App\Dinners\Blt; } );
$MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS (JWFZPV #MU
$MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS (JWFZPV #MU ˞௨ৗͷλΠϓώϯτͱಉ༷ɺ ܧঝؔʹͳ͍ͱΤϥʔʹͳΓ·͢
͜͜·Ͱલ࠲
ຊ ͜ͷ%*ίϯςφ͕ʹཱͬͨ໘Λ͝հʂʂʂ
ͦͷ ࣮ͷࠩ͠ସ͑
Ғ͍ਓʮநʹґଘͤΑʯ w Α͋͘Δ%*ίϯςφͷ͍ํͱͯ͠நΫϥε ʹ۩ΫϥεΛ$MBTT#JOEJOH͢Δ
*OUFSGBDFBOE$MBTT interface Burger { public function addTopping($toppingName); } class Blt
implements Burger { public function addTopping($toppingName) { // } }
#JOE*OUFSGBDF /* in DI Container */ public function boot() {
// ΠϯλʔϑΣʔεʹ۩ΫϥεΛbind // BurgerΠϯλʔϑΣʔεΛλΠϓώϯτ͢Δͱ // BltΫϥε͕ೖ͞ΕΔ $this->app->bind(Burger::class, Blt::class); }
ಥવͷϦΫΤετ ʮԶ#-5͡Όͳͯ͘ςϦϠΩόʔΨʔ͕৯͍ͨʯ
ʮطଘίʔυΛॻ͖͑ͣʹ ཁʹ͍͑ͨʯ
ͦ͏ͩʂநͩʂ w ͜ͷ࣌ɺٻΊΒΕͯΔͷ#VSHFSΠϯλʔ ϑΣʔεΛ࣮ͨ͠৽ͨͳόʔΨʔ w ͭ·Γ෦తͳ࣮͕Ͳ͏มΘΖ͏ͱͦͷந Λ࣮͢Εࠩ͠ସ͕͑Մೳ
/FX$MBTT interface Burger { public function addTopping($toppingName); } class Blt
implements Burger { public function addTopping($toppingName) { // } } class Teriyaki implements Burger { public function addTopping($toppingName) { // } }
$IBOHF$MBTT#JOEJOH /* in DI Container */ public function boot() {
// ݩʑͷΫϥε // $this->app->bind(Burger::class, Blt::class); // ৽ࠩ͘͠͠ସ͑ΔΫϥεΛBinding $this->app->bind(Burger::class, Teriyaki::class); }
.JTTJPO$PNQMFUF 㱼ʆ w நͱ%*ίϯςφΛ͏·͘ѻͬͯطଘίʔυʹ खΛೖΕͣʹ৽͍͠ίʔυʹࠩ͠ସ͑Δ͜ͱ͕ Մೳ w %*ίϯςφ͚ͩͷͰͳ͍ΜͰ͕͢ศརͩΑ ͱ͍͏ʜͶʜ
ͦͷ ςετ࣮
ྫ͑͜ΜͳςετΛॻ͖͍ͨ
501ϖʔδʹΞΫηεͨ͠Β ͪΌΜͱϖʔδ͕දࣔ͞ΕΔ͔
"QQMJDBUJPO5FTUJOH w %#ʹ͍߹Θͤͯฦ͖ͬͯͨͷ͕501ϖʔδ ʹग़Δͣʜ w ͖ͪΜͱɺ͔ͭ%#͔ΒͷϨεϙϯε͕ࡌͬ ͯΕ0,
"QQMJDBUJPO5FTUJOH /** * TOPϖʔδʹΞΫηεͨ͠Β’username'ͷจࣈྻ͕͋Γɺ * HTTPεςʔλείʔυ͕200Ͱ͋Δ͜ͱ */ public function testAccessTopPage()
{ $this->visit('/top') ->see(‘username') ->assertResponseOk(); }
Έ w ςετέʔε࣮ߦ͞ΕΔʹ%#ΞΫηε͢Δͷ πϥΠ w ޭ༻ࣦഊ༻ͷσʔλΛέʔε͝ͱʹ༻ҙ͢Δ ͷπϥΠ w ςετલޙͰ%#ͷ͓আͱ͔πϥΠ
6TF%*$POUBJOFS ͦΕɺ%*ίϯςφͰղܾͰ͖·͢Α
$MBTT#JOEJOH w σʔλʹΞΫηεͯ͠ΔΫϥε%*ίϯςφ͔ Βೖ͞Ε͍ͯΔ w ςετத͚ͩσʔλΞΫηεΫϥεʹߜͬͯϞο ΫΫϥεʹ#JOEJOHͯ͠͠·͓̺͑
.BLJOH.PDL$MBTT // σʔλΞΫηεΫϥεͷϞοΫ࡞ $userMock = \Mockery::mock(\User::class); $userMock->shouldReceive(‘getUsers')->andReturn('username');
#JOEJOH.PDL$MBTT // ϞοΫΫϥεΛBinding $this->app->bind(\User::class, function () use ($userMock) { return
$userMock; });
5FTUJOH public function testAccessTopPage() { // σʔλΞΫηεΫϥεͷϞοΫ࡞ $userMock = \Mockery::mock(\User::class);
$userMock->shouldReceive('getUsers')->andReturn('username'); // ϞοΫΫϥεΛBinding $this->app->bind(\User::class, function () use ($userMock) { return $userMock; }); // ςετ࣮ߦ $this->visit('/top') ->see('username') ->assertResponseOk(); }
.JTTJPO$PNQMFUF 㶅㱿㶅 w ष͍ͷʹ֖ςετͷతΛՌͨͨ͢ΊʹΞϓ ϦέʔγϣϯͷҰ෦͚ͩϞοΫͰࠩ͠ସ͑Δ ͜ͱ͕Ͱ͖ͨʂ w ΞΫηεςετͰͳ͍ͯ͘Ζ͍Ζͳͱ͜ΖͰ Ԡ༻Մೳ w
"1*ΞΫηεͷϞοΫͱ͔
·ͱΊ w %*ίϯςφΫϥεͷґଘ͕ղܾ͞ΕͨΠϯε λϯεΛฦͯ͘͠ΕΔຐ๏ͷശ w ͏·͘པΕςετ࣮ͷࠩ͠ସ͕͑ͱͯ ؆୯@ ᵇʮЏ @ w
ΠέͯΔ%*ίϯςφϥΠϒϥϦҰഋ͋ΔͷͰ ֤ࣗ(PPHMFઌੜʂ
Ҏ্ʂ