Debian Codegraph @

vendor/guzzlehttp/psr7/src/StreamDecoratorTrait.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace GuzzleHttp\Psr7;
6
7 use Psr\Http\Message\StreamInterface;
8
9 /**
10 * Stream decorator trait
11 *
12 * @property StreamInterface $stream
13 */
14 trait StreamDecoratorTrait
15 {
16 /**
17 * @param StreamInterface $stream Stream to decorate
18 */
19 public function __construct(StreamInterface $stream)
20 {
21 $this->stream = $stream;
22 }
23
24 /**
25 * Magic method used to create a new stream if streams are not added in
26 * the constructor of a decorator (e.g., LazyOpenStream).
27 *
28 * @return StreamInterface
29 */
30 public function __get(string $name)
31 {
32 if ($name === 'stream') {
33 $this->stream = $this->createStream();
34
35 return $this->stream;
36 }
37
38 throw new \UnexpectedValueException("$name not found on class");
39 }
40
41 public function __toString(): string
42 {
43 try {
44 if ($this->isSeekable()) {
45 $this->seek(0);
46 }
47
48 return $this->getContents();
49 } catch (\Throwable $e) {
50 if (\PHP_VERSION_ID >= 70400) {
51 throw $e;
52 }
53 trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR);
54
55 return '';
56 }
57 }
58
59 public function getContents(): string
60 {
61 return Utils::copyToString($this);
62 }
63
64 /**
65 * Allow decorators to implement custom methods
66 *
67 * @return mixed
68 */
69 public function __call(string $method, array $args)
70 {
71 /** @var callable $callable */
72 $callable = [$this->stream, $method];
73 $result = ($callable)(...$args);
74
75 // Always return the wrapped object if the result is a return $this
76 return $result === $this->stream ? $this : $result;
77 }
78
79 public function close(): void
80 {
81 $this->stream->close();
82 }
83
84 /**
85 * @return mixed
86 */
87 public function getMetadata($key = null)
88 {
89 if ($key !== null && !\is_string($key)) {
90 \trigger_deprecation(
91 'guzzlehttp/psr7',
92 '2.11',
93 'Passing %s to StreamInterface::getMetadata() is deprecated; guzzlehttp/psr7 3.0 requires string|null for $key.',
94 \get_debug_type($key)
95 );
96 }
97
98 return $this->stream->getMetadata($key);
99 }
100
101 public function detach()
102 {
103 return $this->stream->detach();
104 }
105
106 public function getSize(): ?int
107 {
108 return $this->stream->getSize();
109 }
110
111 public function eof(): bool
112 {
113 return $this->stream->eof();
114 }
115
116 public function tell(): int
117 {
118 return $this->stream->tell();
119 }
120
121 public function isReadable(): bool
122 {
123 return $this->stream->isReadable();
124 }
125
126 public function isWritable(): bool
127 {
128 return $this->stream->isWritable();
129 }
130
131 public function isSeekable(): bool
132 {
133 return $this->stream->isSeekable();
134 }
135
136 public function rewind(): void
137 {
138 $this->seek(0);
139 }
140
141 public function seek($offset, $whence = SEEK_SET): void
142 {
143 if (!\is_int($offset)) {
144 \trigger_deprecation(
145 'guzzlehttp/psr7',
146 '2.11',
147 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $offset.',
148 \get_debug_type($offset)
149 );
150 }
151
152 if (!\is_int($whence)) {
153 \trigger_deprecation(
154 'guzzlehttp/psr7',
155 '2.11',
156 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $whence.',
157 \get_debug_type($whence)
158 );
159 }
160
161 $this->stream->seek($offset, $whence);
162 }
163
164 public function read($length): string
165 {
166 if (!\is_int($length)) {
167 \trigger_deprecation(
168 'guzzlehttp/psr7',
169 '2.11',
170 'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.',
171 \get_debug_type($length)
172 );
173 }
174
175 return $this->stream->read($length);
176 }
177
178 public function write($string): int
179 {
180 if (!\is_string($string)) {
181 \trigger_deprecation(
182 'guzzlehttp/psr7',
183 '2.11',
184 'Passing %s to StreamInterface::write() is deprecated; guzzlehttp/psr7 3.0 requires string for $string.',
185 \get_debug_type($string)
186 );
187 }
188
189 return $this->stream->write($string);
190 }
191
192 /**
193 * Implement in subclasses to dynamically create streams when requested.
194 *
195 * @throws \BadMethodCallException
196 */
197 protected function createStream(): StreamInterface
198 {
199 throw new \BadMethodCallException('Not implemented');
200 }
201 }