StreamWrapper.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpClient\Response;
  11. use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
  12. use Symfony\Contracts\HttpClient\HttpClientInterface;
  13. use Symfony\Contracts\HttpClient\ResponseInterface;
  14. /**
  15. * Allows turning ResponseInterface instances to PHP streams.
  16. *
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. class StreamWrapper
  20. {
  21. /** @var resource|string|null */
  22. public $context;
  23. /** @var HttpClientInterface */
  24. private $client;
  25. /** @var ResponseInterface */
  26. private $response;
  27. /** @var resource|null */
  28. private $content;
  29. /** @var resource|null */
  30. private $handle;
  31. private $blocking = true;
  32. private $timeout;
  33. private $eof = false;
  34. private $offset = 0;
  35. /**
  36. * Creates a PHP stream resource from a ResponseInterface.
  37. *
  38. * @return resource
  39. */
  40. public static function createResource(ResponseInterface $response, HttpClientInterface $client = null)
  41. {
  42. if ($response instanceof StreamableInterface) {
  43. $stack = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 2);
  44. if ($response !== ($stack[1]['object'] ?? null)) {
  45. return $response->toStream(false);
  46. }
  47. }
  48. if (null === $client && !method_exists($response, 'stream')) {
  49. throw new \InvalidArgumentException(sprintf('Providing a client to "%s()" is required when the response doesn\'t have any "stream()" method.', __CLASS__));
  50. }
  51. if (false === stream_wrapper_register('symfony', __CLASS__, \STREAM_IS_URL)) {
  52. throw new \RuntimeException(error_get_last()['message'] ?? 'Registering the "symfony" stream wrapper failed.');
  53. }
  54. try {
  55. $context = [
  56. 'client' => $client ?? $response,
  57. 'response' => $response,
  58. ];
  59. return fopen('symfony://'.$response->getInfo('url'), 'r', false, stream_context_create(['symfony' => $context])) ?: null;
  60. } finally {
  61. stream_wrapper_unregister('symfony');
  62. }
  63. }
  64. public function getResponse(): ResponseInterface
  65. {
  66. return $this->response;
  67. }
  68. /**
  69. * @param resource|callable|null $handle The resource handle that should be monitored when
  70. * stream_select() is used on the created stream
  71. * @param resource|null $content The seekable resource where the response body is buffered
  72. */
  73. public function bindHandles(&$handle, &$content): void
  74. {
  75. $this->handle = &$handle;
  76. $this->content = &$content;
  77. }
  78. public function stream_open(string $path, string $mode, int $options): bool
  79. {
  80. if ('r' !== $mode) {
  81. if ($options & \STREAM_REPORT_ERRORS) {
  82. trigger_error(sprintf('Invalid mode "%s": only "r" is supported.', $mode), \E_USER_WARNING);
  83. }
  84. return false;
  85. }
  86. $context = stream_context_get_options($this->context)['symfony'] ?? null;
  87. $this->client = $context['client'] ?? null;
  88. $this->response = $context['response'] ?? null;
  89. $this->context = null;
  90. if (null !== $this->client && null !== $this->response) {
  91. return true;
  92. }
  93. if ($options & \STREAM_REPORT_ERRORS) {
  94. trigger_error('Missing options "client" or "response" in "symfony" stream context.', \E_USER_WARNING);
  95. }
  96. return false;
  97. }
  98. public function stream_read(int $count)
  99. {
  100. if (\is_resource($this->content)) {
  101. // Empty the internal activity list
  102. foreach ($this->client->stream([$this->response], 0) as $chunk) {
  103. try {
  104. if (!$chunk->isTimeout() && $chunk->isFirst()) {
  105. $this->response->getStatusCode(); // ignore 3/4/5xx
  106. }
  107. } catch (ExceptionInterface $e) {
  108. trigger_error($e->getMessage(), \E_USER_WARNING);
  109. return false;
  110. }
  111. }
  112. if (0 !== fseek($this->content, $this->offset)) {
  113. return false;
  114. }
  115. if ('' !== $data = fread($this->content, $count)) {
  116. fseek($this->content, 0, \SEEK_END);
  117. $this->offset += \strlen($data);
  118. return $data;
  119. }
  120. }
  121. if (\is_string($this->content)) {
  122. if (\strlen($this->content) <= $count) {
  123. $data = $this->content;
  124. $this->content = null;
  125. } else {
  126. $data = substr($this->content, 0, $count);
  127. $this->content = substr($this->content, $count);
  128. }
  129. $this->offset += \strlen($data);
  130. return $data;
  131. }
  132. foreach ($this->client->stream([$this->response], $this->blocking ? $this->timeout : 0) as $chunk) {
  133. try {
  134. $this->eof = true;
  135. $this->eof = !$chunk->isTimeout();
  136. $this->eof = $chunk->isLast();
  137. if ($chunk->isFirst()) {
  138. $this->response->getStatusCode(); // ignore 3/4/5xx
  139. }
  140. if ('' !== $data = $chunk->getContent()) {
  141. if (\strlen($data) > $count) {
  142. if (null === $this->content) {
  143. $this->content = substr($data, $count);
  144. }
  145. $data = substr($data, 0, $count);
  146. }
  147. $this->offset += \strlen($data);
  148. return $data;
  149. }
  150. } catch (ExceptionInterface $e) {
  151. trigger_error($e->getMessage(), \E_USER_WARNING);
  152. return false;
  153. }
  154. }
  155. return '';
  156. }
  157. public function stream_set_option(int $option, int $arg1, ?int $arg2): bool
  158. {
  159. if (\STREAM_OPTION_BLOCKING === $option) {
  160. $this->blocking = (bool) $arg1;
  161. } elseif (\STREAM_OPTION_READ_TIMEOUT === $option) {
  162. $this->timeout = $arg1 + $arg2 / 1e6;
  163. } else {
  164. return false;
  165. }
  166. return true;
  167. }
  168. public function stream_tell(): int
  169. {
  170. return $this->offset;
  171. }
  172. public function stream_eof(): bool
  173. {
  174. return $this->eof && !\is_string($this->content);
  175. }
  176. public function stream_seek(int $offset, int $whence = \SEEK_SET): bool
  177. {
  178. if (!\is_resource($this->content) || 0 !== fseek($this->content, 0, \SEEK_END)) {
  179. return false;
  180. }
  181. $size = ftell($this->content);
  182. if (\SEEK_CUR === $whence) {
  183. $offset += $this->offset;
  184. }
  185. if (\SEEK_END === $whence || $size < $offset) {
  186. foreach ($this->client->stream([$this->response]) as $chunk) {
  187. try {
  188. if ($chunk->isFirst()) {
  189. $this->response->getStatusCode(); // ignore 3/4/5xx
  190. }
  191. // Chunks are buffered in $this->content already
  192. $size += \strlen($chunk->getContent());
  193. if (\SEEK_END !== $whence && $offset <= $size) {
  194. break;
  195. }
  196. } catch (ExceptionInterface $e) {
  197. trigger_error($e->getMessage(), \E_USER_WARNING);
  198. return false;
  199. }
  200. }
  201. if (\SEEK_END === $whence) {
  202. $offset += $size;
  203. }
  204. }
  205. if (0 <= $offset && $offset <= $size) {
  206. $this->eof = false;
  207. $this->offset = $offset;
  208. return true;
  209. }
  210. return false;
  211. }
  212. public function stream_cast(int $castAs)
  213. {
  214. if (\STREAM_CAST_FOR_SELECT === $castAs) {
  215. $this->response->getHeaders(false);
  216. return (\is_callable($this->handle) ? ($this->handle)() : $this->handle) ?? false;
  217. }
  218. return false;
  219. }
  220. public function stream_stat(): array
  221. {
  222. try {
  223. $headers = $this->response->getHeaders(false);
  224. } catch (ExceptionInterface $e) {
  225. trigger_error($e->getMessage(), \E_USER_WARNING);
  226. $headers = [];
  227. }
  228. return [
  229. 'dev' => 0,
  230. 'ino' => 0,
  231. 'mode' => 33060,
  232. 'nlink' => 0,
  233. 'uid' => 0,
  234. 'gid' => 0,
  235. 'rdev' => 0,
  236. 'size' => (int) ($headers['content-length'][0] ?? -1),
  237. 'atime' => 0,
  238. 'mtime' => strtotime($headers['last-modified'][0] ?? '') ?: 0,
  239. 'ctime' => 0,
  240. 'blksize' => 0,
  241. 'blocks' => 0,
  242. ];
  243. }
  244. private function __construct()
  245. {
  246. }
  247. }