PHP Streams

1,520 views

Published on

From basic to intermediate with some advanced techniques on working with streams in php.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,520
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
17
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

PHP Streams

  1. 1. It’s all streams these daysFriday, May 18, 12
  2. 2. GWoo • PHP developer since 1999 • CakePHP 2005-2009 • Lithium 2009-present • Engineer on Orchestra.io @ EngineYard • On a boatFriday, May 18, 12
  3. 3. Basics http://php.net/manual/en/book.stream.phpFriday, May 18, 12
  4. 4. Input / Output • include/require, and *_once variants • fopen, fread, fgets, fgetcsv, fputcsv, fwrite, fclose, feof, etc • file_get_contents, file_put_contents, readfile, file • mkdir, rename, unlink • EVERYTHING!Friday, May 18, 12
  5. 5. Layers • Transports • Wrappers • Contexts • FiltersFriday, May 18, 12
  6. 6. ALL Input/Ouput PHP Streams Layer Stream Filters Stream Stream Context Wrapper Stream TransportFriday, May 18, 12
  7. 7. Transports http://php.net/manual/en/transports.phpFriday, May 18, 12
  8. 8. Transports • socket clients • “stream_set_blocking” non-blocking ioFriday, May 18, 12
  9. 9. Transports ~: php -r "print_r(stream_get_transports());" Array ( [0] => tcp [1] => udp [2] => unix [3] => udg [4] => ssl [5] => sslv3 [6] => sslv2 [7] => tls )Friday, May 18, 12
  10. 10. Example ~: php -r $fp = stream_socket_client("tcp://php.net:80", $errno, $errstr); > fwrite($fp, "HEAD / HTTP/1.0rnHost: php.netrnAccept: */*rnrn"); > print_r(stream_get_contents($fp)); HTTP/1.1 200 OK Date: Fri, 18 May 2012 07:12:38 GMT Server: Apache/1.3.41 (Unix) PHP/5.2.17 X-Powered-By: PHP/5.2.17 Content-language: en Set-Cookie: COUNTRY=ITA%2C85.42.99.82; expires=Fri, 25-May-2012 07:12:38 GMT; path=/; domain=.php.net Last-Modified: Fri, 18 May 2012 10:20:11 GMT Connection: close Content-Type: text/html;charset=utf-8Friday, May 18, 12
  11. 11. Wrappers http://php.net/manual/en/wrappers.phpFriday, May 18, 12
  12. 12. Wrappers ~: php -r "print_r(stream_get_wrappers());" Array ( [0] => https [1] => ftps [2] => compress.zlib [3] => compress.bzip2 [4] => php [5] => file [6] => glob [7] => data [8] => http [9] => ftp [10] => zip [11] => phar )Friday, May 18, 12
  13. 13. Example ~: php -r $fp = fopen("php://temp", "w+"); > fwrite($fp, "hello world"); > rewind($fp); > print_r(stream_get_contents($fp)); hello worldFriday, May 18, 12
  14. 14. Example ~: php -r $fp = fopen("php://stdout", "w"); > fwrite($fp, "hello world"); hello worldFriday, May 18, 12
  15. 15. Contexts http://php.net/manual/en/context.phpFriday, May 18, 12
  16. 16. Contexts • A context is a set of parameters and wrapper specific options that modify or enhance the behavior of streams.Friday, May 18, 12
  17. 17. Contexts ~: php -r $http = array("method" => "HEAD"); > readfile("http://php.net", false, stream_context_create(compact("http"))); > print_r($http_response_header); Array ( [0] => HTTP/1.1 200 OK [1] => Date: Fri, 18 May 2012 07:25:32 GMT [2] => Server: Apache/1.3.41 (Unix) PHP/5.2.17 [3] => X-Powered-By: PHP/5.2.17 [4] => Content-language: en [5] => Set-Cookie: COUNTRY=ITA%2C85.42.99.82; expires=Fri, 25- May-2012 07:25:34 GMT; path=/; domain=.php.net [6] => Last-Modified: Fri, 18 May 2012 10:20:11 GMT [7] => Connection: close [8] => Content-Type: text/html;charset=utf-8 )Friday, May 18, 12
  18. 18. Filters http://php.net/manual/en/filters.phpFriday, May 18, 12
  19. 19. Filters • modify data while reading and/or writing • appended and removed anytime • chain filters as neededFriday, May 18, 12
  20. 20. Default Filters ~: php -r "print_r(stream_get_filters());" Array ( [0] => zlib.* [1] => bzip2.* [2] => convert.iconv.* [3] => string.rot13 [4] => string.toupper [5] => string.tolower [6] => string.strip_tags [7] => convert.* [8] => consumed [9] => dechunk [10] => http.* )Friday, May 18, 12
  21. 21. Example $stream = fopen(data://text/plain, . $body, r); stream_filter_append($stream, dechunk); return trim(stream_get_contents($stream));Friday, May 18, 12
  22. 22. Custom Wrappers http://php.net/manual/en/class.streamwrapper.phpFriday, May 18, 12
  23. 23. streamWrapper { /* Properties */ public resource $context ; /* Methods */ __construct ( void ) __destruct ( void ) public bool dir_closedir ( void ) public bool dir_opendir ( string $path , int $options ) public string dir_readdir ( void ) public bool dir_rewinddir ( void ) public bool mkdir ( string $path , int $mode , int $options ) public bool rename ( string $path_from , string $path_to ) public bool rmdir ( string $path , int $options ) public resource stream_cast ( int $cast_as ) public void stream_close ( void ) public bool stream_eof ( void ) public bool stream_flush ( void ) public bool stream_lock ( mode $operation ) public bool stream_metadata ( int $path , int $option , int $var ) public bool stream_open ( string $path , string $mode , int $options , string &$opened_path ) public string stream_read ( int $count ) public bool stream_seek ( int $offset , int $whence = SEEK_SET ) public bool stream_set_option ( int $option , int $arg1 , int $arg2 ) public array stream_stat ( void ) public int stream_tell ( void ) public bool stream_truncate ( int $new_size ) public int stream_write ( string $data ) public bool unlink ( string $path ) public array url_stat ( string $path , int $flags ) }Friday, May 18, 12
  24. 24. interface Stream_Interface { public function stream_open($path,$mode,$options,&$opened_path); public function stream_close(); public function stream_read($count); public function stream_write($data); public function stream_eof(); public function stream_tell(); public function stream_seek($offset, $whence); public function stream_stat(); public function url_stat($path, $flags); }Friday, May 18, 12
  25. 25. class AutoEscapeStream implements Stream_Interface { }Friday, May 18, 12
  26. 26. class AutoEscapeStream implements Stream_Interface { protected $_position = 0; protected $_stats = array(); protected $_data = null; protected $_path = null; }Friday, May 18, 12
  27. 27. class AutoEscapeStream implements Stream_Interface { ... public function stream_open($path, $mode, $options, &$opened_path) { $path = str_replace(escape://, , $path); if (empty($path)) { return false; } $success = ($this->_data = file_get_contents($path)); $this->_stats = stat($path); if ($success === false) { return false; } $escEcho = /<?=s*$this->(.+?)s*;?s*?>/ms; $this->_data = preg_replace($escEcho, <?php echo $this->$1; ?>, $this->_data); $echo = /<?=s*(.+?)s*;?s*?>/ms; $this->_data = preg_replace($echo, <?php echo $h($1); ?>, $this->_data); return true; } public function stream_read($count) { $result = substr($this->_data, $this->_position, $count); $this->_position += strlen($result); return $result; } public function stream_tell() { return $this->_position; } public function stream_eof() { return ($this->_position >= strlen($this->_data)); }Friday, May 18, 12
  28. 28. class AutoEscapeStream implements Stream_Interface { ... public function stream_seek($offset, $whence) { switch ($whence) { case SEEK_SET: if ($offset < strlen($this->_data) && $offset >= 0) { $this->_position = $offset; return true; } return false; case SEEK_CUR: if ($offset >= 0) { $this->_position += $offset; return true; } return false; case SEEK_END: if (strlen($this->_data) + $offset >= 0) { $this->_position = strlen($this->_data) + $offset; return true; } return false; default: } return false; } public function stream_stat() { return $this->_stats; } public function url_stat() { return $this->_stats; }Friday, May 18, 12
  29. 29. Using the Wrapper stream_wrapper_register("escape", "AutoEscapeStream"); include("escape://template.php");Friday, May 18, 12
  30. 30. Demo • Using phar streams • https://github.com/gwoo/sinatriumFriday, May 18, 12
  31. 31. Questions?Friday, May 18, 12
  32. 32. @gwoo https://github.com/gwoo THANKS!Friday, May 18, 12

×