StreamWrapper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Psr7;
  4. use Psr\Http\Message\StreamInterface;
  5. /**
  6. * Converts Guzzle streams into PHP stream resources.
  7. *
  8. * @see https://www.php.net/streamwrapper
  9. */
  10. final class StreamWrapper
  11. {
  12. /** @var resource */
  13. public $context;
  14. /** @var StreamInterface */
  15. private $stream;
  16. /** @var string r, r+, or w */
  17. private $mode;
  18. /**
  19. * Returns a resource representing the stream.
  20. *
  21. * @param StreamInterface $stream The stream to get a resource for
  22. *
  23. * @return resource
  24. *
  25. * @throws \InvalidArgumentException if stream is not readable or writable
  26. */
  27. public static function getResource(StreamInterface $stream)
  28. {
  29. self::register();
  30. if ($stream->isReadable()) {
  31. $mode = $stream->isWritable() ? 'r+' : 'r';
  32. } elseif ($stream->isWritable()) {
  33. $mode = 'w';
  34. } else {
  35. throw new \InvalidArgumentException('The stream must be readable, '
  36. .'writable, or both.');
  37. }
  38. return fopen('guzzle://stream', $mode, false, self::createStreamContext($stream));
  39. }
  40. /**
  41. * Creates a stream context that can be used to open a stream as a php stream resource.
  42. *
  43. * @return resource
  44. */
  45. public static function createStreamContext(StreamInterface $stream)
  46. {
  47. return stream_context_create([
  48. 'guzzle' => ['stream' => $stream],
  49. ]);
  50. }
  51. /**
  52. * Registers the stream wrapper if needed
  53. */
  54. public static function register(): void
  55. {
  56. if (!in_array('guzzle', stream_get_wrappers())) {
  57. stream_wrapper_register('guzzle', __CLASS__);
  58. }
  59. }
  60. public function stream_open(string $path, string $mode, int $options, string &$opened_path = null): bool
  61. {
  62. $options = stream_context_get_options($this->context);
  63. if (!isset($options['guzzle']['stream'])) {
  64. return false;
  65. }
  66. $this->mode = $mode;
  67. $this->stream = $options['guzzle']['stream'];
  68. return true;
  69. }
  70. public function stream_read(int $count): string
  71. {
  72. return $this->stream->read($count);
  73. }
  74. public function stream_write(string $data): int
  75. {
  76. return $this->stream->write($data);
  77. }
  78. public function stream_tell(): int
  79. {
  80. return $this->stream->tell();
  81. }
  82. public function stream_eof(): bool
  83. {
  84. return $this->stream->eof();
  85. }
  86. public function stream_seek(int $offset, int $whence): bool
  87. {
  88. $this->stream->seek($offset, $whence);
  89. return true;
  90. }
  91. /**
  92. * @return resource|false
  93. */
  94. public function stream_cast(int $cast_as)
  95. {
  96. $stream = clone $this->stream;
  97. $resource = $stream->detach();
  98. return $resource ?? false;
  99. }
  100. /**
  101. * @return array{
  102. * dev: int,
  103. * ino: int,
  104. * mode: int,
  105. * nlink: int,
  106. * uid: int,
  107. * gid: int,
  108. * rdev: int,
  109. * size: int,
  110. * atime: int,
  111. * mtime: int,
  112. * ctime: int,
  113. * blksize: int,
  114. * blocks: int
  115. * }
  116. */
  117. public function stream_stat(): array
  118. {
  119. static $modeMap = [
  120. 'r' => 33060,
  121. 'rb' => 33060,
  122. 'r+' => 33206,
  123. 'w' => 33188,
  124. 'wb' => 33188,
  125. ];
  126. return [
  127. 'dev' => 0,
  128. 'ino' => 0,
  129. 'mode' => $modeMap[$this->mode],
  130. 'nlink' => 0,
  131. 'uid' => 0,
  132. 'gid' => 0,
  133. 'rdev' => 0,
  134. 'size' => $this->stream->getSize() ?: 0,
  135. 'atime' => 0,
  136. 'mtime' => 0,
  137. 'ctime' => 0,
  138. 'blksize' => 0,
  139. 'blocks' => 0,
  140. ];
  141. }
  142. /**
  143. * @return array{
  144. * dev: int,
  145. * ino: int,
  146. * mode: int,
  147. * nlink: int,
  148. * uid: int,
  149. * gid: int,
  150. * rdev: int,
  151. * size: int,
  152. * atime: int,
  153. * mtime: int,
  154. * ctime: int,
  155. * blksize: int,
  156. * blocks: int
  157. * }
  158. */
  159. public function url_stat(string $path, int $flags): array
  160. {
  161. return [
  162. 'dev' => 0,
  163. 'ino' => 0,
  164. 'mode' => 0,
  165. 'nlink' => 0,
  166. 'uid' => 0,
  167. 'gid' => 0,
  168. 'rdev' => 0,
  169. 'size' => 0,
  170. 'atime' => 0,
  171. 'mtime' => 0,
  172. 'ctime' => 0,
  173. 'blksize' => 0,
  174. 'blocks' => 0,
  175. ];
  176. }
  177. }