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
5k
実践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.7k
20220926_セキュリティチームの今_for_Drs._Prime_公開用.pdf
sota1235
0
140
再発防止策を考える技術 / #phpconsen
sota1235
10
3.9k
How to choose the best npm module for your team?
sota1235
9
600
Realtime Database for high traffic production application
sota1235
7
4k
Road to migrate JP Web as a microservice
sota1235
4
1.6k
インターフェース再入門 / Think Interface again
sota1235
6
11k
再発防止策を考える技術 #phpconfuk_rej
sota1235
1
1.2k
Update around Firebase #io18
sota1235
3
4.3k
Other Decks in Technology
See All in Technology
VPC Latticeのサービスエンドポイント機能を使用した複数VPCアクセス
duelist2020jp
0
180
LLM時代の検索とコンテキストエンジニアリング
shibuiwilliam
2
1.1k
人と組織に偏重したEMへのアンチテーゼ──なぜ、EMに設計力が必要なのか/An antithesis to the overemphasis of people and organizations in EM
dskst
5
580
GCASアップデート(202506-202508)
techniczna
0
250
モダンフロントエンド 開発研修
recruitengineers
PRO
2
200
Webアクセシビリティ入門
recruitengineers
PRO
1
160
ソフトウェア エンジニアとしての 姿勢と心構え
recruitengineers
PRO
1
470
制約理論(ToC)入門
recruitengineers
PRO
2
190
あとはAIに任せて人間は自由に生きる
kentaro
3
1.1k
ZOZOTOWNフロントエンドにおけるディレクトリの分割戦略
zozotech
PRO
16
5.1k
Android Studio の 新しいAI機能を試してみよう / Try out the new AI features in Android Studio
yanzm
0
260
Browser
recruitengineers
PRO
3
200
Featured
See All Featured
A designer walks into a library…
pauljervisheath
207
24k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
131
19k
Making Projects Easy
brettharned
117
6.3k
Writing Fast Ruby
sferik
628
62k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.6k
GraphQLとの向き合い方2022年版
quramy
49
14k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
4 Signs Your Business is Dying
shpigford
184
22k
Optimizing for Happiness
mojombo
379
70k
Side Projects
sachag
455
43k
Gamification - CAS2011
davidbonilla
81
5.4k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
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ઌੜʂ
Ҏ্ʂ