Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
DIとLaravel
Search
polidog
November 23, 2018
Programming
480
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
DIとLaravel
polidog
November 23, 2018
Transcript
30 slides
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?
࣮ࡍʹΦϒδΣΫτΛੜ͢ Δ
?>
More Decks by polidog
See All by polidog
omasushiというライブラリを作った
polidog
0
660
RelayerというPHPのフレームワークを作った
polidog
0
230
AIがコードを書く時代の ジェネレーティブプログラミング
polidog
3
1.5k
小さな開発会社を作った理由(再)
polidog
0
350
React Server ComponentsでAPI不要の開発体験
polidog
1
1k
小さな開発会社が受託開発に力を入れる理由
polidog
0
610
小さな開発会社を作った理由
polidog
1
3.8k
小さな開発会社がWebサービスを作る理由
polidog
1
920
トランザクションスクリプトは何がダメなのか?
polidog
2
4.2k
Other Decks in Programming
See All in Programming
テストだけで見つかる? Rubyでつくるメソッド発掘器
izawa
0
130
ビジネスロジックを堅牢に~Scala 3のドメインプリミティブ~
j5ik2o
0
200
止まらない比較を止める — 余帰納法で無限ストリームを比べる
tarao
0
190
우리의 Nitro Modules는 당신의 Turbo Modules보다 아름답다
l2hyunwoo
0
270
LLM 101: Exploring LLMs, Starting with Tokens.
dalinaum
0
110
疎結合とは何か?ソフトウェア設計における結合バランスについて考える #phpcon_niigata
kajitack
2
330
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
200
はじめてのPLATEAU
nokonoko1203
1
660
AI時代のシニアエンジニアとは
yosuke_furukawa
PRO
15
12k
Scalaの型でマルチテナントの認可を守る
nextbeatdev
0
160
React Native에 현대적인 빌드 도구 더하기: Rollipop 구현기
leegeunhyeok
0
180
将来の標準フォーマッター?ktfmtを深堀る
t45k
0
220
Featured
See All Featured
The agentic SEO stack - context over prompts
schlessera
0
1k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
430
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.9k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
380
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.9k
Marketing to machines
jonoalderson
1
6k
Crafting Experiences
bethany
1
440
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
We Are The Robots
honzajavorek
0
450
Building Better People: How to give real-time feedback that sticks.
wjessup
371
20k
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
300