FnStream.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\StreamInterface;
  4. /**
  5. * Compose stream implementations based on a hash of functions.
  6. *
  7. * Allows for easy testing and extension of a provided stream without needing
  8. * to create a concrete class for a simple extension point.
  9. */
  10. class FnStream implements StreamInterface
  11. {
  12. /** @var array */
  13. private $methods;
  14. /** @var array Methods that must be implemented in the given array */
  15. private static $slots = ['__toString', 'close', 'detach', 'rewind',
  16. 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write',
  17. 'isReadable', 'read', 'getContents', 'getMetadata'];
  18. /**
  19. * @param array $methods Hash of method name to a callable.
  20. */
  21. public function __construct(array $methods)
  22. {
  23. $this->methods = $methods;
  24. // Create the functions on the class
  25. foreach ($methods as $name => $fn) {
  26. $this->{'_fn_' . $name} = $fn;
  27. }
  28. }
  29. /**
  30. * Lazily determine which methods are not implemented.
  31. * @throws \BadMethodCallException
  32. */
  33. public function __get($name)
  34. {
  35. throw new \BadMethodCallException(str_replace('_fn_', '', $name)
  36. . '() is not implemented in the FnStream');
  37. }
  38. /**
  39. * The close method is called on the underlying stream only if possible.
  40. */
  41. public function __destruct()
  42. {
  43. if (isset($this->_fn_close)) {
  44. call_user_func($this->_fn_close);
  45. }
  46. }
  47. /**
  48. * An unserialize would allow the __destruct to run when the unserialized value goes out of scope.
  49. * @throws \LogicException
  50. */
  51. public function __wakeup()
  52. {
  53. throw new \LogicException('FnStream should never be unserialized');
  54. }
  55. /**
  56. * Adds custom functionality to an underlying stream by intercepting
  57. * specific method calls.
  58. *
  59. * @param StreamInterface $stream Stream to decorate
  60. * @param array $methods Hash of method name to a closure
  61. *
  62. * @return FnStream
  63. */
  64. public static function decorate(StreamInterface $stream, array $methods)
  65. {
  66. // If any of the required methods were not provided, then simply
  67. // proxy to the decorated stream.
  68. foreach (array_diff(self::$slots, array_keys($methods)) as $diff) {
  69. $methods[$diff] = [$stream, $diff];
  70. }
  71. return new self($methods);
  72. }
  73. public function __toString()
  74. {
  75. return call_user_func($this->_fn___toString);
  76. }
  77. public function close()
  78. {
  79. return call_user_func($this->_fn_close);
  80. }
  81. public function detach()
  82. {
  83. return call_user_func($this->_fn_detach);
  84. }
  85. public function getSize()
  86. {
  87. return call_user_func($this->_fn_getSize);
  88. }
  89. public function tell()
  90. {
  91. return call_user_func($this->_fn_tell);
  92. }
  93. public function eof()
  94. {
  95. return call_user_func($this->_fn_eof);
  96. }
  97. public function isSeekable()
  98. {
  99. return call_user_func($this->_fn_isSeekable);
  100. }
  101. public function rewind()
  102. {
  103. call_user_func($this->_fn_rewind);
  104. }
  105. public function seek($offset, $whence = SEEK_SET)
  106. {
  107. call_user_func($this->_fn_seek, $offset, $whence);
  108. }
  109. public function isWritable()
  110. {
  111. return call_user_func($this->_fn_isWritable);
  112. }
  113. public function write($string)
  114. {
  115. return call_user_func($this->_fn_write, $string);
  116. }
  117. public function isReadable()
  118. {
  119. return call_user_func($this->_fn_isReadable);
  120. }
  121. public function read($length)
  122. {
  123. return call_user_func($this->_fn_read, $length);
  124. }
  125. public function getContents()
  126. {
  127. return call_user_func($this->_fn_getContents);
  128. }
  129. public function getMetadata($key = null)
  130. {
  131. return call_user_func($this->_fn_getMetadata, $key);
  132. }
  133. }