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()ͰऔΕΔ΄͔ɺSTDOUT΍STDIN΋͜ͷ஥ؒ

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 ໘ന͍Αʂ