StreamDecoratorTrait.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 = ($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. * @return mixed
  71. */
  72. public function getMetadata($key = null)
  73. {
  74. return $this->stream->getMetadata($key);
  75. }
  76. public function detach()
  77. {
  78. return $this->stream->detach();
  79. }
  80. public function getSize(): ?int
  81. {
  82. return $this->stream->getSize();
  83. }
  84. public function eof(): bool
  85. {
  86. return $this->stream->eof();
  87. }
  88. public function tell(): int
  89. {
  90. return $this->stream->tell();
  91. }
  92. public function isReadable(): bool
  93. {
  94. return $this->stream->isReadable();
  95. }
  96. public function isWritable(): bool
  97. {
  98. return $this->stream->isWritable();
  99. }
  100. public function isSeekable(): bool
  101. {
  102. return $this->stream->isSeekable();
  103. }
  104. public function rewind(): void
  105. {
  106. $this->seek(0);
  107. }
  108. public function seek($offset, $whence = SEEK_SET): void
  109. {
  110. $this->stream->seek($offset, $whence);
  111. }
  112. public function read($length): string
  113. {
  114. return $this->stream->read($length);
  115. }
  116. public function write($string): int
  117. {
  118. return $this->stream->write($string);
  119. }
  120. /**
  121. * Implement in subclasses to dynamically create streams when requested.
  122. *
  123. * @throws \BadMethodCallException
  124. */
  125. protected function createStream(): StreamInterface
  126. {
  127. throw new \BadMethodCallException('Not implemented');
  128. }
  129. }