Apply default row style in WorkbookManager

Instead of doing it in the Writer
This commit is contained in:
Adrien Loison 2017-11-05 13:59:00 +01:00
parent 3d0f108b1d
commit 8aec9ea992
6 changed files with 50 additions and 37 deletions

View File

@ -7,7 +7,6 @@ use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\CSV\Manager\OptionsManager as CSVOptionsManager; use Box\Spout\Writer\CSV\Manager\OptionsManager as CSVOptionsManager;
use Box\Spout\Writer\CSV\Writer as CSVWriter; use Box\Spout\Writer\CSV\Writer as CSVWriter;
use Box\Spout\Writer\ODS\Creator\HelperFactory as ODSHelperFactory; use Box\Spout\Writer\ODS\Creator\HelperFactory as ODSHelperFactory;
@ -51,12 +50,11 @@ class WriterFactory
private function getCSVWriter() private function getCSVWriter()
{ {
$optionsManager = new CSVOptionsManager(); $optionsManager = new CSVOptionsManager();
$styleMerger = new StyleMerger();
$globalFunctionsHelper = new GlobalFunctionsHelper(); $globalFunctionsHelper = new GlobalFunctionsHelper();
$helperFactory = new HelperFactory(); $helperFactory = new HelperFactory();
return new CSVWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory); return new CSVWriter($optionsManager, $globalFunctionsHelper, $helperFactory);
} }
/** /**
@ -66,13 +64,12 @@ class WriterFactory
{ {
$styleBuilder = new StyleBuilder(); $styleBuilder = new StyleBuilder();
$optionsManager = new XLSXOptionsManager($styleBuilder); $optionsManager = new XLSXOptionsManager($styleBuilder);
$styleMerger = new StyleMerger();
$globalFunctionsHelper = new GlobalFunctionsHelper(); $globalFunctionsHelper = new GlobalFunctionsHelper();
$helperFactory = new XLSXHelperFactory(); $helperFactory = new XLSXHelperFactory();
$managerFactory = new XLSXManagerFactory(new InternalEntityFactory(), $helperFactory); $managerFactory = new XLSXManagerFactory(new InternalEntityFactory(), $helperFactory);
return new XLSXWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory); return new XLSXWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory);
} }
/** /**
@ -82,12 +79,11 @@ class WriterFactory
{ {
$styleBuilder = new StyleBuilder(); $styleBuilder = new StyleBuilder();
$optionsManager = new ODSOptionsManager($styleBuilder); $optionsManager = new ODSOptionsManager($styleBuilder);
$styleMerger = new StyleMerger();
$globalFunctionsHelper = new GlobalFunctionsHelper(); $globalFunctionsHelper = new GlobalFunctionsHelper();
$helperFactory = new ODSHelperFactory(); $helperFactory = new ODSHelperFactory();
$managerFactory = new ODSManagerFactory(new InternalEntityFactory(), $helperFactory); $managerFactory = new ODSManagerFactory(new InternalEntityFactory(), $helperFactory);
return new ODSWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory); return new ODSWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory);
} }
} }

View File

@ -13,6 +13,7 @@ use Box\Spout\Writer\Common\Entity\Workbook;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Helper\FileSystemWithRootFolderHelperInterface; use Box\Spout\Writer\Common\Helper\FileSystemWithRootFolderHelperInterface;
use Box\Spout\Writer\Common\Manager\Style\StyleManagerInterface; use Box\Spout\Writer\Common\Manager\Style\StyleManagerInterface;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\Exception\SheetNotFoundException; use Box\Spout\Writer\Exception\SheetNotFoundException;
use Box\Spout\Writer\Exception\WriterException; use Box\Spout\Writer\Exception\WriterException;
@ -26,7 +27,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
protected $workbook; protected $workbook;
/** @var OptionsManagerInterface */ /** @var OptionsManagerInterface */
protected $optionManager; protected $optionsManager;
/** @var WorksheetManagerInterface */ /** @var WorksheetManagerInterface */
protected $worksheetManager; protected $worksheetManager;
@ -34,6 +35,9 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
/** @var StyleManagerInterface Manages styles */ /** @var StyleManagerInterface Manages styles */
protected $styleManager; protected $styleManager;
/** @var StyleMerger Helper to merge styles */
protected $styleMerger;
/** @var FileSystemWithRootFolderHelperInterface Helper to perform file system operations */ /** @var FileSystemWithRootFolderHelperInterface Helper to perform file system operations */
protected $fileSystemHelper; protected $fileSystemHelper;
@ -51,6 +55,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
* @param OptionsManagerInterface $optionsManager * @param OptionsManagerInterface $optionsManager
* @param WorksheetManagerInterface $worksheetManager * @param WorksheetManagerInterface $worksheetManager
* @param StyleManagerInterface $styleManager * @param StyleManagerInterface $styleManager
* @param StyleMerger $styleMerger
* @param FileSystemWithRootFolderHelperInterface $fileSystemHelper * @param FileSystemWithRootFolderHelperInterface $fileSystemHelper
* @param InternalEntityFactory $entityFactory * @param InternalEntityFactory $entityFactory
* @param ManagerFactoryInterface $managerFactory * @param ManagerFactoryInterface $managerFactory
@ -60,14 +65,16 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
OptionsManagerInterface $optionsManager, OptionsManagerInterface $optionsManager,
WorksheetManagerInterface $worksheetManager, WorksheetManagerInterface $worksheetManager,
StyleManagerInterface $styleManager, StyleManagerInterface $styleManager,
StyleMerger $styleMerger,
FileSystemWithRootFolderHelperInterface $fileSystemHelper, FileSystemWithRootFolderHelperInterface $fileSystemHelper,
InternalEntityFactory $entityFactory, InternalEntityFactory $entityFactory,
ManagerFactoryInterface $managerFactory ManagerFactoryInterface $managerFactory
) { ) {
$this->workbook = $workbook; $this->workbook = $workbook;
$this->optionManager = $optionsManager; $this->optionsManager = $optionsManager;
$this->worksheetManager = $worksheetManager; $this->worksheetManager = $worksheetManager;
$this->styleManager = $styleManager; $this->styleManager = $styleManager;
$this->styleMerger = $styleMerger;
$this->fileSystemHelper = $fileSystemHelper; $this->fileSystemHelper = $fileSystemHelper;
$this->entityFactory = $entityFactory; $this->entityFactory = $entityFactory;
$this->managerFactory = $managerFactory; $this->managerFactory = $managerFactory;
@ -215,7 +222,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
// if we reached the maximum number of rows for the current sheet... // if we reached the maximum number of rows for the current sheet...
if ($hasReachedMaxRows) { if ($hasReachedMaxRows) {
// ... continue writing in a new sheet if option set // ... continue writing in a new sheet if option set
if ($this->optionManager->getOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY)) { if ($this->optionsManager->getOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY)) {
$currentWorksheet = $this->addNewSheetAndMakeItCurrent(); $currentWorksheet = $this->addNewSheetAndMakeItCurrent();
$this->addRowToWorksheet($currentWorksheet, $row); $this->addRowToWorksheet($currentWorksheet, $row);
@ -247,6 +254,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
*/ */
private function addRowToWorksheet(Worksheet $worksheet, Row $row) private function addRowToWorksheet(Worksheet $worksheet, Row $row)
{ {
$this->applyDefaultRowStyle($row);
$this->worksheetManager->addRow($worksheet, $row); $this->worksheetManager->addRow($worksheet, $row);
// update max num columns for the worksheet // update max num columns for the worksheet
@ -255,6 +263,19 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
$worksheet->setMaxNumColumns(max($currentMaxNumColumns, $cellsCount)); $worksheet->setMaxNumColumns(max($currentMaxNumColumns, $cellsCount));
} }
/**
* @param Row $row
*/
private function applyDefaultRowStyle(Row $row)
{
$defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
if ($defaultRowStyle !== null) {
$mergedStyle = $this->styleMerger->merge($row->getStyle(), $defaultRowStyle);
$row->setStyle($mergedStyle);
}
}
/** /**
* Closes the workbook and all its associated sheets. * Closes the workbook and all its associated sheets.
* All the necessary files are written to disk and zipped together to create the final file. * All the necessary files are written to disk and zipped together to create the final file.

View File

@ -7,6 +7,7 @@ use Box\Spout\Writer\Common\Creator\InternalEntityFactory;
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Manager\SheetManager; use Box\Spout\Writer\Common\Manager\SheetManager;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\ODS\Manager\Style\StyleManager; use Box\Spout\Writer\ODS\Manager\Style\StyleManager;
use Box\Spout\Writer\ODS\Manager\Style\StyleRegistry; use Box\Spout\Writer\ODS\Manager\Style\StyleRegistry;
use Box\Spout\Writer\ODS\Manager\WorkbookManager; use Box\Spout\Writer\ODS\Manager\WorkbookManager;
@ -45,6 +46,7 @@ class ManagerFactory implements ManagerFactoryInterface
$fileSystemHelper = $this->helperFactory->createSpecificFileSystemHelper($optionsManager, $this->entityFactory); $fileSystemHelper = $this->helperFactory->createSpecificFileSystemHelper($optionsManager, $this->entityFactory);
$fileSystemHelper->createBaseFilesAndFolders(); $fileSystemHelper->createBaseFilesAndFolders();
$styleMerger = $this->createStyleMerger();
$styleManager = $this->createStyleManager($optionsManager); $styleManager = $this->createStyleManager($optionsManager);
$worksheetManager = $this->createWorksheetManager($styleManager); $worksheetManager = $this->createWorksheetManager($styleManager);
@ -53,6 +55,7 @@ class ManagerFactory implements ManagerFactoryInterface
$optionsManager, $optionsManager,
$worksheetManager, $worksheetManager,
$styleManager, $styleManager,
$styleMerger,
$fileSystemHelper, $fileSystemHelper,
$this->entityFactory, $this->entityFactory,
$this $this
@ -102,4 +105,12 @@ class ManagerFactory implements ManagerFactoryInterface
return new StyleRegistry($defaultRowStyle); return new StyleRegistry($defaultRowStyle);
} }
/**
* @return StyleMerger
*/
private function createStyleMerger()
{
return new StyleMerger();
}
} }

View File

@ -11,7 +11,6 @@ use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Row; use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException; use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\Exception\WriterNotOpenedException;
@ -40,26 +39,20 @@ abstract class WriterAbstract implements WriterInterface
/** @var OptionsManagerInterface Writer options manager */ /** @var OptionsManagerInterface Writer options manager */
protected $optionsManager; protected $optionsManager;
/** @var StyleMerger Helps merge styles together */
protected $styleMerger;
/** @var string Content-Type value for the header - to be defined by child class */ /** @var string Content-Type value for the header - to be defined by child class */
protected static $headerContentType; protected static $headerContentType;
/** /**
* @param OptionsManagerInterface $optionsManager * @param OptionsManagerInterface $optionsManager
* @param StyleMerger $styleMerger
* @param GlobalFunctionsHelper $globalFunctionsHelper * @param GlobalFunctionsHelper $globalFunctionsHelper
* @param HelperFactory $helperFactory * @param HelperFactory $helperFactory
*/ */
public function __construct( public function __construct(
OptionsManagerInterface $optionsManager, OptionsManagerInterface $optionsManager,
StyleMerger $styleMerger,
GlobalFunctionsHelper $globalFunctionsHelper, GlobalFunctionsHelper $globalFunctionsHelper,
HelperFactory $helperFactory HelperFactory $helperFactory
) { ) {
$this->optionsManager = $optionsManager; $this->optionsManager = $optionsManager;
$this->styleMerger = $styleMerger;
$this->globalFunctionsHelper = $globalFunctionsHelper; $this->globalFunctionsHelper = $globalFunctionsHelper;
$this->helperFactory = $helperFactory; $this->helperFactory = $helperFactory;
} }
@ -188,7 +181,6 @@ abstract class WriterAbstract implements WriterInterface
// empty $dataRow should not add an empty line // empty $dataRow should not add an empty line
if ($row->hasCells()) { if ($row->hasCells()) {
try { try {
$this->applyDefaultRowStyle($row);
$this->addRowToWriter($row); $this->addRowToWriter($row);
} catch (SpoutException $e) { } catch (SpoutException $e) {
// if an exception occurs while writing data, // if an exception occurs while writing data,
@ -223,21 +215,6 @@ abstract class WriterAbstract implements WriterInterface
return $this; return $this;
} }
/**
* @TODO: Move this into styleMerger
*
* @param Row $row
*/
private function applyDefaultRowStyle(Row $row)
{
$defaultRowStyle = $this->optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
if ($defaultRowStyle !== null) {
$mergedStyle = $this->styleMerger->merge($row->getStyle(), $defaultRowStyle);
$row->setStyle($mergedStyle);
}
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -10,7 +10,6 @@ use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Entity\Row; use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Entity\Worksheet;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface; use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface;
use Box\Spout\Writer\Exception\SheetNotFoundException; use Box\Spout\Writer\Exception\SheetNotFoundException;
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException; use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
@ -31,19 +30,17 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
/** /**
* @param OptionsManagerInterface $optionsManager * @param OptionsManagerInterface $optionsManager
* @param StyleMerger $styleMerger
* @param GlobalFunctionsHelper $globalFunctionsHelper * @param GlobalFunctionsHelper $globalFunctionsHelper
* @param HelperFactory $helperFactory * @param HelperFactory $helperFactory
* @param ManagerFactoryInterface $managerFactory * @param ManagerFactoryInterface $managerFactory
*/ */
public function __construct( public function __construct(
OptionsManagerInterface $optionsManager, OptionsManagerInterface $optionsManager,
StyleMerger $styleMerger,
GlobalFunctionsHelper $globalFunctionsHelper, GlobalFunctionsHelper $globalFunctionsHelper,
HelperFactory $helperFactory, HelperFactory $helperFactory,
ManagerFactoryInterface $managerFactory ManagerFactoryInterface $managerFactory
) { ) {
parent::__construct($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory); parent::__construct($optionsManager, $globalFunctionsHelper, $helperFactory);
$this->managerFactory = $managerFactory; $this->managerFactory = $managerFactory;
} }

View File

@ -7,6 +7,7 @@ use Box\Spout\Writer\Common\Creator\InternalEntityFactory;
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface;
use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Options;
use Box\Spout\Writer\Common\Manager\SheetManager; use Box\Spout\Writer\Common\Manager\SheetManager;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\XLSX\Manager\SharedStringsManager; use Box\Spout\Writer\XLSX\Manager\SharedStringsManager;
use Box\Spout\Writer\XLSX\Manager\Style\StyleManager; use Box\Spout\Writer\XLSX\Manager\Style\StyleManager;
use Box\Spout\Writer\XLSX\Manager\Style\StyleRegistry; use Box\Spout\Writer\XLSX\Manager\Style\StyleRegistry;
@ -49,6 +50,7 @@ class ManagerFactory implements ManagerFactoryInterface
$xlFolder = $fileSystemHelper->getXlFolder(); $xlFolder = $fileSystemHelper->getXlFolder();
$sharedStringsManager = $this->createSharedStringsManager($xlFolder); $sharedStringsManager = $this->createSharedStringsManager($xlFolder);
$styleMerger = $this->createStyleMerger();
$styleManager = $this->createStyleManager($optionsManager); $styleManager = $this->createStyleManager($optionsManager);
$worksheetManager = $this->createWorksheetManager($optionsManager, $styleManager, $sharedStringsManager); $worksheetManager = $this->createWorksheetManager($optionsManager, $styleManager, $sharedStringsManager);
@ -57,6 +59,7 @@ class ManagerFactory implements ManagerFactoryInterface
$optionsManager, $optionsManager,
$worksheetManager, $worksheetManager,
$styleManager, $styleManager,
$styleMerger,
$fileSystemHelper, $fileSystemHelper,
$this->entityFactory, $this->entityFactory,
$this $this
@ -112,6 +115,14 @@ class ManagerFactory implements ManagerFactoryInterface
return new StyleRegistry($defaultRowStyle); return new StyleRegistry($defaultRowStyle);
} }
/**
* @return StyleMerger
*/
private function createStyleMerger()
{
return new StyleMerger();
}
/** /**
* @param string $xlFolder Path to the "xl" folder * @param string $xlFolder Path to the "xl" folder
* @return SharedStringsManager * @return SharedStringsManager