Skip to content

Commit f45ef10

Browse files
committed
Remove the memory detection logic from build
* Remove the memory detection logic from MultipartStreamBuilder::build.
1 parent c6ad17a commit f45ef10

File tree

2 files changed

+11
-69
lines changed

2 files changed

+11
-69
lines changed

CHANGELOG.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## 1.2.0 - (unreleased)
44

55
- Refactored MultipartStreamBuilder to clean up and allow injecting data without a filename
6-
- Dynamically use memory or temp file to buffer the stream content. Allow to stream content larger php memory limit.
6+
- Dynamically use memory or temp file to buffer the stream content.
77

88
## 1.1.2 - 2020-07-13
99

@@ -25,23 +25,23 @@ No changes from 0.2.0.
2525
## 0.2.0 - 2017-02-20
2626

2727
You may do a BC update to version 0.2.0 if you are sure that you are not adding
28-
multiple resources with the same name to the Builder.
28+
multiple resources with the same name to the Builder.
2929

3030
### Fixed
3131

32-
- Make sure one can add resources with same name without overwrite.
32+
- Make sure one can add resources with same name without overwrite.
3333

3434
## 0.1.6 - 2017-02-16
3535

3636
### Fixed
3737

38-
- Performance improvements by avoid using `uniqid()`.
38+
- Performance improvements by avoid using `uniqid()`.
3939

4040
## 0.1.5 - 2017-02-14
4141

4242
### Fixed
4343

44-
- Support for non-readable streams. This fix was needed because flaws in Guzzle, Zend and Slims implementations of PSR-7.
44+
- Support for non-readable streams. This fix was needed because flaws in Guzzle, Zend and Slims implementations of PSR-7.
4545

4646
## 0.1.4 - 2016-12-31
4747

@@ -53,7 +53,7 @@ multiple resources with the same name to the Builder.
5353

5454
### Added
5555

56-
- Added `CustomMimetypeHelper` to allow you to configure custom mimetypes.
56+
- Added `CustomMimetypeHelper` to allow you to configure custom mimetypes.
5757

5858
### Changed
5959

@@ -63,13 +63,13 @@ multiple resources with the same name to the Builder.
6363

6464
### Added
6565

66-
- Support for Outlook msg files.
66+
- Support for Outlook msg files.
6767

6868
## 0.1.1 - 2016-08-10
6969

7070
### Added
7171

72-
- Support for Apple passbook.
72+
- Support for Apple passbook.
7373

7474
## 0.1.0 - 2016-07-19
7575

src/MultipartStreamBuilder.php

+3-61
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ class MultipartStreamBuilder
3838
*/
3939
private $data = [];
4040

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-
4841
/**
4942
* @param HttplugStreamFactory|StreamFactoryInterface|null $streamFactory
5043
*/
@@ -138,18 +131,10 @@ public function addResource($name, $resource, array $options = [])
138131
*/
139132
public function build()
140133
{
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-
149134
// Open a temporary read-write stream as buffer.
150135
// If the size is less than predefined limit, things will stay in memory.
151136
// 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+');
153138
foreach ($this->data as $data) {
154139
// Add start and headers
155140
fwrite($buffer, "--{$this->getBoundary()}\r\n".
@@ -166,8 +151,8 @@ public function build()
166151
}
167152
if ($contentStream->isReadable()) {
168153
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));
171156
}
172157
} else {
173158
// Try to getContents for non-readable stream.
@@ -371,47 +356,4 @@ private function createStream($resource)
371356

372357
throw new \InvalidArgumentException(sprintf('First argument to "%s::createStream()" must be a string, resource or StreamInterface.', __CLASS__));
373358
}
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-
}
417359
}

0 commit comments

Comments
 (0)