| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Psr\Http\Message\StreamInterface; |
| 7 |
/** |
| 8 |
* Compose stream implementations based on a hash of callables. |
| 9 |
* |
| 10 |
* Allows for easy testing and extension of a provided stream without needing |
| 11 |
* to create a concrete class for a simple extension point. |
| 12 |
*/ |
| 13 |
#[\AllowDynamicProperties] |
| 14 |
final class FnStream implements StreamInterface |
| 15 |
{ |
| 16 |
use NonSerializableStreamTrait; |
| 17 |
private const SLOTS = ['__toString', 'close', 'detach', 'rewind', 'getSize', 'tell', 'eof', 'isSeekable', 'seek', 'isWritable', 'write', 'isReadable', 'read', 'getContents', 'getMetadata']; |
| 18 |
/** @var array<string, callable> */ |
| 19 |
private array $methods; |
| 20 |
private bool $detached = \false; |
| 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 callables 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(\sprintf('%s() is not implemented in the FnStream', DiagnosticValue::escape(\str_replace('_fn_', '', $name)))); |
| 40 |
} |
| 41 |
/** |
| 42 |
* The close method is called on the underlying stream only if possible. |
| 43 |
*/ |
| 44 |
public function __destruct() |
| 45 |
{ |
| 46 |
if ($this->detached || !isset($this->_fn_close)) { |
| 47 |
return; |
| 48 |
} |
| 49 |
try { |
| 50 |
$this->close(); |
| 51 |
} catch (\Throwable $e) { |
| 52 |
// Destructors must not surface cleanup failures. |
| 53 |
} |
| 54 |
} |
| 55 |
/** |
| 56 |
* An unserialize would allow the __destruct to run when the unserialized value goes out of scope. |
| 57 |
* |
| 58 |
* @throws \LogicException |
| 59 |
*/ |
| 60 |
public function __wakeup() : void |
| 61 |
{ |
| 62 |
$this->methods = []; |
| 63 |
$this->detached = \true; |
| 64 |
throw new \LogicException(static::class . ' should never be unserialized'); |
| 65 |
} |
| 66 |
public function __unserialize(array $data) : void |
| 67 |
{ |
| 68 |
$this->methods = []; |
| 69 |
$this->detached = \true; |
| 70 |
throw new \LogicException(static::class . ' should never be unserialized'); |
| 71 |
} |
| 72 |
/** |
| 73 |
* Adds custom functionality to an underlying stream by intercepting |
| 74 |
* specific method calls. |
| 75 |
* |
| 76 |
* @param StreamInterface $stream Stream to decorate |
| 77 |
* @param array<string, callable> $methods Hash of method name to a callable |
| 78 |
*/ |
| 79 |
public static function decorate(StreamInterface $stream, array $methods) : self |
| 80 |
{ |
| 81 |
// If any of the required methods were not provided, then simply |
| 82 |
// proxy to the decorated stream. |
| 83 |
foreach (\array_diff(self::SLOTS, \array_keys($methods)) as $diff) { |
| 84 |
/** @var callable $callable */ |
| 85 |
$callable = [$stream, $diff]; |
| 86 |
$methods[$diff] = $callable; |
| 87 |
} |
| 88 |
return new self($methods); |
| 89 |
} |
| 90 |
public function __toString() : string |
| 91 |
{ |
| 92 |
$this->assertAttached(); |
| 93 |
/** @var string */ |
| 94 |
return ($this->_fn___toString)(); |
| 95 |
} |
| 96 |
public function close() : void |
| 97 |
{ |
| 98 |
if ($this->detached) { |
| 99 |
return; |
| 100 |
} |
| 101 |
$close = $this->_fn_close; |
| 102 |
$this->detached = \true; |
| 103 |
$close(); |
| 104 |
} |
| 105 |
public function detach() |
| 106 |
{ |
| 107 |
if ($this->detached) { |
| 108 |
return null; |
| 109 |
} |
| 110 |
$detach = $this->_fn_detach; |
| 111 |
$result = $detach(); |
| 112 |
$this->detached = \true; |
| 113 |
return $result; |
| 114 |
} |
| 115 |
public function getSize() : ?int |
| 116 |
{ |
| 117 |
if ($this->detached) { |
| 118 |
return null; |
| 119 |
} |
| 120 |
return ($this->_fn_getSize)(); |
| 121 |
} |
| 122 |
public function tell() : int |
| 123 |
{ |
| 124 |
$this->assertAttached(); |
| 125 |
return ($this->_fn_tell)(); |
| 126 |
} |
| 127 |
public function eof() : bool |
| 128 |
{ |
| 129 |
$this->assertAttached(); |
| 130 |
return ($this->_fn_eof)(); |
| 131 |
} |
| 132 |
public function isSeekable() : bool |
| 133 |
{ |
| 134 |
if ($this->detached) { |
| 135 |
return \false; |
| 136 |
} |
| 137 |
return ($this->_fn_isSeekable)(); |
| 138 |
} |
| 139 |
public function rewind() : void |
| 140 |
{ |
| 141 |
$this->assertAttached(); |
| 142 |
($this->_fn_rewind)(); |
| 143 |
} |
| 144 |
public function seek(int $offset, int $whence = \SEEK_SET) : void |
| 145 |
{ |
| 146 |
$this->assertAttached(); |
| 147 |
($this->_fn_seek)($offset, $whence); |
| 148 |
} |
| 149 |
public function isWritable() : bool |
| 150 |
{ |
| 151 |
if ($this->detached) { |
| 152 |
return \false; |
| 153 |
} |
| 154 |
return ($this->_fn_isWritable)(); |
| 155 |
} |
| 156 |
public function write(string $string) : int |
| 157 |
{ |
| 158 |
$this->assertAttached(); |
| 159 |
return ($this->_fn_write)($string); |
| 160 |
} |
| 161 |
public function isReadable() : bool |
| 162 |
{ |
| 163 |
if ($this->detached) { |
| 164 |
return \false; |
| 165 |
} |
| 166 |
return ($this->_fn_isReadable)(); |
| 167 |
} |
| 168 |
public function read(int $length) : string |
| 169 |
{ |
| 170 |
$this->assertAttached(); |
| 171 |
if ($length < 0) { |
| 172 |
throw new \RuntimeException('Length parameter cannot be negative'); |
| 173 |
} |
| 174 |
return ($this->_fn_read)($length); |
| 175 |
} |
| 176 |
public function getContents() : string |
| 177 |
{ |
| 178 |
$this->assertAttached(); |
| 179 |
return ($this->_fn_getContents)(); |
| 180 |
} |
| 181 |
/** |
| 182 |
* @return mixed |
| 183 |
*/ |
| 184 |
public function getMetadata(?string $key = null) |
| 185 |
{ |
| 186 |
if ($this->detached) { |
| 187 |
return $key === null ? [] : null; |
| 188 |
} |
| 189 |
return ($this->_fn_getMetadata)($key); |
| 190 |
} |
| 191 |
private function assertAttached() : void |
| 192 |
{ |
| 193 |
if ($this->detached) { |
| 194 |
throw new \RuntimeException('Stream is detached'); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|