|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class StreamFactory |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource StreamFactory.php |
|
6
|
|
|
* @created 27.08.2018 |
|
7
|
|
|
* @package chillerlan\HTTP\Psr17 |
|
8
|
|
|
* @author smiley <[email protected]> |
|
9
|
|
|
* @copyright 2018 smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\HTTP\Psr17; |
|
14
|
|
|
|
|
15
|
|
|
use chillerlan\HTTP\Psr7\Stream; |
|
16
|
|
|
use Psr\Http\Message\{StreamFactoryInterface, StreamInterface}; |
|
17
|
|
|
|
|
18
|
|
|
final class StreamFactory implements StreamFactoryInterface{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Create a new stream from a string. |
|
22
|
|
|
* |
|
23
|
|
|
* The stream SHOULD be created with a temporary resource. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $content String content with which to populate the stream. |
|
26
|
|
|
* |
|
27
|
|
|
* @return \Psr\Http\Message\StreamInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
public function createStream(string $content = ''):StreamInterface{ |
|
30
|
|
|
return create_stream($content); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create a stream from an existing file. |
|
35
|
|
|
* |
|
36
|
|
|
* The file MUST be opened using the given mode, which may be any mode |
|
37
|
|
|
* supported by the `fopen` function. |
|
38
|
|
|
* |
|
39
|
|
|
* The `$filename` MAY be any string supported by `fopen()`. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $filename Filename or stream URI to use as basis of stream. |
|
42
|
|
|
* @param string $mode Mode with which to open the underlying filename/stream. |
|
43
|
|
|
* |
|
44
|
|
|
* @return \Psr\Http\Message\StreamInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
public function createStreamFromFile(string $filename, string $mode = 'r'):StreamInterface{ |
|
47
|
|
|
return new Stream(fopen($filename, $mode)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create a new stream from an existing resource. |
|
52
|
|
|
* |
|
53
|
|
|
* The stream MUST be readable and may be writable. |
|
54
|
|
|
* |
|
55
|
|
|
* @param resource $resource PHP resource to use as basis of stream. |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Psr\Http\Message\StreamInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
public function createStreamFromResource($resource):StreamInterface{ |
|
60
|
|
|
return new Stream($resource); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|