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