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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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.7k
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
生成AI×AWS CDK×AWS FISで"振り返れる"ミニGameDayをつくろう
yoshimi0227
2
510
Multicaで30個のミニプロジェクトをAIエージェント運用して見えてきたこと
eiei114
1
610
シンガポールで登壇してきます
yama3133
0
340
Playwright × AI Agent でE2Eテストはどう変わるか AI駆動テストの可能性と実用検証の結果
taiga7543
2
760
2年前に削除したPHPクラスが、 ある日突然決済をエラーにした
ykagano
1
720
Webアプリ認証の全体像 / The Big Picture of Web App Authentication
kitano_yuichi
1
410
AI x 開発生産性を取り巻く予算戦略と投資対効果
i35_267
7
2.8k
kaonavi Tech Night#1
kaonavi
0
140
AI時代におけるテストの基礎の再定義 / Rethinking the Fundamentals of Testing in the AI Era
mineo_matsuya
7
2.7k
副作用のある Lambda でも Lambda Power Tuning は使えるのか / lambda-power-tuning-side-effects
koukihosaka
1
130
プロダクト開発組織の現在地(Ver.2026/07) / product-organization
kaonavi
0
300
“それは自分の仕事じゃない"を越えて行け
yuukiyo
1
520
Featured
See All Featured
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
3
360
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
45k
Designing for Performance
lara
611
70k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
220
Building an army of robots
kneath
306
46k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
490
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
160
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.8k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
Tell your own story through comics
letsgokoyo
1
1k
Google's AI Overviews - The New Search
badams
0
1.1k
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 ໘ന͍Αʂ