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
PHPのstreamで遊ぶ
Search
Hiraku NAKANO
April 21, 2016
Technology
5k
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
54k
極める!メソッドチェーン #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
2k
グローバル展開を支える量子的なサービス設計 #mercariday / mercariday2017-api
hirak
22
14k
Composerプラグインを作ってみよう /phpcon2016
hirak
5
14k
Other Decks in Technology
See All in Technology
現場に行くだけでは足りない——プロダクトエンジニアが業務の流れを捉える観点と、その鍛え方
takumiengineering
0
320
Amazon S3 Tablesに全部任せてみた結果——コンパクション/スナップショット管理は本当に手放せるか
shigeruoda
0
410
生成AI時代の クレデンシャルとパーミッション設計
nrinetcom
PRO
4
1.6k
GoにおけるFFIのこれまでとこれから
goccy
1
380
Microsoft 365 Copilot chat -tekoälypalvelun tietosuojaongelmat
hponka
0
520
ペアプロの価値はコードを書くことだけじゃない
codmoninc
PRO
0
130
振り返りこそエンジニアの本領
negima
0
330
【Oracle AI Spotlight ウェビナー】AWSか、Azureか、Google Cloudか。その議論にオラクルを含める意義。
oracle4engineer
PRO
2
240
少人数データチームのDevin活用実践事例
runandy16
2
450
Nav2、Nav3 ... はたまた自作?〜 作って理解する Nav3 の設計意図 〜 / Nav2, Nav3 ... or Build Your Own? — Understanding Nav3's design intent by building it from scratch
yanzm
0
260
設計の世代交代を乗り越える、14年続くAndroidアプリの開発戦略
sansantech
PRO
1
150
[DroidKaigi 2026] Making UI specifications visible: Android UI development in the AI agent era supported by Compose Screenshot Testing and galleries
syarihu
0
600
Featured
See All Featured
Darren the Foodie - Storyboard
khoart
PRO
3
3.9k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.7k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
230
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.2k
Documentation Writing (for coders)
carmenintech
77
5.5k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
260
The SEO Collaboration Effect
kristinabergwall1
1
550
Joys of Absence: A Defence of Solitary Play
codingconduct
1
480
30 Presentation Tips
portentint
PRO
1
390
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Un-Boring Meetings
codingconduct
0
410
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 ໘ന͍Αʂ