Skip to content

Commit e60e6f7

Browse files
committed
ISSUE-345: return file
1 parent 44b328a commit e60e6f7

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/Domain/Subscription/Service/SubscriberCsvExportManager.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use PhpList\Core\Domain\Subscription\Model\Subscriber;
99
use PhpList\Core\Domain\Subscription\Repository\SubscriberAttributeDefinitionRepository;
1010
use PhpList\Core\Domain\Subscription\Repository\SubscriberRepository;
11+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1112
use Symfony\Component\HttpFoundation\Response;
1213
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
13-
use Symfony\Component\HttpFoundation\StreamedResponse;
1414

1515
/**
1616
* Service for importing and exporting subscribers from/to CSV files.
@@ -36,17 +36,18 @@ public function __construct(
3636
*
3737
* @param SubscriberFilter|null $filter Optional filter to apply
3838
* @param int $batchSize Number of subscribers to process in each batch
39-
* @return Response A streamed response with the CSV file
39+
* @return Response A response with the CSV file for download
4040
*/
4141
public function exportToCsv(?SubscriberFilter $filter = null, int $batchSize = 1000): Response
4242
{
4343
if ($filter === null) {
4444
$filter = new SubscriberFilter();
4545
}
4646

47-
$response = new StreamedResponse(function () use ($filter, $batchSize) {
48-
$this->generateCsvContent($filter, $batchSize);
49-
});
47+
$tempFilePath = tempnam(sys_get_temp_dir(), 'subscribers_export_');
48+
$this->generateCsvContent($filter, $batchSize, $tempFilePath);
49+
50+
$response = new BinaryFileResponse($tempFilePath);
5051

5152
return $this->configureResponse($response);
5253
}
@@ -56,10 +57,11 @@ public function exportToCsv(?SubscriberFilter $filter = null, int $batchSize = 1
5657
*
5758
* @param SubscriberFilter $filter Filter to apply
5859
* @param int $batchSize Batch size for processing
60+
* @param string $filePath Path to the file where CSV content will be written
5961
*/
60-
private function generateCsvContent(SubscriberFilter $filter, int $batchSize): void
62+
private function generateCsvContent(SubscriberFilter $filter, int $batchSize, string $filePath): void
6163
{
62-
$handle = fopen('php://output', 'w');
64+
$handle = fopen($filePath, 'w');
6365
$attributeDefinitions = $this->definitionRepository->findAll();
6466

6567
$headers = $this->getExportHeaders($attributeDefinitions);
@@ -159,17 +161,18 @@ private function getSubscriberRow(Subscriber $subscriber, array $attributeDefini
159161
/**
160162
* Configure the response for CSV download.
161163
*
162-
* @param StreamedResponse $response The response
164+
* @param BinaryFileResponse $response The response
163165
* @return Response The configured response
164166
*/
165-
private function configureResponse(StreamedResponse $response): Response
167+
private function configureResponse(BinaryFileResponse $response): Response
166168
{
167169
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
168170
$disposition = $response->headers->makeDisposition(
169171
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
170172
'subscribers_export_' . date('Y-m-d') . '.csv'
171173
);
172174
$response->headers->set('Content-Disposition', $disposition);
175+
$response->deleteFileAfterSend();
173176

174177
return $response;
175178
}

0 commit comments

Comments
 (0)