8
8
use PhpList \Core \Domain \Subscription \Model \Subscriber ;
9
9
use PhpList \Core \Domain \Subscription \Repository \SubscriberAttributeDefinitionRepository ;
10
10
use PhpList \Core \Domain \Subscription \Repository \SubscriberRepository ;
11
+ use Symfony \Component \HttpFoundation \BinaryFileResponse ;
11
12
use Symfony \Component \HttpFoundation \Response ;
12
13
use Symfony \Component \HttpFoundation \ResponseHeaderBag ;
13
- use Symfony \Component \HttpFoundation \StreamedResponse ;
14
14
15
15
/**
16
16
* Service for importing and exporting subscribers from/to CSV files.
@@ -36,17 +36,18 @@ public function __construct(
36
36
*
37
37
* @param SubscriberFilter|null $filter Optional filter to apply
38
38
* @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
40
40
*/
41
41
public function exportToCsv (?SubscriberFilter $ filter = null , int $ batchSize = 1000 ): Response
42
42
{
43
43
if ($ filter === null ) {
44
44
$ filter = new SubscriberFilter ();
45
45
}
46
46
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 );
50
51
51
52
return $ this ->configureResponse ($ response );
52
53
}
@@ -56,10 +57,11 @@ public function exportToCsv(?SubscriberFilter $filter = null, int $batchSize = 1
56
57
*
57
58
* @param SubscriberFilter $filter Filter to apply
58
59
* @param int $batchSize Batch size for processing
60
+ * @param string $filePath Path to the file where CSV content will be written
59
61
*/
60
- private function generateCsvContent (SubscriberFilter $ filter , int $ batchSize ): void
62
+ private function generateCsvContent (SubscriberFilter $ filter , int $ batchSize, string $ filePath ): void
61
63
{
62
- $ handle = fopen (' php://output ' , 'w ' );
64
+ $ handle = fopen ($ filePath , 'w ' );
63
65
$ attributeDefinitions = $ this ->definitionRepository ->findAll ();
64
66
65
67
$ headers = $ this ->getExportHeaders ($ attributeDefinitions );
@@ -159,17 +161,18 @@ private function getSubscriberRow(Subscriber $subscriber, array $attributeDefini
159
161
/**
160
162
* Configure the response for CSV download.
161
163
*
162
- * @param StreamedResponse $response The response
164
+ * @param BinaryFileResponse $response The response
163
165
* @return Response The configured response
164
166
*/
165
- private function configureResponse (StreamedResponse $ response ): Response
167
+ private function configureResponse (BinaryFileResponse $ response ): Response
166
168
{
167
169
$ response ->headers ->set ('Content-Type ' , 'text/csv; charset=utf-8 ' );
168
170
$ disposition = $ response ->headers ->makeDisposition (
169
171
ResponseHeaderBag::DISPOSITION_ATTACHMENT ,
170
172
'subscribers_export_ ' . date ('Y-m-d ' ) . '.csv '
171
173
);
172
174
$ response ->headers ->set ('Content-Disposition ' , $ disposition );
175
+ $ response ->deleteFileAfterSend ();
173
176
174
177
return $ response ;
175
178
}
0 commit comments