Skip to content

Commit 4edfeb2

Browse files
committed
Reduced memory consumption
1 parent 530593a commit 4edfeb2

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

src/Http/CurlDispatcher.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
use Psr\Http\Message\RequestInterface;
88
use Psr\Http\Message\ResponseFactoryInterface;
99
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\StreamInterface;
1011

1112
/**
1213
* Class to fetch html pages
1314
*/
1415
final class CurlDispatcher
1516
{
17+
private static int $contentLengthThreshold = 5000000;
18+
1619
private RequestInterface $request;
1720
private $curl;
18-
private $result;
1921
private array $headers = [];
2022
private $isBinary = false;
21-
private $body;
23+
private ?StreamInterface $body = null;
2224
private ?int $error = null;
2325
private array $settings;
2426

@@ -136,7 +138,9 @@ private function exec(ResponseFactoryInterface $responseFactory): ResponseInterf
136138

137139
if ($this->body) {
138140
//5Mb max
139-
$response->getBody()->write(stream_get_contents($this->body, 5000000, 0));
141+
$this->body->rewind();
142+
$response = $response->withBody($this->body);
143+
$this->body = null;
140144
}
141145

142146
return $response;
@@ -199,9 +203,13 @@ private function writeBody($curl, $string): int
199203
}
200204

201205
if (!$this->body) {
202-
$this->body = fopen('php://temp', 'w+');
206+
$this->body = FactoryDiscovery::getStreamFactory()->createStreamFromFile('php://temp', 'w+');
207+
}
208+
209+
if ($this->body->getSize() > self::$contentLengthThreshold) {
210+
return count($string);
203211
}
204212

205-
return fwrite($this->body, $string);
213+
return $this->body->write($string);
206214
}
207215
}

src/Http/FactoryDiscovery.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Psr\Http\Message\RequestFactoryInterface;
77
use Psr\Http\Message\ResponseFactoryInterface;
8+
use Psr\Http\Message\StreamFactoryInterface;
89
use Psr\Http\Message\UriFactoryInterface;
910
use RuntimeException;
1011

@@ -34,6 +35,14 @@ abstract class FactoryDiscovery
3435
'Sunrise\Http\Message\UriFactory',
3536
];
3637

38+
private const STREAM = [
39+
'Laminas\Diactoros\StreamFactory',
40+
'GuzzleHttp\Psr7\HttpFactory',
41+
'Slim\Psr7\Factory\StreamFactory',
42+
'Nyholm\Psr7\Factory\Psr17Factory',
43+
'Sunrise\Http\Message\StreamFactory',
44+
];
45+
3746
public static function getRequestFactory(): RequestFactoryInterface
3847
{
3948
if ($class = self::searchClass(self::REQUEST)) {
@@ -57,6 +66,17 @@ public static function getUriFactory(): UriFactoryInterface
5766
if ($class = self::searchClass(self::URI)) {
5867
return new $class();
5968
}
69+
70+
throw new RuntimeException('No UriFactoryInterface detected');
71+
}
72+
73+
public static function getStreamFactory(): StreamFactoryInterface
74+
{
75+
if ($class = self::searchClass(self::STREAM)) {
76+
return new $class();
77+
}
78+
79+
throw new RuntimeException('No StreamFactoryInterface detected');
6080
}
6181

6282
private static function searchClass($classes): ?string

0 commit comments

Comments
 (0)