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