GuzzleHttpPsr7StreamWrapper

Converts Guzzle streams into PHP stream resources.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Psr7/StreamWrapper.php  
  1. class StreamWrapper 
  2. /** @var resource */ 
  3. public $context; 
  4.  
  5. /** @var StreamInterface */ 
  6. private $stream; 
  7.  
  8. /** @var string r, r+, or w */ 
  9. private $mode; 
  10.  
  11. /** 
  12. * Returns a resource representing the stream. 
  13. * @param StreamInterface $stream The stream to get a resource for 
  14. * @return resource 
  15. * @throws \InvalidArgumentException if stream is not readable or writable 
  16. */ 
  17. public static function getResource(StreamInterface $stream) 
  18. self::register(); 
  19.  
  20. if ($stream->isReadable()) { 
  21. $mode = $stream->isWritable() ? 'r+' : 'r'; 
  22. } elseif ($stream->isWritable()) { 
  23. $mode = 'w'; 
  24. } else { 
  25. throw new \InvalidArgumentException('The stream must be readable, ' 
  26. . 'writable, or both.'); 
  27.  
  28. return fopen('guzzle://stream', $mode, null, stream_context_create([ 
  29. 'guzzle' => ['stream' => $stream] 
  30. ])); 
  31.  
  32. /** 
  33. * Registers the stream wrapper if needed 
  34. */ 
  35. public static function register() 
  36. if (!in_array('guzzle', stream_get_wrappers())) { 
  37. stream_wrapper_register('guzzle', __CLASS__); 
  38.  
  39. public function stream_open($path, $mode, $options, &$opened_path) 
  40. $options = stream_context_get_options($this->context); 
  41.  
  42. if (!isset($options['guzzle']['stream'])) { 
  43. return false; 
  44.  
  45. $this->mode = $mode; 
  46. $this->stream = $options['guzzle']['stream']; 
  47.  
  48. return true; 
  49.  
  50. public function stream_read($count) 
  51. return $this->stream->read($count); 
  52.  
  53. public function stream_write($data) 
  54. return (int) $this->stream->write($data); 
  55.  
  56. public function stream_tell() 
  57. return $this->stream->tell(); 
  58.  
  59. public function stream_eof() 
  60. return $this->stream->eof(); 
  61.  
  62. public function stream_seek($offset, $whence) 
  63. $this->stream->seek($offset, $whence); 
  64.  
  65. return true; 
  66.  
  67. public function stream_stat() 
  68. static $modeMap = [ 
  69. 'r' => 33060,  
  70. 'r+' => 33206,  
  71. 'w' => 33188 
  72. ]; 
  73.  
  74. return [ 
  75. 'dev' => 0,  
  76. 'ino' => 0,  
  77. 'mode' => $modeMap[$this->mode],  
  78. 'nlink' => 0,  
  79. 'uid' => 0,  
  80. 'gid' => 0,  
  81. 'rdev' => 0,  
  82. 'size' => $this->stream->getSize() ?: 0,  
  83. 'atime' => 0,  
  84. 'mtime' => 0,  
  85. 'ctime' => 0,  
  86. 'blksize' => 0,  
  87. 'blocks' => 0 
  88. ];