AmpBody.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\Internal;
  11. use Amp\ByteStream\InputStream;
  12. use Amp\ByteStream\ResourceInputStream;
  13. use Amp\Http\Client\RequestBody;
  14. use Amp\Promise;
  15. use Amp\Success;
  16. use Symfony\Component\HttpClient\Exception\TransportException;
  17. /**
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. *
  20. * @internal
  21. */
  22. class AmpBody implements RequestBody, InputStream
  23. {
  24. /**
  25. * @var \Amp\ByteStream\ResourceInputStream|\Closure|string
  26. */
  27. private $body;
  28. /**
  29. * @var mixed[]
  30. */
  31. private $info;
  32. /**
  33. * @var \Closure
  34. */
  35. private $onProgress;
  36. /**
  37. * @var int|null
  38. */
  39. private $offset = 0;
  40. /**
  41. * @var int
  42. */
  43. private $length = -1;
  44. /**
  45. * @var int|null
  46. */
  47. private $uploaded;
  48. /**
  49. * @param \Closure|resource|string $body
  50. * @param \Closure $onProgress
  51. */
  52. public function __construct($body, &$info, $onProgress)
  53. {
  54. $this->info = &$info;
  55. $this->onProgress = $onProgress;
  56. if (\is_resource($body)) {
  57. $this->offset = ftell($body);
  58. $this->length = fstat($body)['size'];
  59. $this->body = new ResourceInputStream($body);
  60. } elseif (\is_string($body)) {
  61. $this->length = \strlen($body);
  62. $this->body = $body;
  63. } else {
  64. $this->body = $body;
  65. }
  66. }
  67. public function createBodyStream()
  68. {
  69. if (null !== $this->uploaded) {
  70. $this->uploaded = null;
  71. if (\is_string($this->body)) {
  72. $this->offset = 0;
  73. } elseif ($this->body instanceof ResourceInputStream) {
  74. fseek($this->body->getResource(), $this->offset);
  75. }
  76. }
  77. return $this;
  78. }
  79. public function getHeaders()
  80. {
  81. return new Success([]);
  82. }
  83. public function getBodyLength()
  84. {
  85. return new Success($this->length - $this->offset);
  86. }
  87. public function read()
  88. {
  89. $this->info['size_upload'] += $this->uploaded;
  90. $this->uploaded = 0;
  91. ($this->onProgress)();
  92. $chunk = $this->doRead();
  93. $chunk->onResolve(function ($e, $data) {
  94. if (null !== $data) {
  95. $this->uploaded = \strlen($data);
  96. } else {
  97. $this->info['upload_content_length'] = $this->info['size_upload'];
  98. }
  99. });
  100. return $chunk;
  101. }
  102. /**
  103. * @param \Amp\Http\Client\RequestBody $body
  104. */
  105. public static function rewind($body)
  106. {
  107. if (!$body instanceof self) {
  108. return $body;
  109. }
  110. $body->uploaded = null;
  111. if ($body->body instanceof ResourceInputStream) {
  112. fseek($body->body->getResource(), $body->offset);
  113. return new $body($body->body, $body->info, $body->onProgress);
  114. }
  115. if (\is_string($body->body)) {
  116. $body->offset = 0;
  117. }
  118. return $body;
  119. }
  120. private function doRead()
  121. {
  122. if ($this->body instanceof ResourceInputStream) {
  123. return $this->body->read();
  124. }
  125. if (null === $this->offset || !$this->length) {
  126. return new Success();
  127. }
  128. if (\is_string($this->body)) {
  129. $this->offset = null;
  130. return new Success($this->body);
  131. }
  132. if ('' === $data = ($this->body)(16372)) {
  133. $this->offset = null;
  134. return new Success();
  135. }
  136. if (!\is_string($data)) {
  137. throw new TransportException(sprintf('Return value of the "body" option callback must be string, "%s" returned.', get_debug_type($data)));
  138. }
  139. return new Success($data);
  140. }
  141. }