Skip to content

Commit e8ece71

Browse files
authored
Update ResponseFactory.php
1 parent 2a542b4 commit e8ece71

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/Tqdev/PhpCrudApi/ResponseFactory.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public static function fromXml(int $status, string $xml): ResponseInterface
2323

2424
public static function fromCsv(int $status, string $csv): ResponseInterface
2525
{
26-
$response = self::from($status, 'text/csv', $csv);
27-
return $response->withHeader('Content-Type', 'text/csv');
26+
return self::from($status, 'text/csv', $csv);
2827
}
2928

3029
public static function fromHtml(int $status, string $html): ResponseInterface
@@ -38,6 +37,17 @@ public static function fromObject(int $status, $body): ResponseInterface
3837
return self::from($status, 'application/json', $content);
3938
}
4039

40+
public static function fromFile(int $status, string $filename): ResponseInterface
41+
{
42+
$psr17Factory = new Psr17Factory();
43+
$response = $psr17Factory->createResponse($status);
44+
$stream = $psr17Factory->createStreamFromFile($filename);
45+
$response = $response->withBody($stream);
46+
$response = $response->withHeader('Content-Type', mime_content_type(basename($filename)));
47+
$response = $response->withHeader('Content-Length', filesize($filename));
48+
return $response;
49+
}
50+
4151
private static function from(int $status, string $contentType, string $content): ResponseInterface
4252
{
4353
$psr17Factory = new Psr17Factory();

0 commit comments

Comments
 (0)