FnStream.php 4.4 KB

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