StreamDecoratorTrait.php 3.2 KB

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