FnStream.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. *
  32. * @throws \BadMethodCallException
  33. */
  34. public function __get($name)
  35. {
  36. throw new \BadMethodCallException(str_replace('_fn_', '', $name)
  37. . '() is not implemented in the FnStream');
  38. }
  39. /**
  40. * The close method is called on the underlying stream only if possible.
  41. */
  42. public function __destruct()
  43. {
  44. if (isset($this->_fn_close)) {
  45. call_user_func($this->_fn_close);
  46. }
  47. }
  48. /**
  49. * An unserialize would allow the __destruct to run when the unserialized value goes out of scope.
  50. * @throws \LogicException
  51. */
  52. public function __wakeup()
  53. {
  54. throw new \LogicException('FnStream should never be unserialized');
  55. }
  56. /**
  57. * Adds custom functionality to an underlying stream by intercepting
  58. * specific method calls.
  59. *
  60. * @param StreamInterface $stream Stream to decorate
  61. * @param array $methods Hash of method name to a closure
  62. *
  63. * @return FnStream
  64. */
  65. public static function decorate(StreamInterface $stream, array $methods)
  66. {
  67. // If any of the required methods were not provided, then simply
  68. // proxy to the decorated stream.
  69. foreach (array_diff(self::$slots, array_keys($methods)) as $diff) {
  70. $methods[$diff] = [$stream, $diff];
  71. }
  72. return new self($methods);
  73. }
  74. public function __toString()
  75. {
  76. return call_user_func($this->_fn___toString);
  77. }
  78. public function close()
  79. {
  80. return call_user_func($this->_fn_close);
  81. }
  82. public function detach()
  83. {
  84. return call_user_func($this->_fn_detach);
  85. }
  86. public function getSize()
  87. {
  88. return call_user_func($this->_fn_getSize);
  89. }
  90. public function tell()
  91. {
  92. return call_user_func($this->_fn_tell);
  93. }
  94. public function eof()
  95. {
  96. return call_user_func($this->_fn_eof);
  97. }
  98. public function isSeekable()
  99. {
  100. return call_user_func($this->_fn_isSeekable);
  101. }
  102. public function rewind()
  103. {
  104. call_user_func($this->_fn_rewind);
  105. }
  106. public function seek($offset, $whence = SEEK_SET)
  107. {
  108. call_user_func($this->_fn_seek, $offset, $whence);
  109. }
  110. public function isWritable()
  111. {
  112. return call_user_func($this->_fn_isWritable);
  113. }
  114. public function write($string)
  115. {
  116. return call_user_func($this->_fn_write, $string);
  117. }
  118. public function isReadable()
  119. {
  120. return call_user_func($this->_fn_isReadable);
  121. }
  122. public function read($length)
  123. {
  124. return call_user_func($this->_fn_read, $length);
  125. }
  126. public function getContents()
  127. {
  128. return call_user_func($this->_fn_getContents);
  129. }
  130. public function getMetadata($key = null)
  131. {
  132. return call_user_func($this->_fn_getMetadata, $key);
  133. }
  134. }