@@ -38,13 +38,6 @@ class MultipartStreamBuilder
38
38
*/
39
39
private $ data = [];
40
40
41
- /**
42
- * @var int Bytes of unallocated memory to use for stream buffer.
43
- * To have MultipartStreamBuilder manage it automatically, set to -1.
44
- * Default: -1
45
- */
46
- private $ bufferMaxMemory = -1 ;
47
-
48
41
/**
49
42
* @param HttplugStreamFactory|StreamFactoryInterface|null $streamFactory
50
43
*/
@@ -138,18 +131,10 @@ public function addResource($name, $resource, array $options = [])
138
131
*/
139
132
public function build ()
140
133
{
141
- // Assign maximimum 1/4 php's available memory
142
- // to attempt buffering the stream content.
143
- // If the stream content exceed this, will fallback
144
- // to use temporary file.
145
- $ maxmemory = ($ this ->bufferMaxMemory < 0 )
146
- ? \floor (static ::getAvailableMemory () / 4 )
147
- : $ this ->bufferMaxMemory ;
148
-
149
134
// Open a temporary read-write stream as buffer.
150
135
// If the size is less than predefined limit, things will stay in memory.
151
136
// If the size is more than that, things will be stored in temp file.
152
- $ buffer = fopen ('php://temp/maxmemory: ' . $ maxmemory , 'r+ ' );
137
+ $ buffer = fopen ('php://temp ' , 'r+ ' );
153
138
foreach ($ this ->data as $ data ) {
154
139
// Add start and headers
155
140
fwrite ($ buffer , "-- {$ this ->getBoundary ()}\r\n" .
@@ -166,8 +151,8 @@ public function build()
166
151
}
167
152
if ($ contentStream ->isReadable ()) {
168
153
while (!$ contentStream ->eof ()) {
169
- // read 8KB chunk into buffer until reached EOF.
170
- fwrite ($ buffer , $ contentStream ->read (8192 ));
154
+ // read 1MB chunk into buffer until reached EOF.
155
+ fwrite ($ buffer , $ contentStream ->read (1048576 ));
171
156
}
172
157
} else {
173
158
// Try to getContents for non-readable stream.
@@ -371,47 +356,4 @@ private function createStream($resource)
371
356
372
357
throw new \InvalidArgumentException (sprintf ('First argument to "%s::createStream()" must be a string, resource or StreamInterface. ' , __CLASS__ ));
373
358
}
374
-
375
- /**
376
- * Setup the stream buffer size limit. PHP will allocate buffer
377
- * in memory if the size of the stream is smaller than this size.
378
- * Otherwise, PHP will store the stream data in a temporary file.
379
- *
380
- * @param int $size size of stream data buffered (in bytes)
381
- * until using temporary file to buffer
382
- */
383
- public function setBufferMaxMemory (int $ size ): MultipartStreamBuilder
384
- {
385
- $ this ->bufferMaxMemory = $ size ;
386
-
387
- return $ this ;
388
- }
389
-
390
- /**
391
- * Estimate the available memory in the system by php.ini memory_limit
392
- * and memory_get_usage(). If memory_limit is "-1", the default estimation
393
- * would be 100MB.
394
- *
395
- * @throws \Exception if the ini format does not match expectation
396
- */
397
- protected static function getAvailableMemory (): int
398
- {
399
- $ memory_limit = ini_get ('memory_limit ' );
400
- if ('-1 ' === $ memory_limit ) {
401
- // If there is no memory limit, return 100MB by default.
402
- return 100 * 1024 * 1024 ;
403
- }
404
- if (!preg_match ('/^(\d+)(G|M|K|)$/ ' , $ memory_limit , $ matches )) {
405
- throw new \Exception ("Unknown memory_limit format: {$ memory_limit }" );
406
- }
407
- if ('G ' === $ matches [2 ]) {
408
- $ memory_limit = $ matches [1 ] * 1024 * 1024 * 1024 ; // nnnG -> nnn GB
409
- } elseif ('M ' === $ matches [2 ]) {
410
- $ memory_limit = $ matches [1 ] * 1024 * 1024 ; // nnnM -> nnn MB
411
- } elseif ('K ' === $ matches [2 ]) {
412
- $ memory_limit = $ matches [1 ] * 1024 ; // nnnK -> nnn KB
413
- }
414
-
415
- return (int ) $ memory_limit - \memory_get_usage ();
416
- }
417
359
}
0 commit comments