StreamWrapper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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<int|string, int>
  102. */
  103. public function stream_stat(): array
  104. {
  105. static $modeMap = [
  106. 'r' => 33060,
  107. 'rb' => 33060,
  108. 'r+' => 33206,
  109. 'w' => 33188,
  110. 'wb' => 33188
  111. ];
  112. return [
  113. 'dev' => 0,
  114. 'ino' => 0,
  115. 'mode' => $modeMap[$this->mode],
  116. 'nlink' => 0,
  117. 'uid' => 0,
  118. 'gid' => 0,
  119. 'rdev' => 0,
  120. 'size' => $this->stream->getSize() ?: 0,
  121. 'atime' => 0,
  122. 'mtime' => 0,
  123. 'ctime' => 0,
  124. 'blksize' => 0,
  125. 'blocks' => 0
  126. ];
  127. }
  128. /**
  129. * @return array<int|string, int>
  130. */
  131. public function url_stat(string $path, int $flags): array
  132. {
  133. return [
  134. 'dev' => 0,
  135. 'ino' => 0,
  136. 'mode' => 0,
  137. 'nlink' => 0,
  138. 'uid' => 0,
  139. 'gid' => 0,
  140. 'rdev' => 0,
  141. 'size' => 0,
  142. 'atime' => 0,
  143. 'mtime' => 0,
  144. 'ctime' => 0,
  145. 'blksize' => 0,
  146. 'blocks' => 0
  147. ];
  148. }
  149. }