GuzzleHttpPsr7FnStream

Compose stream implementations based on a hash of functions.

Defined (1)

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

/lib/Azure/GuzzleHttp/Psr7/FnStream.php  
  1. class FnStream implements StreamInterface 
  2. /** @var array */ 
  3. private $methods; 
  4.  
  5. /** @var array Methods that must be implemented in the given array */ 
  6. private static $slots = ['__toString', 'close', 'detach', 'rewind',  
  7. 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write',  
  8. 'isReadable', 'read', 'getContents', 'getMetadata']; 
  9.  
  10. /** 
  11. * @param array $methods Hash of method name to a callable. 
  12. */ 
  13. public function __construct(array $methods) 
  14. $this->methods = $methods; 
  15.  
  16. // Create the functions on the class 
  17. foreach ($methods as $name => $fn) { 
  18. $this->{'_fn_' . $name} = $fn; 
  19.  
  20. /** 
  21. * Lazily determine which methods are not implemented. 
  22. * @throws \BadMethodCallException 
  23. */ 
  24. public function __get($name) 
  25. throw new \BadMethodCallException(str_replace('_fn_', '', $name) 
  26. . '() is not implemented in the FnStream'); 
  27.  
  28. /** 
  29. * The close method is called on the underlying stream only if possible. 
  30. */ 
  31. public function __destruct() 
  32. if (isset($this->_fn_close)) { 
  33. call_user_func($this->_fn_close); 
  34.  
  35. /** 
  36. * Adds custom functionality to an underlying stream by intercepting 
  37. * specific method calls. 
  38. * @param StreamInterface $stream Stream to decorate 
  39. * @param array $methods Hash of method name to a closure 
  40. * @return FnStream 
  41. */ 
  42. public static function decorate(StreamInterface $stream, array $methods) 
  43. // If any of the required methods were not provided, then simply 
  44. // proxy to the decorated stream. 
  45. foreach (array_diff(self::$slots, array_keys($methods)) as $diff) { 
  46. $methods[$diff] = [$stream, $diff]; 
  47.  
  48. return new self($methods); 
  49.  
  50. public function __toString() 
  51. return call_user_func($this->_fn___toString); 
  52.  
  53. public function close() 
  54. return call_user_func($this->_fn_close); 
  55.  
  56. public function detach() 
  57. return call_user_func($this->_fn_detach); 
  58.  
  59. public function getSize() 
  60. return call_user_func($this->_fn_getSize); 
  61.  
  62. public function tell() 
  63. return call_user_func($this->_fn_tell); 
  64.  
  65. public function eof() 
  66. return call_user_func($this->_fn_eof); 
  67.  
  68. public function isSeekable() 
  69. return call_user_func($this->_fn_isSeekable); 
  70.  
  71. public function rewind() 
  72. call_user_func($this->_fn_rewind); 
  73.  
  74. public function seek($offset, $whence = SEEK_SET) 
  75. call_user_func($this->_fn_seek, $offset, $whence); 
  76.  
  77. public function isWritable() 
  78. return call_user_func($this->_fn_isWritable); 
  79.  
  80. public function write($string) 
  81. return call_user_func($this->_fn_write, $string); 
  82.  
  83. public function isReadable() 
  84. return call_user_func($this->_fn_isReadable); 
  85.  
  86. public function read($length) 
  87. return call_user_func($this->_fn_read, $length); 
  88.  
  89. public function getContents() 
  90. return call_user_func($this->_fn_getContents); 
  91.  
  92. public function getMetadata($key = null) 
  93. return call_user_func($this->_fn_getMetadata, $key);