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.8k
実践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.1k
20220926_セキュリティチームの今_for_Drs._Prime_公開用.pdf
sota1235
0
75
再発防止策を考える技術 / #phpconsen
sota1235
10
3.7k
How to choose the best npm module for your team?
sota1235
9
550
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.2k
Other Decks in Technology
See All in Technology
複雑なState管理からの脱却
sansantech
PRO
1
140
ISUCONに強くなるかもしれない日々の過ごしかた/Findy ISUCON 2024-11-14
fujiwara3
8
870
TypeScriptの次なる大進化なるか!? 条件型を返り値とする関数の型推論
uhyo
2
1.6k
マルチモーダル / AI Agent / LLMOps 3つの技術トレンドで理解するLLMの今後の展望
hirosatogamo
37
12k
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
170
ドメインの本質を掴む / Get the essence of the domain
sinsoku
2
150
Oracle Cloud Infrastructureデータベース・クラウド:各バージョンのサポート期間
oracle4engineer
PRO
28
12k
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
隣接領域をBeyondするFinatextのエンジニア組織設計 / beyond-engineering-areas
stajima
1
270
Lambda10周年!Lambdaは何をもたらしたか
smt7174
2
110
【若手エンジニア応援LT会】ソフトウェアを学んできた私がインフラエンジニアを目指した理由
kazushi_ohata
0
150
Python(PYNQ)がテーマのAMD主催のFPGAコンテストに参加してきた
iotengineer22
0
470
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
33
2.9k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Building an army of robots
kneath
302
43k
The Invisible Side of Design
smashingmag
298
50k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
How GitHub (no longer) Works
holman
310
140k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
KATA
mclloyd
29
14k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
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ઌੜʂ
Ҏ্ʂ