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