PHPのstreamで遊ぶ
by
Hiraku NAKANO
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
STREAMͰ༡Ϳ PHPBLT#4
Slide 2
Slide 2 text
͓લͩΕΑʁ Hiraku ▸ composerʹจ۟ݴͬͯΔਓ ▸ ޫ͕͍ͱจ۟ݴͬͯΔਓ ▸ PHPerྺ8͙Β͍
Slide 3
Slide 3 text
STREAM ҙࣝͯ͠Δʁ
Slide 4
Slide 4 text
PHPʹ͓͚Δ STREAM
Slide 5
Slide 5 text
fopen, fclose, freadͳͲ CݴޠͰॻ͍ͯ͋Δ෦ SplFileObject, file_get_contents ετϦʔϜ (file://, php://, http://ͳͲ) ਂ PHPer͕ීஈ ৮ͬͯΔॴ ΩϥΩϥͨ͠ ͭ
Slide 6
Slide 6 text
͏ͪΐͬͱਅ໘ͳઆ໌ PHPͷSTREAMͱ ▸ UNIXͷϑΝΠϧهड़ࢠ(FD)ʹ૬͢ΔPHPػೳ ▸ resourceܕͷҰछ ▸ is_resource($fp) === true ▸ get_resource_type($fp) === 'stream' ▸ fopen()ͰऔΕΔ΄͔ɺSTDOUTSTDIN͜ͷؒ
Slide 7
Slide 7 text
ίʔυͰݴ͑
Slide 8
Slide 8 text
৭ʑछྨ͕͋Δ ▸ ϑΝΠϧύεͷઌ಄ͷ"file:// ..."ͷ෦Ͱذ͍ͯ͠Δ ▸ লུ͢Δͱfile://ͷҙຯʹͳΔ ▸ http:// ͱ͔ php:// ͱ͔ data:// ͋ΔΑ
Slide 9
Slide 9 text
ΈΜͳҙࣝͤͣʹ ͍ͬͯΔͣʂ
Slide 10
Slide 10 text
PHPͷ"ετϦʔϜ"͕࣋ͭ ػೳ ϑΝΠϧγεςϜ ΛநԽͨ͠ͷ ڊେͳσʔλΛѻ͑Δ ௨৴ΛநԽ ฒߦॲཧ
Slide 11
Slide 11 text
໊લΑ͘ͳ͍ΑͶɻɻ ▸ Node.jsͷstreamsͱҧ͏ ▸ σΟϨΫτϦͷ֓೦͕͋Δ ▸ ύΠϓͱ͔ιέοτͱ͔ ▸ ඇಉظ͚ͨ͠ΕࣗͰॻ͘ ▸ όΠφϦྻ͔͠ѻ͑ͳ͍
Slide 12
Slide 12 text
STREAMͰ༡Ϳ
Slide 13
Slide 13 text
fopen, fclose, freadͳͲ CݴޠͰॻ͍ͯ͋Δ෦ SplFileObject, file_get_contents ετϦʔϜ (file://, php://, http://ͳͲ) ਂ PHPer͕ීஈ ৮ͬͯΔॴ ΩϥΩϥͨ͠ ͭ
Slide 14
Slide 14 text
=حົͳ͜ͱ͕Ͱ͖Δ ਂʹ͍ۙͱ͍͏͜ͱ
Slide 15
Slide 15 text
STREAMͰ༡Ϳ ωλ1
Slide 16
Slide 16 text
͝ଘͰ͠ΐ͏͔ ΈࠐΈͷstreamফͤΔ ▸ stream_wrapper_unregisterؔ ▸ Ϋϥεؔ(ී௨)ফͤͳ͍͚Ͳɺ streamඪ४ػೳͰফͤΔ PHPͰػೳΛ ফͤΔͷΊͣΒ͍͠
Slide 17
Slide 17 text
ͼͬ͘Γ͢Δ΄ͲPHP͕ʹཱͨͳ͘ͳΔtips stream_wrapper_unregister('file') ▸ file://ͷొΛফ͢ ▸ require 'hoge.php'; Ͱ͖ͳ͘ͳΔ ▸ file_get_contents('hoge.php'); Ͱ͖ͳ͘ͳΔ ԿͰ͖ͳ͍ʂ
Slide 18
Slide 18 text
ͬͯΈΔ $ 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
Slide 19
Slide 19 text
STREAMͰ༡Ϳ ωλ2
Slide 20
Slide 20 text
͝ଘͰ͠ΐ͏͔ ফͨ͠streamࣗ࡞൛ʹஔ͖͑ΒΕΔ ▸ stream_wrapper_registerؔ ▸ streamWrapperͬΆ͍ΫϥεΛPHPͰ࣮ͯ͠ొ ▸ http://php.net/manual/ja/class.streamwrapper.php streamͰͷϞοΫԽ
Slide 21
Slide 21 text
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');
Slide 22
Slide 22 text
php > require 's1.php'; php > require 's1.php'; うるせーバーカ php > require 's'; うるせーバーカ php > echo file_get_contents('hoge'); うるせーバーカ ἤͬͯ͘ΔPHP͕Ͱ͖ͨ
Slide 23
Slide 23 text
STREAMͷϞοΫԽʂʂͬͯॻ͍͚ͨͲ ▸ requireͱ͔ͷΫϥεಡΈࠐΈ·ͰϞοΫԽ͞ΕͪΌ͏ ▸ ݩͷfile://͕͑ͳ͘ͳΔ ▸ ͭΑ͗ͯ͢ʹཱͨͳ͍…
Slide 24
Slide 24 text
STREAMͰ༡Ϳ ωλ3
Slide 25
Slide 25 text
PHP PREPROCESSOR
Slide 26
Slide 26 text
ωλͩͬͨΜͰ͚͢Ͳ ͓ ▸ setter/getterΛ͘ॻ͖͍ͨ... ▸ ϚδοΫϝιουνϟϥ͍͔Βݏ
Slide 27
Slide 27 text
ϚΫϩͬΆ͍ͷΛ࡞Δ ▸ PHPͷίʔυΛు͘PHPͷίʔυΛॻ͘ ▸ 1ճ࣮ߦͨ݁͠ՌΛrequire/file_get_contentsʹ͢ ▸ ͱ͍͏ͷΛstreamWrapperͰ࡞Δ
Slide 28
Slide 28 text
σϞ
Slide 29
Slide 29 text
STREAM ໘ന͍Αʂ