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
Transcript
29 slides
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 ໘ന͍Αʂ
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
Taking back control of your AI development
inesmontani
PRO
0
310
Webメール開発を大幅に変えたJMAPというゲームチェンジャー
tascript
1
460
コーディングできない僕にBobが舞い降りた
shumpei3
0
200
Well, that’s weird…
csswizardry
1
210
From vibe coding to reliable (agentic) development
jcabot
0
250
AI-Ready なデータ基盤ってなに? & 新卒未経験が、いま考えていること
imuta
0
1.1k
MCPでネットワーク障害調査を自動化する前に直すべき3つのこと
yama3133
1
670
20261119_AgentCore
takuyay0ne
2
290
2026-11-16 JAWS-UG コンテナ支部 #31 ゆるLT大会 ECR のライフサイクルポリシーを いい感じに設定したい / ECR Lifecycle policy
masasuzu
3
620
IVRy における AI 時代のデータプロダクトと事業開発
ivry_presentationmaterials
0
110
デザインシステムのフィードバックループをほぼ自動化した話
tongari07
0
160
【Oracle AI SPOTLIGHT ウェビナー】AI時代、狙われるデータと止められない業務をどう守るか?
oracle4engineer
PRO
1
140
Featured
See All Featured
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
2
450
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
2
4k
Google's AI Overviews - The New Search
badams
0
1.7k
What does AI have to do with Human Rights?
axbom
PRO
1
2.5k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.9k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Paper Plane
katiecoart
PRO
6
55k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.3k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7.1k
sira's awesome portfolio website redesign presentation
elsirapls
0
530
Navigating Weather and Climate Data
rabernat
0
600