GuzzleHttpPsr7StreamDecoratorTrait

Stream decorator trait.

Defined (1)

The class is defined in the following location(s).

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