|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Palmtree\Csv; |
|
6
|
|
|
|
|
7
|
|
|
class Downloader extends Writer |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Default headers used to tell client the response is a downloadable, |
|
11
|
|
|
* non-cacheable file. |
|
12
|
|
|
* |
|
13
|
|
|
* @var array<string, string> |
|
14
|
|
|
*/ |
|
15
|
|
|
private $responseHeaders = [ |
|
16
|
|
|
'Content-Type' => 'text/csv', |
|
17
|
|
|
'Content-Description' => 'File Transfer', |
|
18
|
|
|
'Content-Transfer-Encoding' => 'Binary', |
|
19
|
|
|
'Expires' => '0', |
|
20
|
|
|
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', |
|
21
|
|
|
'Pragma' => 'public', |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
private string $filename; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(string $filename, iterable $responseHeaders = []) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->filename = $filename; |
|
29
|
|
|
|
|
30
|
|
|
$this->addResponseHeaders($responseHeaders); |
|
31
|
|
|
$this->addResponseHeader('Content-Disposition', sprintf('attachment; filename="%s"', $this->getFilename())); |
|
32
|
|
|
|
|
33
|
|
|
parent::__construct('php://temp'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function download(string $filename, array $data): void |
|
37
|
|
|
{ |
|
38
|
|
|
$downloader = new self($filename); |
|
39
|
|
|
$downloader->setData($data); |
|
40
|
|
|
$downloader->sendResponse(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Attempts to send the file as a download to the client. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function sendResponse(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->getDocument()->trimFinalLineEnding(); |
|
49
|
|
|
|
|
50
|
|
|
if (!headers_sent()) { |
|
51
|
|
|
header(sprintf('Content-Length: %s', $this->getDocument()->getSize())); |
|
52
|
|
|
|
|
53
|
|
|
foreach ($this->getResponseHeaders() as $key => $value) { |
|
54
|
|
|
header("$key: $value"); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->getDocument()->fseek(0); |
|
59
|
|
|
$this->getDocument()->fpassthru(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getResponseHeaders(): array |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->responseHeaders; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function addResponseHeaders(iterable $headers = []): self |
|
68
|
|
|
{ |
|
69
|
|
|
foreach ($headers as $key => $value) { |
|
70
|
|
|
$this->addResponseHeader($key, $value); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function addResponseHeader(string $key, string $value): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->responseHeaders[$key] = $value; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function removeResponseHeader(string $key): void |
|
82
|
|
|
{ |
|
83
|
|
|
unset($this->responseHeaders[$key]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getFilename(): string |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->filename; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|