1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\JsonObjects; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\MessageInterface; |
6
|
|
|
use Psr\Http\Message\StreamInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The data streaming. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class DataStreaming |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Stream content from the given data. |
16
|
|
|
* |
17
|
|
|
* @param mixed $data |
18
|
|
|
* @return resource |
19
|
|
|
* |
20
|
|
|
* @throws JsonObjectsException |
21
|
|
|
*/ |
22
|
12 |
|
public function streamData($data) |
23
|
|
|
{ |
24
|
12 |
|
$method = 'stream' . ucfirst(gettype($data)); |
25
|
|
|
|
26
|
12 |
|
if (method_exists($this, $method)) { |
27
|
10 |
|
return $this->{$method}($data); |
28
|
|
|
} |
29
|
|
|
|
30
|
2 |
|
throw new JsonObjectsException('Unable to create a stream from the given data.'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Stream content from the given resource |
35
|
|
|
* |
36
|
|
|
* @param mixed $resource |
37
|
|
|
* @return resource |
38
|
|
|
* |
39
|
|
|
* @throws JsonObjectsException |
40
|
|
|
*/ |
41
|
4 |
|
public function streamResource($resource) |
42
|
|
|
{ |
43
|
4 |
|
if (is_resource($resource)) { |
44
|
3 |
|
return $resource; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
throw new JsonObjectsException('Unable to create a stream from an invalid resource.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Stream content from the given string |
52
|
|
|
* |
53
|
|
|
* @param string $string |
54
|
|
|
* @return resource |
55
|
|
|
* |
56
|
|
|
* @throws JsonObjectsException |
57
|
|
|
*/ |
58
|
11 |
|
public function streamString(string $string) |
59
|
|
|
{ |
60
|
11 |
|
$stream = extension_loaded('zlib') ? @gzopen($string, 'rb') : @fopen($string, 'rb'); |
61
|
|
|
|
62
|
11 |
|
if ($stream === false) { |
63
|
1 |
|
throw new JsonObjectsException("Failed to open stream from: {$string}"); |
64
|
|
|
} |
65
|
|
|
|
66
|
10 |
|
return $stream; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Stream content from the given object |
71
|
|
|
* |
72
|
|
|
* @param mixed $object |
73
|
|
|
* @return resource |
74
|
|
|
* |
75
|
|
|
* @throws JsonObjectsException |
76
|
|
|
*/ |
77
|
5 |
|
public function streamObject($object) |
78
|
|
|
{ |
79
|
5 |
|
if (!is_object($object)) { |
80
|
1 |
|
throw new JsonObjectsException('Unable to stream content from object while providing ' . gettype($object)); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
if ($object instanceof MessageInterface) { |
84
|
1 |
|
$object = $object->getBody(); |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
if ($object instanceof StreamInterface) { |
88
|
3 |
|
return $this->streamWrapper($object); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
throw new JsonObjectsException('Unable to create a stream from ' . get_class($object)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Stream content from the given stream wrapper |
96
|
|
|
* |
97
|
|
|
* @param \Psr\Http\Message\StreamInterface $stream |
98
|
|
|
* @return resource |
99
|
|
|
* |
100
|
|
|
* @throws JsonObjectsException |
101
|
|
|
*/ |
102
|
3 |
|
public function streamWrapper(StreamInterface $stream) |
103
|
|
|
{ |
104
|
|
|
// Register the stream wrapper if not already registered |
105
|
3 |
|
if (!in_array(StreamWrapper::NAME, stream_get_wrappers())) { |
106
|
1 |
|
stream_wrapper_register(StreamWrapper::NAME, StreamWrapper::class); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Retrieve a handler of the opened stream |
110
|
3 |
|
$resource = @fopen(StreamWrapper::NAME . '://stream', 'rb', false, stream_context_create([ |
111
|
3 |
|
StreamWrapper::NAME => compact('stream'), |
112
|
|
|
])); |
113
|
|
|
|
114
|
3 |
|
if ($resource === false) { |
115
|
1 |
|
throw new JsonObjectsException('Failed to open stream from ' . get_class($stream)); |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
return $resource; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|