PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.0.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.0.1
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / vendor / guzzlehttp / psr7 / src / FnStream.php

FnStream.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 11.0.1, at vendor/guzzlehttp/psr7/src/FnStream.php

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