StreamDecoratorTrait.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Psr7;
  4. use Psr\Http\Message\StreamInterface;
  5. /**
  6. * Stream decorator trait
  7. *
  8. * @property StreamInterface $stream
  9. */
  10. trait StreamDecoratorTrait
  11. {
  12. /**
  13. * @param StreamInterface $stream Stream to decorate
  14. */
  15. public function __construct(StreamInterface $stream)
  16. {
  17. $this->stream = $stream;
  18. }
  19. /**
  20. * Magic method used to create a new stream if streams are not added in
  21. * the constructor of a decorator (e.g., LazyOpenStream).
  22. *
  23. * @return StreamInterface
  24. */
  25. public function __get(string $name)
  26. {
  27. if ($name === 'stream') {
  28. $this->stream = $this->createStream();
  29. return $this->stream;
  30. }
  31. throw new \UnexpectedValueException("$name not found on class");
  32. }
  33. public function __toString(): string
  34. {
  35. try {
  36. if ($this->isSeekable()) {
  37. $this->seek(0);
  38. }
  39. return $this->getContents();
  40. } catch (\Throwable $e) {
  41. if (\PHP_VERSION_ID >= 70400) {
  42. throw $e;
  43. }
  44. trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR);
  45. return '';
  46. }
  47. }
  48. public function getContents(): string
  49. {
  50. return Utils::copyToString($this);
  51. }
  52. /**
  53. * Allow decorators to implement custom methods
  54. *
  55. * @return mixed
  56. */
  57. public function __call(string $method, array $args)
  58. {
  59. /** @var callable $callable */
  60. $callable = [$this->stream, $method];
  61. $result = call_user_func_array($callable, $args);
  62. // Always return the wrapped object if the result is a return $this
  63. return $result === $this->stream ? $this : $result;
  64. }
  65. public function close(): void
  66. {
  67. $this->stream->close();
  68. }
  69. /**
  70. * {@inheritdoc}
  71. *
  72. * @return mixed
  73. */
  74. public function getMetadata($key = null)
  75. {
  76. return $this->stream->getMetadata($key);
  77. }
  78. public function detach()
  79. {
  80. return $this->stream->detach();
  81. }
  82. public function getSize(): ?int
  83. {
  84. return $this->stream->getSize();
  85. }
  86. public function eof(): bool
  87. {
  88. return $this->stream->eof();
  89. }
  90. public function tell(): int
  91. {
  92. return $this->stream->tell();
  93. }
  94. public function isReadable(): bool
  95. {
  96. return $this->stream->isReadable();
  97. }
  98. public function isWritable(): bool
  99. {
  100. return $this->stream->isWritable();
  101. }
  102. public function isSeekable(): bool
  103. {
  104. return $this->stream->isSeekable();
  105. }
  106. public function rewind(): void
  107. {
  108. $this->seek(0);
  109. }
  110. public function seek($offset, $whence = SEEK_SET): void
  111. {
  112. $this->stream->seek($offset, $whence);
  113. }
  114. public function read($length): string
  115. {
  116. return $this->stream->read($length);
  117. }
  118. public function write($string): int
  119. {
  120. return $this->stream->write($string);
  121. }
  122. /**
  123. * Implement in subclasses to dynamically create streams when requested.
  124. *
  125. * @throws \BadMethodCallException
  126. */
  127. protected function createStream(): StreamInterface
  128. {
  129. throw new \BadMethodCallException('Not implemented');
  130. }
  131. }