|
7 | 7 | use Psr\Http\Message\StreamFactoryInterface;
|
8 | 8 | use Psr\Http\Message\StreamInterface;
|
9 | 9 |
|
| 10 | +use function function_exists; |
10 | 11 | use function GuzzleHttp\Psr7\stream_for;
|
11 | 12 | use function GuzzleHttp\Psr7\try_fopen;
|
12 | 13 |
|
13 | 14 | class StreamFactory implements StreamFactoryInterface
|
14 | 15 | {
|
15 | 16 | public function createStream(string $content = ''): StreamInterface
|
16 | 17 | {
|
17 |
| - if (\function_exists('stream_for')) { |
| 18 | + if (function_exists('GuzzleHttp\Psr7\stream_for')) { |
18 | 19 | // fallback for guzzlehttp/psr7<1.7.0
|
19 | 20 | return stream_for($content);
|
20 | 21 | }
|
| 22 | + |
21 | 23 | return Utils::streamFor($content);
|
22 | 24 | }
|
23 | 25 |
|
24 | 26 | public function createStreamFromFile(string $file, string $mode = 'r'): StreamInterface
|
25 | 27 | {
|
26 |
| - if (\function_exists('try_fopen') && \function_exists('steam_for')) { |
| 28 | + if (function_exists('GuzzleHttp\Psr7\try_fopen')) { |
27 | 29 | // fallback for guzzlehttp/psr7<1.7.0
|
28 | 30 | $resource = try_fopen($file, $mode);
|
29 |
| - |
30 |
| - return stream_for($resource); |
| 31 | + } else { |
| 32 | + $resource = Utils::tryFopen($file, $mode); |
31 | 33 | }
|
32 |
| - $resource = Utils::tryFopen($file, $mode); |
33 | 34 |
|
34 |
| - return new Stream($resource); |
| 35 | + |
| 36 | + return $this->createStreamFromResource($resource); |
35 | 37 | }
|
36 | 38 |
|
37 | 39 | public function createStreamFromResource($resource): StreamInterface
|
38 | 40 | {
|
39 |
| - if (\function_exists('stream_for')) { |
40 |
| - // fallback for guzzlehttp/psr7<1.7.0 |
41 |
| - return stream_for($resource); |
42 |
| - } |
43 | 41 | return new Stream($resource);
|
44 | 42 | }
|
45 | 43 | }
|
0 commit comments