FnStream.php 4.2 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. ($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. /** @var string */
  84. return ($this->_fn___toString)();
  85. } catch (\Throwable $e) {
  86. if (\PHP_VERSION_ID >= 70400) {
  87. throw $e;
  88. }
  89. trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR);
  90. return '';
  91. }
  92. }
  93. public function close(): void
  94. {
  95. ($this->_fn_close)();
  96. }
  97. public function detach()
  98. {
  99. return ($this->_fn_detach)();
  100. }
  101. public function getSize(): ?int
  102. {
  103. return ($this->_fn_getSize)();
  104. }
  105. public function tell(): int
  106. {
  107. return ($this->_fn_tell)();
  108. }
  109. public function eof(): bool
  110. {
  111. return ($this->_fn_eof)();
  112. }
  113. public function isSeekable(): bool
  114. {
  115. return ($this->_fn_isSeekable)();
  116. }
  117. public function rewind(): void
  118. {
  119. ($this->_fn_rewind)();
  120. }
  121. public function seek($offset, $whence = SEEK_SET): void
  122. {
  123. ($this->_fn_seek)($offset, $whence);
  124. }
  125. public function isWritable(): bool
  126. {
  127. return ($this->_fn_isWritable)();
  128. }
  129. public function write($string): int
  130. {
  131. return ($this->_fn_write)($string);
  132. }
  133. public function isReadable(): bool
  134. {
  135. return ($this->_fn_isReadable)();
  136. }
  137. public function read($length): string
  138. {
  139. return ($this->_fn_read)($length);
  140. }
  141. public function getContents(): string
  142. {
  143. return ($this->_fn_getContents)();
  144. }
  145. /**
  146. * @return mixed
  147. */
  148. public function getMetadata($key = null)
  149. {
  150. return ($this->_fn_getMetadata)($key);
  151. }
  152. }