StreamDecoratorTrait.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace GuzzleHttp\Stream;
  3. use GuzzleHttp\Stream\Exception\CannotAttachException;
  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. public function __get($name)
  22. {
  23. if ($name == 'stream') {
  24. $this->stream = $this->createStream();
  25. return $this->stream;
  26. }
  27. throw new \UnexpectedValueException("$name not found on class");
  28. }
  29. public function __toString()
  30. {
  31. try {
  32. $this->seek(0);
  33. return $this->getContents();
  34. } catch (\Exception $e) {
  35. // Really, PHP? https://bugs.php.net/bug.php?id=53648
  36. trigger_error('StreamDecorator::__toString exception: '
  37. . (string) $e, E_USER_ERROR);
  38. return '';
  39. }
  40. }
  41. public function getContents()
  42. {
  43. return Utils::copyToString($this);
  44. }
  45. /**
  46. * Allow decorators to implement custom methods
  47. *
  48. * @param string $method Missing method name
  49. * @param array $args Method arguments
  50. *
  51. * @return mixed
  52. */
  53. public function __call($method, array $args)
  54. {
  55. $result = call_user_func_array(array($this->stream, $method), $args);
  56. // Always return the wrapped object if the result is a return $this
  57. return $result === $this->stream ? $this : $result;
  58. }
  59. public function close()
  60. {
  61. $this->stream->close();
  62. }
  63. public function getMetadata($key = null)
  64. {
  65. return $this->stream->getMetadata($key);
  66. }
  67. public function detach()
  68. {
  69. return $this->stream->detach();
  70. }
  71. public function attach($stream)
  72. {
  73. throw new CannotAttachException();
  74. }
  75. public function getSize()
  76. {
  77. return $this->stream->getSize();
  78. }
  79. public function eof()
  80. {
  81. return $this->stream->eof();
  82. }
  83. public function tell()
  84. {
  85. return $this->stream->tell();
  86. }
  87. public function isReadable()
  88. {
  89. return $this->stream->isReadable();
  90. }
  91. public function isWritable()
  92. {
  93. return $this->stream->isWritable();
  94. }
  95. public function isSeekable()
  96. {
  97. return $this->stream->isSeekable();
  98. }
  99. public function seek($offset, $whence = SEEK_SET)
  100. {
  101. return $this->stream->seek($offset, $whence);
  102. }
  103. public function read($length)
  104. {
  105. return $this->stream->read($length);
  106. }
  107. public function write($string)
  108. {
  109. return $this->stream->write($string);
  110. }
  111. /**
  112. * Implement in subclasses to dynamically create streams when requested.
  113. *
  114. * @return StreamInterface
  115. * @throws \BadMethodCallException
  116. */
  117. protected function createStream()
  118. {
  119. throw new \BadMethodCallException('createStream() not implemented in '
  120. . get_class($this));
  121. }
  122. }