Нет описания

FnStream.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * Adds custom functionality to an underlying stream by intercepting
  49. * specific method calls.
  50. *
  51. * @param StreamInterface $stream Stream to decorate
  52. * @param array $methods Hash of method name to a closure
  53. *
  54. * @return FnStream
  55. */
  56. public static function decorate(StreamInterface $stream, array $methods)
  57. {
  58. // If any of the required methods were not provided, then simply
  59. // proxy to the decorated stream.
  60. foreach (array_diff(self::$slots, array_keys($methods)) as $diff) {
  61. $methods[$diff] = [$stream, $diff];
  62. }
  63. return new self($methods);
  64. }
  65. public function __toString()
  66. {
  67. return call_user_func($this->_fn___toString);
  68. }
  69. public function close()
  70. {
  71. return call_user_func($this->_fn_close);
  72. }
  73. public function detach()
  74. {
  75. return call_user_func($this->_fn_detach);
  76. }
  77. public function getSize()
  78. {
  79. return call_user_func($this->_fn_getSize);
  80. }
  81. public function tell()
  82. {
  83. return call_user_func($this->_fn_tell);
  84. }
  85. public function eof()
  86. {
  87. return call_user_func($this->_fn_eof);
  88. }
  89. public function isSeekable()
  90. {
  91. return call_user_func($this->_fn_isSeekable);
  92. }
  93. public function rewind()
  94. {
  95. call_user_func($this->_fn_rewind);
  96. }
  97. public function seek($offset, $whence = SEEK_SET)
  98. {
  99. call_user_func($this->_fn_seek, $offset, $whence);
  100. }
  101. public function isWritable()
  102. {
  103. return call_user_func($this->_fn_isWritable);
  104. }
  105. public function write($string)
  106. {
  107. return call_user_func($this->_fn_write, $string);
  108. }
  109. public function isReadable()
  110. {
  111. return call_user_func($this->_fn_isReadable);
  112. }
  113. public function read($length)
  114. {
  115. return call_user_func($this->_fn_read, $length);
  116. }
  117. public function getContents()
  118. {
  119. return call_user_func($this->_fn_getContents);
  120. }
  121. public function getMetadata($key = null)
  122. {
  123. return call_user_func($this->_fn_getMetadata, $key);
  124. }
  125. }