|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OneSheet; |
|
4
|
|
|
|
|
5
|
|
|
use OneSheet\Style\Styler; |
|
6
|
|
|
use OneSheet\Xml\DefaultXml; |
|
7
|
|
|
use OneSheet\Xml\SheetXml; |
|
8
|
|
|
|
|
9
|
|
|
class Finalizer |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Sheet[] |
|
13
|
|
|
*/ |
|
14
|
|
|
private $sheets; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var SheetFile[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private $sheetFiles; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \ZipArchive |
|
23
|
|
|
*/ |
|
24
|
|
|
private $zip; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Styler |
|
28
|
|
|
*/ |
|
29
|
|
|
private $styler; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Workbook |
|
33
|
|
|
*/ |
|
34
|
|
|
private $workbook; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Workbook $workbook |
|
38
|
|
|
* @param Sheet[] $sheets |
|
39
|
|
|
* @param Styler $styler |
|
40
|
|
|
* @param SheetFile[] $sheetFiles |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function __construct(array $sheets, Styler $styler, array $sheetFiles, Workbook $workbook) |
|
43
|
|
|
{ |
|
44
|
4 |
|
$this->sheets = $sheets; |
|
45
|
4 |
|
$this->styler = $styler; |
|
46
|
4 |
|
$this->sheetFiles = $sheetFiles; |
|
47
|
4 |
|
$this->workbook = $workbook; |
|
48
|
4 |
|
$this->zip = new \ZipArchive(); |
|
49
|
4 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Finalize the xlsx file. |
|
53
|
|
|
* |
|
54
|
|
|
* @param resource $output |
|
55
|
|
|
*/ |
|
56
|
4 |
|
public function finalize($output) |
|
57
|
|
|
{ |
|
58
|
4 |
|
$zipFileUrl = sys_get_temp_dir() . '/' . uniqid(); |
|
59
|
|
|
|
|
60
|
4 |
|
$this->fillZipWithFileContents($zipFileUrl); |
|
61
|
4 |
|
if (!$this->zip->close()) { |
|
62
|
|
|
throw new \RuntimeException('Failed to close zip file!'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
4 |
|
$this->copyToOutputAndCleanup($output, $zipFileUrl); |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Add all file and string contents to zip file. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $zipFileUrl |
|
72
|
|
|
*/ |
|
73
|
4 |
|
private function fillZipWithFileContents($zipFileUrl) |
|
74
|
|
|
{ |
|
75
|
4 |
|
$this->zip->open($zipFileUrl, \ZipArchive::CREATE); |
|
76
|
4 |
|
foreach ($this->sheets as $sheetId => $sheet) { |
|
77
|
4 |
|
$this->finalizeSheet($this->sheetFiles[$sheetId], $sheet, $sheetId); |
|
78
|
|
|
} |
|
79
|
4 |
|
$this->finalizeWorkbook(array_keys($this->sheets)); |
|
80
|
4 |
|
$this->finalizeStyles(); |
|
81
|
4 |
|
$this->finalizeDefaultXmls(); |
|
82
|
4 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param SheetFile $sheetFile |
|
86
|
|
|
* @param Sheet $sheet |
|
87
|
|
|
* @param string $sheetId |
|
88
|
|
|
*/ |
|
89
|
4 |
|
private function finalizeSheet($sheetFile, $sheet, $sheetId) |
|
90
|
|
|
{ |
|
91
|
4 |
|
$sheetFile->fwrite('</sheetData></worksheet>'); |
|
92
|
4 |
|
$sheetFile->rewind(); |
|
93
|
4 |
|
$sheetFile->fwrite(SheetXml::HEADER_XML); |
|
94
|
4 |
|
$sheetFile->fwrite($sheet->getDimensionXml()); |
|
95
|
4 |
|
$sheetFile->fwrite($sheet->getSheetViewsXml()); |
|
96
|
4 |
|
$sheetFile->fwrite($sheet->getColsXml()); |
|
97
|
4 |
|
$this->zip->addFile($sheetFile->getFilePath(), "xl/worksheets/{$sheetId}.xml"); |
|
98
|
4 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Write style xml file. |
|
102
|
|
|
*/ |
|
103
|
4 |
|
private function finalizeStyles() |
|
104
|
|
|
{ |
|
105
|
4 |
|
$this->zip->addFromString('xl/styles.xml', $this->styler->getStyleSheetXml()); |
|
106
|
4 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Write workbook file. |
|
110
|
|
|
* @param array $sheetIds |
|
111
|
|
|
*/ |
|
112
|
4 |
|
private function finalizeWorkbook(array $sheetIds) |
|
113
|
|
|
{ |
|
114
|
4 |
|
$this->zip->addFromString('xl/workbook.xml', $this->workbook->getWorkbookXml($sheetIds)); |
|
115
|
4 |
|
$this->zip->addFromString('xl/_rels/workbook.xml.rels', $this->workbook->getWorkbookRelsXml($sheetIds)); |
|
116
|
4 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Add default xmls to zip archive. |
|
120
|
|
|
*/ |
|
121
|
4 |
|
private function finalizeDefaultXmls() |
|
122
|
|
|
{ |
|
123
|
4 |
|
$this->zip->addFromString('[Content_Types].xml', DefaultXml::CONTENT_TYPES); |
|
124
|
4 |
|
$this->zip->addFromString('docProps/core.xml', |
|
125
|
4 |
|
sprintf(DefaultXml::DOCPROPS_CORE, date(DATE_W3C), date(DATE_W3C))); |
|
126
|
4 |
|
$this->zip->addFromString('docProps/app.xml', DefaultXml::DOCPROPS_APP); |
|
127
|
4 |
|
$this->zip->addFromString('_rels/.rels', DefaultXml::RELS_RELS); |
|
128
|
4 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Write zip/xlsx contents to specified output |
|
132
|
|
|
* and unlink/delete files. |
|
133
|
|
|
* |
|
134
|
|
|
* @param resource $output |
|
135
|
|
|
* @param string $zipFileUrl |
|
136
|
|
|
*/ |
|
137
|
4 |
|
private function copyToOutputAndCleanup($output, $zipFileUrl) |
|
138
|
|
|
{ |
|
139
|
4 |
|
$zipFilePointer = fopen($zipFileUrl, 'r'); |
|
140
|
4 |
|
if (!stream_copy_to_stream($zipFilePointer, $output) |
|
141
|
4 |
|
|| !fclose($zipFilePointer) |
|
142
|
4 |
|
|| !fclose($output) |
|
143
|
4 |
|
|| !unlink($zipFileUrl) |
|
144
|
|
|
) { |
|
145
|
|
|
throw new \RuntimeException("Failed to copy stream and clean up!"); |
|
146
|
|
|
} |
|
147
|
4 |
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|