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
PHPのstreamで遊ぶ
Search
Hiraku NAKANO
April 21, 2016
Technology
5.1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PHPのstreamで遊ぶ
#phpblt 4回目で話したネタです
Hiraku NAKANO
April 21, 2016
More Decks by Hiraku NAKANO
See All by Hiraku NAKANO
iD決済を支える技術 / #merpay_techtalk
hirak
33
7.3k
github.com/mercari の今と昔 /github-mercari
hirak
8
3.2k
PHPStanで始める継続的静的解析 #phperkaigi /php-static-analysis
hirak
14
55k
極める!メソッドチェーン #phpstudy / fluent-interface
hirak
8
3.1k
evalこそパワー #phpkansai / phpkansai2017
hirak
2
1.8k
PHPのエラーと例外再入門 / php-error-and-exception
hirak
19
20k
メルカリ JP Tech Side / mercari-jp-tech-side
hirak
7
2.1k
グローバル展開を支える量子的なサービス設計 #mercariday / mercariday2017-api
hirak
22
14k
Composerプラグインを作ってみよう /phpcon2016
hirak
5
14k
Other Decks in Technology
See All in Technology
reInvent2026stby LT00 "Change your life!"
shinichirokawano
1
480
生き様としてのSRE
hokkai7go
0
140
SLOを阻む不安
sraku2159
0
130
渡米前の準備を制す!re:Invent 2026徹底準備術 / The Ultimate Preparation Guide for re:Invent 2026
yuj1osm
1
440
2拠点SRE活動始めてみた | sogaoh's LT @ ホンマでっかSRE勉強会 #3
sogaoh
PRO
0
210
AIエージェント時代、Kaggleにどう取り組むか
takanao
2
410
自律的なチームを作るためにTechPMがやらないこと
ny7760
0
160
AIコーディングエージェント開発_全体像
happysamurai294
0
130
基本から分かる!AWSセキュリティ運用の考え方と任せ方
cmusudakeisuke
1
260
Oracle Database Security 詳細解説
oracle4engineer
PRO
1
160
(swiftc) NonCopyableとライフタイム修飾子
teamhimeh
0
160
AIで選択肢が広がった組織設計の理想と現実
konifar
16
7.1k
Featured
See All Featured
Faster Mobile Websites
deanohume
310
32k
Marketing to machines
jonoalderson
1
5.9k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
Thoughts on Productivity
jonyablonski
76
5.4k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
Code Review Best Practice
trishagee
74
20k
Rails Girls Zürich Keynote
gr2m
96
14k
For a Future-Friendly Web
brad_frost
184
11k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
43k
Transcript
STREAMͰ༡Ϳ PHPBLT#4
͓લͩΕΑʁ Hiraku ▸ composerʹจ۟ݴͬͯΔਓ ▸ ޫ͕͍ͱจ۟ݴͬͯΔਓ ▸ PHPerྺ8͙Β͍
STREAM ҙࣝͯ͠Δʁ
PHPʹ͓͚Δ STREAM
fopen, fclose, freadͳͲ CݴޠͰॻ͍ͯ͋Δ෦ SplFileObject, file_get_contents ετϦʔϜ (file://, php://, http://ͳͲ)
ਂ PHPer͕ීஈ ৮ͬͯΔॴ ΩϥΩϥͨ͠ ͭ
͏ͪΐͬͱਅ໘ͳઆ໌ PHPͷSTREAMͱ ▸ UNIXͷϑΝΠϧهड़ࢠ(FD)ʹ૬͢ΔPHPػೳ ▸ resourceܕͷҰछ ▸ is_resource($fp) === true
▸ get_resource_type($fp) === 'stream' ▸ fopen()ͰऔΕΔ΄͔ɺSTDOUTSTDIN͜ͷؒ
ίʔυͰݴ͑ <?php $fs = fopen('hoge.txt', 'r'); //↑これがストリームリソース $file = new
SplFileObject('hoge.txt'); //↑内部的にはストリームリソースが存在している
৭ʑछྨ͕͋Δ ▸ ϑΝΠϧύεͷઌ಄ͷ"file:// ..."ͷ෦Ͱذ͍ͯ͠Δ ▸ লུ͢Δͱfile://ͷҙຯʹͳΔ ▸ http:// ͱ͔ php://
ͱ͔ data:// ͋ΔΑ
ΈΜͳҙࣝͤͣʹ ͍ͬͯΔͣʂ
PHPͷ"ετϦʔϜ"͕࣋ͭ ػೳ ϑΝΠϧγεςϜ ΛநԽͨ͠ͷ ڊେͳσʔλΛѻ͑Δ ௨৴ΛநԽ ฒߦॲཧ
໊લΑ͘ͳ͍ΑͶɻɻ ▸ Node.jsͷstreamsͱҧ͏ ▸ σΟϨΫτϦͷ֓೦͕͋Δ ▸ ύΠϓͱ͔ιέοτͱ͔ ▸ ඇಉظ͚ͨ͠ΕࣗͰॻ͘ ▸
όΠφϦྻ͔͠ѻ͑ͳ͍
STREAMͰ༡Ϳ
fopen, fclose, freadͳͲ CݴޠͰॻ͍ͯ͋Δ෦ SplFileObject, file_get_contents ετϦʔϜ (file://, php://, http://ͳͲ)
ਂ PHPer͕ීஈ ৮ͬͯΔॴ ΩϥΩϥͨ͠ ͭ
=حົͳ͜ͱ͕Ͱ͖Δ ਂʹ͍ۙͱ͍͏͜ͱ
STREAMͰ༡Ϳ ωλ1
͝ଘͰ͠ΐ͏͔ ΈࠐΈͷstreamফͤΔ ▸ stream_wrapper_unregisterؔ ▸ Ϋϥεؔ(ී௨)ফͤͳ͍͚Ͳɺ streamඪ४ػೳͰফͤΔ PHPͰػೳΛ ফͤΔͷΊͣΒ͍͠
ͼͬ͘Γ͢Δ΄ͲPHP͕ʹཱͨͳ͘ͳΔtips stream_wrapper_unregister('file') ▸ file://ͷొΛফ͢ ▸ require 'hoge.php'; Ͱ͖ͳ͘ͳΔ ▸ file_get_contents('hoge.php');
Ͱ͖ͳ͘ͳΔ ԿͰ͖ͳ͍ʂ
ͬͯΈΔ $ php -a Interactive shell php > stream_wrapper_unregister('file'); php
> echo file_get_contents('hoge.txt'); PHP Warning: file_get_contents(): file:// wrapper is disabled in the server configuration in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. file_get_contents() php shell code:1
STREAMͰ༡Ϳ ωλ2
͝ଘͰ͠ΐ͏͔ ফͨ͠streamࣗ࡞൛ʹஔ͖͑ΒΕΔ ▸ stream_wrapper_registerؔ ▸ streamWrapperͬΆ͍ΫϥεΛPHPͰ࣮ͯ͠ొ ▸ http://php.net/manual/ja/class.streamwrapper.php streamͰͷϞοΫԽ
<?php class Aori { private $finished = false; function stream_open($path,
$mode, $options, &$opened_path) { return true; } function stream_read(int $count) { if ($this->finished) { return false; } $this->finished = true; return 'うるせーバーカ'; } function stream_eof() { return $this->finished; } function stream_stat() { return []; } } stream_wrapper_unregister('file'); stream_wrapper_register('file', 'Aori');
php > require 's1.php'; php > require 's1.php'; うるせーバーカ php
> require 's'; うるせーバーカ php > echo file_get_contents('hoge'); うるせーバーカ ἤͬͯ͘ΔPHP͕Ͱ͖ͨ
STREAMͷϞοΫԽʂʂͬͯॻ͍͚ͨͲ ▸ requireͱ͔ͷΫϥεಡΈࠐΈ·ͰϞοΫԽ͞ΕͪΌ͏ ▸ ݩͷfile://͕͑ͳ͘ͳΔ ▸ ͭΑ͗ͯ͢ʹཱͨͳ͍…
STREAMͰ༡Ϳ ωλ3
PHP PREPROCESSOR
ωλͩͬͨΜͰ͚͢Ͳ ͓ ▸ setter/getterΛ͘ॻ͖͍ͨ... ▸ ϚδοΫϝιουνϟϥ͍͔Βݏ
ϚΫϩͬΆ͍ͷΛ࡞Δ ▸ PHPͷίʔυΛు͘PHPͷίʔυΛॻ͘ ▸ 1ճ࣮ߦͨ݁͠ՌΛrequire/file_get_contentsʹ͢ ▸ ͱ͍͏ͷΛstreamWrapperͰ࡞Δ
σϞ
STREAM ໘ന͍Αʂ