StreamDecoratorTrait.php 3.2 KB

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