@@ -13,14 +13,23 @@ class String
1313 protected $ stream = null ;
1414 protected $ streamObject = null ;
1515 protected $ compressionLevel = 6 ;
16+ protected $ isRealFile = false ;
1617
17- public function __construct ($ readOnly = false , $ compressionLevel = 6 )
18+ public function __construct ($ readOnly = false , $ compressionLevel = 6 , $ filepath = ' php://memory ' )
1819 {
19- $ this ->compressionLevel = $ compressionLevel ;
20- $ this ->stream = fopen ('php://memory ' , 'w ' );
20+ $ this ->replaceStream ($ readOnly , $ compressionLevel , $ filepath );
21+ }
22+
23+ public function replaceStream ($ readOnly = false , $ compressionLevel = 6 , $ filepath = 'php://memory ' )
24+ {
25+ if (substr ($ filepath , 0 , 6 ) !== 'php:// ' ) {
26+ $ this ->isRealFile = true ;
27+ }
28+ $ this ->compressionLevel = $ compressionLevel ;
29+ $ this ->stream = fopen ($ filepath , 'r+ ' );
2130 $ this ->streamObject = Psr7 \stream_for ($ this ->stream );
2231 $ this ->gzStream = new GzStreamGuzzle ($ this ->streamObject , $ readOnly , $ this ->compressionLevel );
23- }
32+ }
2433
2534 public function write ($ string , $ options = 0 , $ depth = 512 )
2635 {
@@ -31,19 +40,22 @@ public function write($string, $options = 0, $depth = 512)
3140 return $ this ->getGzStream ()->write ($ string );
3241 }
3342
43+ public function read ($ length = 65536 )
44+ {
45+ $ ret = $ this ->getGzStream ()->read ($ length );
46+ return $ ret ;
47+ }
48+
3449 public function prepend ($ string , $ compressionLevel = 6 )
3550 {
3651 $ this ->prepareForRead ();
3752 $ gzStreamReadOnly = $ this ->getGzStream ()->readOnlyStream ();
3853
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 );
54+ $ this ->replaceStream (false , $ compressionLevel , 'php://memory ' );
4355
4456 $ this ->getGzStream ()->write ($ string );
4557
46- while ($ buffer = $ gzStreamReadOnly ->read (4096 )) {
58+ while ($ buffer = $ gzStreamReadOnly ->read ()) {
4759 $ this ->getGzStream ()->write ($ buffer );
4860 }
4961 }
@@ -102,6 +114,11 @@ public function getStreamObject()
102114
103115 public function getReadOnlyStream ()
104116 {
117+ if ($ this ->isRealFile ) {
118+ return $ this ->getGzStream ();
119+ }
120+
121+ // More specifically for the in-memory streams:
105122 $ this ->prepareForRead ();
106123 $ gzStreamReadOnly = $ this ->getGzStream ()->readOnlyStream ();
107124
0 commit comments