酒店预订平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

164 linhas
3.9 KiB

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