1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\Manager; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Exception\IOException; |
6
|
|
|
use Box\Spout\Writer\Common\Helper\FileSystemWithRootFolderHelperInterface; |
7
|
|
|
use Box\Spout\Writer\Common\Helper\StyleHelperInterface; |
8
|
|
|
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface; |
9
|
|
|
use Box\Spout\Writer\Common\Options; |
10
|
|
|
use Box\Spout\Writer\Common\Sheet; |
11
|
|
|
use Box\Spout\Writer\Entity\Workbook; |
12
|
|
|
use Box\Spout\Writer\Entity\Worksheet; |
13
|
|
|
use Box\Spout\Writer\Exception\SheetNotFoundException; |
14
|
|
|
use Box\Spout\Writer\Exception\WriterException; |
15
|
|
|
use Box\Spout\Writer\Factory\EntityFactory; |
16
|
|
|
use Box\Spout\Writer\Style\Style; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class WorkbookManagerAbstract |
20
|
|
|
* Abstract workbook manager, providing the generic interfaces to work with workbook. |
21
|
|
|
* |
22
|
|
|
* @package Box\Spout\Writer\Manager |
23
|
|
|
*/ |
24
|
|
|
abstract class WorkbookManagerAbstract implements WorkbookManagerInterface |
25
|
|
|
{ |
26
|
|
|
/** @var Workbook The workbook to manage */ |
27
|
|
|
protected $workbook; |
28
|
|
|
|
29
|
|
|
/** @var OptionsManagerInterface */ |
30
|
|
|
protected $optionManager; |
31
|
|
|
|
32
|
|
|
/** @var WorksheetManagerInterface */ |
33
|
|
|
protected $worksheetManager; |
34
|
|
|
|
35
|
|
|
/** @var StyleHelperInterface */ |
36
|
|
|
protected $styleHelper; |
37
|
|
|
|
38
|
|
|
/** @var FileSystemWithRootFolderHelperInterface Helper to perform file system operations */ |
39
|
|
|
protected $fileSystemHelper; |
40
|
|
|
|
41
|
|
|
/** @var EntityFactory Factory to create entities */ |
42
|
|
|
private $entityFactory; |
43
|
|
|
|
44
|
|
|
/** @var Worksheet The worksheet where data will be written to */ |
45
|
|
|
protected $currentWorksheet; |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Workbook $workbook |
50
|
|
|
* @param OptionsManagerInterface $optionsManager |
51
|
|
|
* @param WorksheetManagerInterface $worksheetManager |
52
|
|
|
* @param StyleHelperInterface $styleHelper |
53
|
|
|
* @param FileSystemWithRootFolderHelperInterface $fileSystemHelper |
54
|
|
|
* @param EntityFactory $entityFactory |
55
|
|
|
*/ |
56
|
89 |
|
public function __construct( |
57
|
|
|
Workbook $workbook, |
58
|
|
|
OptionsManagerInterface $optionsManager, |
59
|
|
|
WorksheetManagerInterface $worksheetManager, |
60
|
|
|
StyleHelperInterface $styleHelper, |
61
|
|
|
FileSystemWithRootFolderHelperInterface $fileSystemHelper, |
62
|
|
|
EntityFactory $entityFactory) |
63
|
|
|
{ |
64
|
89 |
|
$this->workbook = $workbook; |
65
|
89 |
|
$this->optionManager = $optionsManager; |
66
|
89 |
|
$this->worksheetManager = $worksheetManager; |
67
|
89 |
|
$this->styleHelper = $styleHelper; |
68
|
89 |
|
$this->fileSystemHelper = $fileSystemHelper; |
69
|
89 |
|
$this->entityFactory = $entityFactory; |
70
|
89 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return int Maximum number of rows/columns a sheet can contain |
74
|
|
|
*/ |
75
|
|
|
abstract protected function getMaxRowsPerWorksheet(); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Sheet $sheet |
79
|
|
|
* @return string The file path where the data for the given sheet will be stored |
80
|
|
|
*/ |
81
|
|
|
abstract protected function getWorksheetFilePath(Sheet $sheet); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return Workbook |
85
|
|
|
*/ |
86
|
70 |
|
public function getWorkbook() |
87
|
|
|
{ |
88
|
70 |
|
return $this->workbook; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Creates a new sheet in the workbook and make it the current sheet. |
93
|
|
|
* The writing will resume where it stopped (i.e. data won't be truncated). |
94
|
|
|
* |
95
|
|
|
* @return Worksheet The created sheet |
96
|
|
|
* @throws IOException If unable to open the sheet for writing |
97
|
|
|
*/ |
98
|
89 |
|
public function addNewSheetAndMakeItCurrent() |
99
|
|
|
{ |
100
|
89 |
|
$worksheet = $this->addNewSheet(); |
101
|
89 |
|
$this->setCurrentWorksheet($worksheet); |
102
|
|
|
|
103
|
89 |
|
return $worksheet; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Creates a new sheet in the workbook. The current sheet remains unchanged. |
108
|
|
|
* |
109
|
|
|
* @return Worksheet The created sheet |
110
|
|
|
* @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing |
111
|
|
|
*/ |
112
|
89 |
|
private function addNewSheet() |
113
|
|
|
{ |
114
|
89 |
|
$worksheets = $this->getWorksheets(); |
115
|
|
|
|
116
|
89 |
|
$newSheetIndex = count($worksheets); |
117
|
89 |
|
$sheet = new Sheet($newSheetIndex, $this->workbook->getInternalId()); |
118
|
|
|
|
119
|
89 |
|
$worksheetFilePath = $this->getWorksheetFilePath($sheet); |
120
|
89 |
|
$worksheet = $this->entityFactory->createWorksheet($worksheetFilePath, $sheet); |
121
|
|
|
|
122
|
89 |
|
$this->worksheetManager->startSheet($worksheet); |
123
|
|
|
|
124
|
89 |
|
$worksheets[] = $worksheet; |
125
|
89 |
|
$this->workbook->setWorksheets($worksheets); |
126
|
|
|
|
127
|
89 |
|
return $worksheet; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return Worksheet[] All the workbook's sheets |
132
|
|
|
*/ |
133
|
89 |
|
public function getWorksheets() |
134
|
|
|
{ |
135
|
89 |
|
return $this->workbook->getWorksheets(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the current sheet |
140
|
|
|
* |
141
|
|
|
* @return Worksheet The current sheet |
142
|
|
|
*/ |
143
|
70 |
|
public function getCurrentWorksheet() |
144
|
|
|
{ |
145
|
70 |
|
return $this->currentWorksheet; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Sets the given sheet as the current one. New data will be written to this sheet. |
150
|
|
|
* The writing will resume where it stopped (i.e. data won't be truncated). |
151
|
|
|
* |
152
|
|
|
* @param Sheet $sheet The "external" sheet to set as current |
153
|
|
|
* @return void |
154
|
|
|
* @throws SheetNotFoundException If the given sheet does not exist in the workbook |
155
|
|
|
*/ |
156
|
4 |
|
public function setCurrentSheet(Sheet $sheet) |
157
|
|
|
{ |
158
|
4 |
|
$worksheet = $this->getWorksheetFromExternalSheet($sheet); |
159
|
4 |
|
if ($worksheet !== null) { |
160
|
4 |
|
$this->currentWorksheet = $worksheet; |
161
|
|
|
} else { |
162
|
|
|
throw new SheetNotFoundException('The given sheet does not exist in the workbook.'); |
163
|
|
|
} |
164
|
4 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param Worksheet $worksheet |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
89 |
|
private function setCurrentWorksheet($worksheet) |
171
|
|
|
{ |
172
|
89 |
|
$this->currentWorksheet = $worksheet; |
173
|
89 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns the worksheet associated to the given external sheet. |
177
|
|
|
* |
178
|
|
|
* @param Sheet $sheet |
179
|
|
|
* @return Worksheet|null The worksheet associated to the given external sheet or null if not found. |
180
|
|
|
*/ |
181
|
4 |
|
private function getWorksheetFromExternalSheet($sheet) |
182
|
|
|
{ |
183
|
4 |
|
$worksheetFound = null; |
184
|
|
|
|
185
|
4 |
|
foreach ($this->getWorksheets() as $worksheet) { |
186
|
4 |
|
if ($worksheet->getExternalSheet() === $sheet) { |
187
|
4 |
|
$worksheetFound = $worksheet; |
188
|
4 |
|
break; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
4 |
|
return $worksheetFound; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Adds data to the current sheet. |
197
|
|
|
* If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination |
198
|
|
|
* with the creation of new worksheets if one worksheet has reached its maximum capicity. |
199
|
|
|
* |
200
|
|
|
* @param array $dataRow Array containing data to be written. Cannot be empty. |
201
|
|
|
* Example $dataRow = ['data1', 1234, null, '', 'data5']; |
202
|
|
|
* @param Style $style Style to be applied to the row. |
203
|
|
|
* @return void |
204
|
|
|
* @throws IOException If trying to create a new sheet and unable to open the sheet for writing |
205
|
|
|
* @throws WriterException If unable to write data |
206
|
|
|
*/ |
207
|
64 |
|
public function addRowToCurrentWorksheet($dataRow, Style $style) |
208
|
|
|
{ |
209
|
64 |
|
$currentWorksheet = $this->getCurrentWorksheet(); |
210
|
64 |
|
$hasReachedMaxRows = $this->hasCurrentWorkseetReachedMaxRows(); |
211
|
|
|
|
212
|
|
|
// if we reached the maximum number of rows for the current sheet... |
213
|
64 |
|
if ($hasReachedMaxRows) { |
214
|
|
|
// ... continue writing in a new sheet if option set |
215
|
4 |
|
if ($this->optionManager->getOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY)) { |
216
|
2 |
|
$currentWorksheet = $this->addNewSheetAndMakeItCurrent(); |
217
|
|
|
|
218
|
2 |
|
$this->addRowWithStyleToWorksheet($currentWorksheet, $dataRow, $style); |
219
|
2 |
|
} else { |
220
|
|
|
// otherwise, do nothing as the data won't be written anyways |
221
|
|
|
} |
222
|
|
|
} else { |
223
|
64 |
|
$this->addRowWithStyleToWorksheet($currentWorksheet, $dataRow, $style); |
224
|
|
|
} |
225
|
61 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return bool Whether the current worksheet has reached the maximum number of rows per sheet. |
229
|
|
|
*/ |
230
|
64 |
|
private function hasCurrentWorkseetReachedMaxRows() |
231
|
|
|
{ |
232
|
64 |
|
$currentWorksheet = $this->getCurrentWorksheet(); |
233
|
64 |
|
return ($currentWorksheet->getLastWrittenRowIndex() >= $this->getMaxRowsPerWorksheet()); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Adds data with the given style to the given sheet. |
238
|
|
|
* |
239
|
|
|
* @param Worksheet $worksheet Worksheet to write the row to |
240
|
|
|
* @param array $dataRow Array containing data to be written. Cannot be empty. |
241
|
|
|
* Example $dataRow = ['data1', 1234, null, '', 'data5']; |
242
|
|
|
* @param Style $style Style to be applied to the row. |
243
|
|
|
* @return void |
244
|
|
|
* @throws WriterException If unable to write data |
245
|
|
|
*/ |
246
|
64 |
|
private function addRowWithStyleToWorksheet(Worksheet $worksheet, $dataRow, Style $style) |
247
|
|
|
{ |
248
|
64 |
|
$updatedStyle = $this->styleHelper->applyExtraStylesIfNeeded($style, $dataRow); |
249
|
64 |
|
$registeredStyle = $this->styleHelper->registerStyle($updatedStyle); |
250
|
64 |
|
$this->worksheetManager->addRow($worksheet, $dataRow, $registeredStyle); |
251
|
|
|
|
252
|
|
|
// update max num columns for the worksheet |
253
|
61 |
|
$currentMaxNumColumns = $worksheet->getMaxNumColumns(); |
254
|
61 |
|
$cellsCount = count($dataRow); |
255
|
61 |
|
$worksheet->setMaxNumColumns(max($currentMaxNumColumns, $cellsCount)); |
256
|
61 |
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Closes the workbook and all its associated sheets. |
260
|
|
|
* All the necessary files are written to disk and zipped together to create the final file. |
261
|
|
|
* All the temporary files are then deleted. |
262
|
|
|
* |
263
|
|
|
* @param resource $finalFilePointer Pointer to the spreadsheet that will be created |
264
|
|
|
* @return void |
265
|
|
|
*/ |
266
|
70 |
|
public function close($finalFilePointer) |
267
|
|
|
{ |
268
|
70 |
|
$this->closeAllWorksheets(); |
269
|
70 |
|
$this->closeRemainingObjects(); |
270
|
70 |
|
$this->writeAllFilesToDiskAndZipThem($finalFilePointer); |
271
|
70 |
|
$this->cleanupTempFolder(); |
272
|
70 |
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Closes custom objects that are still opened |
276
|
|
|
* |
277
|
|
|
* @return void |
278
|
|
|
*/ |
279
|
34 |
|
protected function closeRemainingObjects() |
280
|
|
|
{ |
281
|
|
|
// do nothing by default |
282
|
34 |
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Writes all the necessary files to disk and zip them together to create the final file. |
286
|
|
|
* |
287
|
|
|
* @param resource $finalFilePointer Pointer to the spreadsheet that will be created |
288
|
|
|
* @return void |
289
|
|
|
*/ |
290
|
|
|
abstract protected function writeAllFilesToDiskAndZipThem($finalFilePointer); |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Closes all workbook's associated sheets. |
294
|
|
|
* |
295
|
|
|
* @return void |
296
|
|
|
*/ |
297
|
70 |
|
private function closeAllWorksheets() |
298
|
|
|
{ |
299
|
70 |
|
$worksheets = $this->getWorksheets(); |
300
|
|
|
|
301
|
70 |
|
foreach ($worksheets as $worksheet) { |
302
|
70 |
|
$this->worksheetManager->close($worksheet); |
303
|
|
|
} |
304
|
70 |
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Deletes the root folder created in the temp folder and all its contents. |
308
|
|
|
* |
309
|
|
|
* @return void |
310
|
|
|
*/ |
311
|
70 |
|
protected function cleanupTempFolder() |
312
|
|
|
{ |
313
|
70 |
|
$rootFolder = $this->fileSystemHelper->getRootFolder(); |
314
|
70 |
|
$this->fileSystemHelper->deleteFolderRecursively($rootFolder); |
315
|
|
|
} |
316
|
|
|
} |