|
9 | 9 |
|
10 | 10 | class String |
11 | 11 | { |
12 | | - protected $gzStream = null; |
13 | | - protected $stream = null; |
14 | | - protected $streamObject = null; |
15 | | - protected $compressionLevel = 6; |
16 | | - |
17 | | - public function __construct($readOnly = false, $compressionLevel = 6) |
18 | | - { |
19 | | - $this->compressionLevel = $compressionLevel; |
20 | | - $this->stream = fopen('php://memory', 'w'); |
21 | | - $this->streamObject = Psr7\stream_for($this->stream); |
22 | | - $this->gzStream = new GzStreamGuzzle($this->streamObject, $readOnly, $this->compressionLevel); |
23 | | - } |
24 | | - |
25 | | - public function write($string, $options = 0, $depth = 512) |
26 | | - { |
27 | | - if (!is_string($string)) |
28 | | - { |
29 | | - $string = json_encode($string, $options, $depth); |
30 | | - } |
31 | | - |
32 | | - return $this->getGzStream()->write($string); |
33 | | - } |
34 | | - |
35 | | - public function prepend($string, $compressionLevel = 6) |
36 | | - { |
37 | | - $this->prepareForRead(); |
38 | | - $gzStreamReadOnly = $this->getGzStream()->readOnlyStream(); |
39 | | - |
40 | | - $this->compressionLevel = $compressionLevel; |
41 | | - $this->stream = fopen('php://memory', 'w'); |
42 | | - $this->streamObject = Psr7\stream_for($this->stream); |
43 | | - $this->gzStream = new GzStreamGuzzle($this->streamObject, false, $compressionLevel); |
44 | | - |
45 | | - $this->getGzStream()->write($string); |
46 | | - |
47 | | - while($buffer = $gzStreamReadOnly->read(4096)) { |
48 | | - $this->getGzStream()->write($buffer); |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - public function getCompressedSize() |
53 | | - { |
54 | | - return strlen($this->getCompressedContents()); |
55 | | - } |
56 | | - |
57 | | - public function getDecompressedContents() |
58 | | - { |
59 | | - return gzdecode($this->getCompressedContents()); |
60 | | - } |
61 | | - |
62 | | - public function writeDecompressedContents($filename, $lock = false) |
63 | | - { |
64 | | - return file_put_contents($filename, $this->getDecompressedContents(), $lock ? LOCK_EX : 0); |
65 | | - } |
66 | | - |
67 | | - public function getCompressedContents() |
68 | | - { |
69 | | - $this->prepareForRead(); |
70 | | - return stream_get_contents($this->getStream()); |
71 | | - } |
72 | | - |
73 | | - public function writeCompressedContents($filename, $lock = false) |
74 | | - { |
75 | | - return file_put_contents($filename, $this->getCompressedContents(), $lock ? LOCK_EX : 0); |
76 | | - } |
77 | | - |
78 | | - public function getGzStream() |
79 | | - { |
80 | | - return $this->gzStream; |
81 | | - } |
82 | | - |
83 | | - public function prepareForRead() |
84 | | - { |
85 | | - $this->getGzStream()->writeFooterEarly(); |
| 12 | + protected $gzStream = null; |
| 13 | + protected $stream = null; |
| 14 | + protected $streamObject = null; |
| 15 | + protected $compressionLevel = 6; |
| 16 | + |
| 17 | + public function __construct($readOnly = false, $compressionLevel = 6) |
| 18 | + { |
| 19 | + $this->compressionLevel = $compressionLevel; |
| 20 | + $this->stream = fopen('php://memory', 'w'); |
| 21 | + $this->streamObject = Psr7\stream_for($this->stream); |
| 22 | + $this->gzStream = new GzStreamGuzzle($this->streamObject, $readOnly, $this->compressionLevel); |
| 23 | + } |
| 24 | + |
| 25 | + public function write($string, $options = 0, $depth = 512) |
| 26 | + { |
| 27 | + if (!is_string($string)) { |
| 28 | + $string = json_encode($string, $options, $depth); |
| 29 | + } |
| 30 | + |
| 31 | + return $this->getGzStream()->write($string); |
| 32 | + } |
| 33 | + |
| 34 | + public function prepend($string, $compressionLevel = 6) |
| 35 | + { |
| 36 | + $this->prepareForRead(); |
| 37 | + $gzStreamReadOnly = $this->getGzStream()->readOnlyStream(); |
| 38 | + |
| 39 | + $this->compressionLevel = $compressionLevel; |
| 40 | + $this->stream = fopen('php://memory', 'w'); |
| 41 | + $this->streamObject = Psr7\stream_for($this->stream); |
| 42 | + $this->gzStream = new GzStreamGuzzle($this->streamObject, false, $compressionLevel); |
| 43 | + |
| 44 | + $this->getGzStream()->write($string); |
| 45 | + |
| 46 | + while ($buffer = $gzStreamReadOnly->read(4096)) { |
| 47 | + $this->getGzStream()->write($buffer); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public function getCompressedSize() |
| 52 | + { |
| 53 | + return strlen($this->getCompressedContents()); |
| 54 | + } |
| 55 | + |
| 56 | + public function getDecompressedContents() |
| 57 | + { |
| 58 | + return gzdecode($this->getCompressedContents()); |
| 59 | + } |
| 60 | + |
| 61 | + public function writeDecompressedContents($filename, $lock = false) |
| 62 | + { |
| 63 | + return file_put_contents($filename, $this->getDecompressedContents(), $lock ? LOCK_EX : 0); |
| 64 | + } |
| 65 | + |
| 66 | + public function getCompressedContents() |
| 67 | + { |
| 68 | + $this->prepareForRead(); |
| 69 | + return stream_get_contents($this->getStream()); |
| 70 | + } |
| 71 | + |
| 72 | + public function writeCompressedContents($filename, $lock = false) |
| 73 | + { |
| 74 | + return file_put_contents($filename, $this->getCompressedContents(), $lock ? LOCK_EX : 0); |
| 75 | + } |
| 76 | + |
| 77 | + public function getGzStream() |
| 78 | + { |
| 79 | + return $this->gzStream; |
| 80 | + } |
| 81 | + |
| 82 | + public function prepareForRead() |
| 83 | + { |
| 84 | + $this->getGzStream()->writeFooterEarly(); |
86 | 85 | $this->rewind(); |
87 | | - } |
88 | | - |
89 | | - public function rewind() |
90 | | - { |
91 | | - rewind($this->stream); |
92 | | - } |
93 | | - |
94 | | - public function getStream() |
95 | | - { |
96 | | - return $this->stream; |
97 | | - } |
98 | | - |
99 | | - public function getStreamObject() |
100 | | - { |
101 | | - return $this->streamObject; |
102 | | - } |
103 | | - |
104 | | - public function getReadOnlyStream() |
105 | | - { |
106 | | - $this->prepareForRead(); |
107 | | - $gzStreamReadOnly = $this->getGzStream()->readOnlyStream(); |
108 | | - |
109 | | - return $gzStreamReadOnly; |
110 | | - } |
| 86 | + } |
| 87 | + |
| 88 | + public function rewind() |
| 89 | + { |
| 90 | + rewind($this->stream); |
| 91 | + } |
| 92 | + |
| 93 | + public function getStream() |
| 94 | + { |
| 95 | + return $this->stream; |
| 96 | + } |
| 97 | + |
| 98 | + public function getStreamObject() |
| 99 | + { |
| 100 | + return $this->streamObject; |
| 101 | + } |
| 102 | + |
| 103 | + public function getReadOnlyStream() |
| 104 | + { |
| 105 | + $this->prepareForRead(); |
| 106 | + $gzStreamReadOnly = $this->getGzStream()->readOnlyStream(); |
| 107 | + |
| 108 | + return $gzStreamReadOnly; |
| 109 | + } |
111 | 110 | } |
0 commit comments