From 4ec3a21170bc49455f43e49c17fd0e371e032c3d Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Sun, 27 Aug 2017 01:44:20 +0200 Subject: [PATCH] Random DI improvements (#458) * Add random DI improvements Fixing things that were previously missed * Split InternalFactory into Manager and Helper factories --- src/Spout/Common/Creator/HelperFactory.php | 9 ++ .../Reader/ODS/Creator/HelperFactory.php | 5 +- .../Reader/ODS/Helper/SettingsHelper.php | 14 ++- src/Spout/Reader/ODS/SheetIterator.php | 2 +- .../Writer/Common/Creator/EntityFactory.php | 38 ++++---- .../Writer/Common/Creator/ManagerFactory.php | 24 ----- ...erface.php => ManagerFactoryInterface.php} | 10 ++- src/Spout/Writer/Common/Entity/Sheet.php | 2 +- src/Spout/Writer/Common/Helper/ZipHelper.php | 15 +++- .../Manager/WorkbookManagerAbstract.php | 12 ++- .../Writer/ODS/Creator/HelperFactory.php | 58 ++++++++++++ ...InternalFactory.php => ManagerFactory.php} | 89 ++++++++---------- .../Writer/ODS/Manager/WorksheetManager.php | 11 ++- src/Spout/Writer/WriterAbstract.php | 12 ++- src/Spout/Writer/WriterFactory.php | 18 ++-- .../Writer/WriterMultiSheetsAbstract.php | 21 +++-- .../Writer/XLSX/Creator/HelperFactory.php | 59 ++++++++++++ ...InternalFactory.php => ManagerFactory.php} | 90 ++++++++----------- .../Writer/XLSX/Manager/WorksheetManager.php | 11 ++- 19 files changed, 317 insertions(+), 183 deletions(-) delete mode 100644 src/Spout/Writer/Common/Creator/ManagerFactory.php rename src/Spout/Writer/Common/Creator/{InternalFactoryInterface.php => ManagerFactoryInterface.php} (66%) create mode 100644 src/Spout/Writer/ODS/Creator/HelperFactory.php rename src/Spout/Writer/ODS/Creator/{InternalFactory.php => ManagerFactory.php} (54%) create mode 100644 src/Spout/Writer/XLSX/Creator/HelperFactory.php rename src/Spout/Writer/XLSX/Creator/{InternalFactory.php => ManagerFactory.php} (60%) diff --git a/src/Spout/Common/Creator/HelperFactory.php b/src/Spout/Common/Creator/HelperFactory.php index 9b5c793..755fe6c 100644 --- a/src/Spout/Common/Creator/HelperFactory.php +++ b/src/Spout/Common/Creator/HelperFactory.php @@ -5,6 +5,7 @@ namespace Box\Spout\Common\Creator; use Box\Spout\Common\Helper\EncodingHelper; use Box\Spout\Common\Helper\FileSystemHelper; use Box\Spout\Common\Helper\GlobalFunctionsHelper; +use Box\Spout\Common\Helper\StringHelper; /** * Class HelperFactory @@ -39,4 +40,12 @@ class HelperFactory { return new EncodingHelper($globalFunctionsHelper); } + + /** + * @return StringHelper + */ + public function createStringHelper() + { + return new StringHelper(); + } } diff --git a/src/Spout/Reader/ODS/Creator/HelperFactory.php b/src/Spout/Reader/ODS/Creator/HelperFactory.php index 1fad2e9..0109498 100644 --- a/src/Spout/Reader/ODS/Creator/HelperFactory.php +++ b/src/Spout/Reader/ODS/Creator/HelperFactory.php @@ -25,11 +25,12 @@ class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory } /** + * @param EntityFactory $entityFactory * @return SettingsHelper */ - public function createSettingsHelper() + public function createSettingsHelper($entityFactory) { - return new SettingsHelper(); + return new SettingsHelper($entityFactory); } /** diff --git a/src/Spout/Reader/ODS/Helper/SettingsHelper.php b/src/Spout/Reader/ODS/Helper/SettingsHelper.php index a5388ef..a2faf48 100644 --- a/src/Spout/Reader/ODS/Helper/SettingsHelper.php +++ b/src/Spout/Reader/ODS/Helper/SettingsHelper.php @@ -3,6 +3,7 @@ namespace Box\Spout\Reader\ODS\Helper; use Box\Spout\Reader\Exception\XMLProcessingException; +use Box\Spout\Reader\ODS\Creator\EntityFactory; use Box\Spout\Reader\Wrapper\XMLReader; /** @@ -20,13 +21,24 @@ class SettingsHelper const XML_ATTRIBUTE_CONFIG_NAME = 'config:name'; const XML_ATTRIBUTE_VALUE_ACTIVE_TABLE = 'ActiveTable'; + /** @var EntityFactory Factory to create entities */ + private $entityFactory; + + /** + * @param EntityFactory $entityFactory Factory to create entities + */ + public function __construct($entityFactory) + { + $this->entityFactory = $entityFactory; + } + /** * @param string $filePath Path of the file to be read * @return string|null Name of the sheet that was defined as active or NULL if none found */ public function getActiveSheetName($filePath) { - $xmlReader = new XMLReader(); + $xmlReader = $this->entityFactory->createXMLReader(); if ($xmlReader->openFileInZip($filePath, self::SETTINGS_XML_FILE_PATH) === false) { return null; } diff --git a/src/Spout/Reader/ODS/SheetIterator.php b/src/Spout/Reader/ODS/SheetIterator.php index eab49af..2bdc956 100644 --- a/src/Spout/Reader/ODS/SheetIterator.php +++ b/src/Spout/Reader/ODS/SheetIterator.php @@ -63,7 +63,7 @@ class SheetIterator implements IteratorInterface $this->escaper = $helperFactory->createStringsEscaper(); - $settingsHelper = $helperFactory->createSettingsHelper(); + $settingsHelper = $helperFactory->createSettingsHelper($entityFactory); $this->activeSheetName = $settingsHelper->getActiveSheetName($filePath); } diff --git a/src/Spout/Writer/Common/Creator/EntityFactory.php b/src/Spout/Writer/Common/Creator/EntityFactory.php index 0e553a0..b44d099 100644 --- a/src/Spout/Writer/Common/Creator/EntityFactory.php +++ b/src/Spout/Writer/Common/Creator/EntityFactory.php @@ -2,9 +2,11 @@ namespace Box\Spout\Writer\Common\Creator; +use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Workbook; use Box\Spout\Writer\Common\Entity\Worksheet; +use Box\Spout\Writer\Common\Manager\SheetManager; /** * Class EntityFactory @@ -14,19 +16,6 @@ use Box\Spout\Writer\Common\Entity\Worksheet; */ class EntityFactory { - /** @var ManagerFactory */ - private $managerFactory; - - /** - * EntityFactory constructor. - * - * @param ManagerFactory $managerFactory - */ - public function __construct(ManagerFactory $managerFactory) - { - $this->managerFactory = $managerFactory; - } - /** * @return Workbook */ @@ -48,11 +37,28 @@ class EntityFactory /** * @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based) * @param string $associatedWorkbookId ID of the sheet's associated workbook + * @param SheetManager $sheetManager To manage sheets * @return Sheet */ - public function createSheet($sheetIndex, $associatedWorkbookId) + public function createSheet($sheetIndex, $associatedWorkbookId, $sheetManager) { - $sheetManager = $this->managerFactory->createSheetManager(); return new Sheet($sheetIndex, $associatedWorkbookId, $sheetManager); } -} \ No newline at end of file + + /** + * @param mixed $cellValue + * @return Cell + */ + public function createCell($cellValue) + { + return new Cell($cellValue); + } + + /** + * @return \ZipArchive + */ + public function createZipArchive() + { + return new \ZipArchive(); + } +} diff --git a/src/Spout/Writer/Common/Creator/ManagerFactory.php b/src/Spout/Writer/Common/Creator/ManagerFactory.php deleted file mode 100644 index 6aa6939..0000000 --- a/src/Spout/Writer/Common/Creator/ManagerFactory.php +++ /dev/null @@ -1,24 +0,0 @@ -entityFactory = $entityFactory; + } + /** * Returns a new ZipArchive instance pointing at the given path. * @@ -24,7 +37,7 @@ class ZipHelper */ public function createZip($tmpFolderPath) { - $zip = new \ZipArchive(); + $zip = $this->entityFactory->createZipArchive(); $zipFilePath = $tmpFolderPath . self::ZIP_EXTENSION; $zip->open($zipFilePath, \ZipArchive::CREATE|\ZipArchive::OVERWRITE); diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php index 514674b..c9fd024 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php @@ -4,6 +4,7 @@ namespace Box\Spout\Writer\Common\Manager; use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Manager\OptionsManagerInterface; +use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; use Box\Spout\Writer\Common\Helper\FileSystemWithRootFolderHelperInterface; use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Manager\Style\StyleManagerInterface; @@ -41,6 +42,9 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface /** @var EntityFactory Factory to create entities */ protected $entityFactory; + /** @var ManagerFactoryInterface $managerFactory Factory to create managers */ + protected $managerFactory; + /** @var Worksheet The worksheet where data will be written to */ protected $currentWorksheet; @@ -52,6 +56,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface * @param StyleManagerInterface $styleManager * @param FileSystemWithRootFolderHelperInterface $fileSystemHelper * @param EntityFactory $entityFactory + * @param ManagerFactoryInterface $managerFactory */ public function __construct( Workbook $workbook, @@ -59,7 +64,8 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface WorksheetManagerInterface $worksheetManager, StyleManagerInterface $styleManager, FileSystemWithRootFolderHelperInterface $fileSystemHelper, - EntityFactory $entityFactory) + EntityFactory $entityFactory, + ManagerFactoryInterface $managerFactory) { $this->workbook = $workbook; $this->optionManager = $optionsManager; @@ -67,6 +73,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface $this->styleManager = $styleManager; $this->fileSystemHelper = $fileSystemHelper; $this->entityFactory = $entityFactory; + $this->managerFactory = $managerFactory; } /** @@ -114,7 +121,8 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface $worksheets = $this->getWorksheets(); $newSheetIndex = count($worksheets); - $sheet = $this->entityFactory->createSheet($newSheetIndex, $this->workbook->getInternalId()); + $sheetManager = $this->managerFactory->createSheetManager(); + $sheet = $this->entityFactory->createSheet($newSheetIndex, $this->workbook->getInternalId(), $sheetManager); $worksheetFilePath = $this->getWorksheetFilePath($sheet); $worksheet = $this->entityFactory->createWorksheet($worksheetFilePath, $sheet); diff --git a/src/Spout/Writer/ODS/Creator/HelperFactory.php b/src/Spout/Writer/ODS/Creator/HelperFactory.php new file mode 100644 index 0000000..decbe81 --- /dev/null +++ b/src/Spout/Writer/ODS/Creator/HelperFactory.php @@ -0,0 +1,58 @@ +getOption(Options::TEMP_FOLDER); + $zipHelper = $this->createZipHelper($entityFactory); + + return new FileSystemHelper($tempFolder, $zipHelper); + } + + /** + * @param $entityFactory + * @return ZipHelper + */ + private function createZipHelper($entityFactory) + { + return new ZipHelper($entityFactory); + } + + /** + * @return Escaper\ODS + */ + public function createStringsEscaper() + { + return new Escaper\ODS(); + } + + /** + * @return StringHelper + */ + public function createStringHelper() + { + return new StringHelper(); + } +} diff --git a/src/Spout/Writer/ODS/Creator/InternalFactory.php b/src/Spout/Writer/ODS/Creator/ManagerFactory.php similarity index 54% rename from src/Spout/Writer/ODS/Creator/InternalFactory.php rename to src/Spout/Writer/ODS/Creator/ManagerFactory.php index 49ac6d2..e7bc2e7 100644 --- a/src/Spout/Writer/ODS/Creator/InternalFactory.php +++ b/src/Spout/Writer/ODS/Creator/ManagerFactory.php @@ -2,38 +2,38 @@ namespace Box\Spout\Writer\ODS\Creator; -use Box\Spout\Common\Helper\StringHelper; -use Box\Spout\Writer\Common\Helper\ZipHelper; use Box\Spout\Common\Manager\OptionsManagerInterface; -use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Creator\EntityFactory; -use Box\Spout\Writer\Common\Creator\InternalFactoryInterface; -use Box\Spout\Writer\ODS\Helper\FileSystemHelper; +use Box\Spout\Writer\Common\Entity\Options; +use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; +use Box\Spout\Writer\Common\Manager\SheetManager; use Box\Spout\Writer\ODS\Manager\Style\StyleManager; use Box\Spout\Writer\ODS\Manager\Style\StyleRegistry; use Box\Spout\Writer\ODS\Manager\WorkbookManager; use Box\Spout\Writer\ODS\Manager\WorksheetManager; -use Box\Spout\Common\Helper\Escaper; /** - * Class InternalFactory - * Factory for all useful types of objects needed by the ODS Writer + * Class ManagerFactory + * Factory for managers needed by the ODS Writer * * @package Box\Spout\Writer\ODS\Creator */ -class InternalFactory implements InternalFactoryInterface +class ManagerFactory implements ManagerFactoryInterface { /** @var EntityFactory */ - private $entityFactory; + protected $entityFactory; + + /** @var HelperFactory $helperFactory */ + protected $helperFactory; /** - * InternalFactory constructor. - * * @param EntityFactory $entityFactory + * @param HelperFactory $helperFactory */ - public function __construct(EntityFactory $entityFactory) + public function __construct(EntityFactory $entityFactory, HelperFactory $helperFactory) { $this->entityFactory = $entityFactory; + $this->helperFactory = $helperFactory; } /** @@ -44,13 +44,21 @@ class InternalFactory implements InternalFactoryInterface { $workbook = $this->entityFactory->createWorkbook(); - $fileSystemHelper = $this->createFileSystemHelper($optionsManager); + $fileSystemHelper = $this->helperFactory->createSpecificFileSystemHelper($optionsManager, $this->entityFactory); $fileSystemHelper->createBaseFilesAndFolders(); $styleManager = $this->createStyleManager($optionsManager); $worksheetManager = $this->createWorksheetManager(); - return new WorkbookManager($workbook, $optionsManager, $worksheetManager, $styleManager, $fileSystemHelper, $this->entityFactory); + return new WorkbookManager( + $workbook, + $optionsManager, + $worksheetManager, + $styleManager, + $fileSystemHelper, + $this->entityFactory, + $this + ); } /** @@ -58,10 +66,19 @@ class InternalFactory implements InternalFactoryInterface */ private function createWorksheetManager() { - $stringsEscaper = $this->createStringsEscaper(); - $stringsHelper = $this->createStringHelper(); + $stringsEscaper = $this->helperFactory->createStringsEscaper(); + $stringsHelper = $this->helperFactory->createStringHelper(); - return new WorksheetManager($stringsEscaper, $stringsHelper); + return new WorksheetManager($stringsEscaper, $stringsHelper, $this->entityFactory); + } + + /** + * @return SheetManager + */ + public function createSheetManager() + { + $stringHelper = $this->helperFactory->createStringHelper(); + return new SheetManager($stringHelper); } /** @@ -83,40 +100,4 @@ class InternalFactory implements InternalFactoryInterface $defaultRowStyle = $optionsManager->getOption(Options::DEFAULT_ROW_STYLE); return new StyleRegistry($defaultRowStyle); } - - /** - * @param OptionsManagerInterface $optionsManager - * @return FileSystemHelper - */ - public function createFileSystemHelper(OptionsManagerInterface $optionsManager) - { - $tempFolder = $optionsManager->getOption(Options::TEMP_FOLDER); - $zipHelper = $this->createZipHelper(); - - return new FileSystemHelper($tempFolder, $zipHelper); - } - - /** - * @return ZipHelper - */ - private function createZipHelper() - { - return new ZipHelper(); - } - - /** - * @return Escaper\ODS - */ - private function createStringsEscaper() - { - return new Escaper\ODS(); - } - - /** - * @return StringHelper - */ - private function createStringHelper() - { - return new StringHelper(); - } } diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 4638e15..dff564f 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -5,6 +5,7 @@ namespace Box\Spout\Writer\ODS\Manager; use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Helper\StringHelper; +use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; @@ -25,18 +26,24 @@ class WorksheetManager implements WorksheetManagerInterface /** @var StringHelper String helper */ private $stringHelper; + /** @var EntityFactory Factory to create entities */ + private $entityFactory; + /** * WorksheetManager constructor. * * @param \Box\Spout\Common\Helper\Escaper\ODS $stringsEscaper * @param StringHelper $stringHelper + * @param EntityFactory $entityFactory */ public function __construct( \Box\Spout\Common\Helper\Escaper\ODS $stringsEscaper, - StringHelper $stringHelper) + StringHelper $stringHelper, + EntityFactory $entityFactory) { $this->stringsEscaper = $stringsEscaper; $this->stringHelper = $stringHelper; + $this->entityFactory = $entityFactory; } /** @@ -160,7 +167,7 @@ class WorksheetManager implements WorksheetManagerInterface if ($cellValue instanceof Cell) { $cell = $cellValue; } else { - $cell = new Cell($cellValue); + $cell = $this->entityFactory->createCell($cellValue); } if ($cell->isString()) { diff --git a/src/Spout/Writer/WriterAbstract.php b/src/Spout/Writer/WriterAbstract.php index 2c20142..2cb4a00 100644 --- a/src/Spout/Writer/WriterAbstract.php +++ b/src/Spout/Writer/WriterAbstract.php @@ -2,10 +2,10 @@ namespace Box\Spout\Writer; +use Box\Spout\Common\Creator\HelperFactory; use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Exception\SpoutException; -use Box\Spout\Common\Helper\FileSystemHelper; use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Style\Style; @@ -34,6 +34,9 @@ abstract class WriterAbstract implements WriterInterface /** @var GlobalFunctionsHelper Helper to work with global functions */ protected $globalFunctionsHelper; + /** @var HelperFactory $helperFactory */ + protected $helperFactory; + /** @var OptionsManagerInterface Writer options manager */ protected $optionsManager; @@ -50,15 +53,18 @@ abstract class WriterAbstract implements WriterInterface * @param OptionsManagerInterface $optionsManager * @param StyleMerger $styleMerger * @param GlobalFunctionsHelper $globalFunctionsHelper + * @param HelperFactory $helperFactory */ public function __construct( OptionsManagerInterface $optionsManager, StyleMerger $styleMerger, - GlobalFunctionsHelper $globalFunctionsHelper) + GlobalFunctionsHelper $globalFunctionsHelper, + HelperFactory $helperFactory) { $this->optionsManager = $optionsManager; $this->styleMerger = $styleMerger; $this->globalFunctionsHelper = $globalFunctionsHelper; + $this->helperFactory = $helperFactory; $this->resetRowStyleToDefault(); } @@ -372,7 +378,7 @@ abstract class WriterAbstract implements WriterInterface // remove output file if it was created if ($this->globalFunctionsHelper->file_exists($this->outputFilePath)) { $outputFolderPath = dirname($this->outputFilePath); - $fileSystemHelper = new FileSystemHelper($outputFolderPath); + $fileSystemHelper = $this->helperFactory->createFileSystemHelper($outputFolderPath); $fileSystemHelper->deleteFile($this->outputFilePath); } } diff --git a/src/Spout/Writer/WriterFactory.php b/src/Spout/Writer/WriterFactory.php index b10db44..85a680d 100644 --- a/src/Spout/Writer/WriterFactory.php +++ b/src/Spout/Writer/WriterFactory.php @@ -2,11 +2,11 @@ namespace Box\Spout\Writer; +use Box\Spout\Common\Creator\HelperFactory; use Box\Spout\Common\Exception\UnsupportedTypeException; use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Type; use Box\Spout\Writer\Common\Creator\EntityFactory; -use Box\Spout\Writer\Common\Creator\ManagerFactory; use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; use Box\Spout\Writer\Common\Manager\Style\StyleMerger; @@ -47,7 +47,9 @@ class WriterFactory $styleMerger = new StyleMerger(); $globalFunctionsHelper = new GlobalFunctionsHelper(); - return new CSV\Writer($optionsManager, $styleMerger, $globalFunctionsHelper); + $helperFactory = new HelperFactory(); + + return new CSV\Writer($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory); } /** @@ -60,10 +62,10 @@ class WriterFactory $styleMerger = new StyleMerger(); $globalFunctionsHelper = new GlobalFunctionsHelper(); - $entityFactory = new EntityFactory(new ManagerFactory()); - $internalFactory = new XLSX\Creator\InternalFactory($entityFactory); + $helperFactory = new XLSX\Creator\HelperFactory(); + $managerFactory = new XLSX\Creator\ManagerFactory(new EntityFactory(), $helperFactory); - return new XLSX\Writer($optionsManager, $styleMerger, $globalFunctionsHelper, $internalFactory); + return new XLSX\Writer($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory); } /** @@ -76,9 +78,9 @@ class WriterFactory $styleMerger = new StyleMerger(); $globalFunctionsHelper = new GlobalFunctionsHelper(); - $entityFactory = new EntityFactory(new ManagerFactory()); - $internalFactory = new ODS\Creator\InternalFactory($entityFactory); + $helperFactory = new ODS\Creator\HelperFactory(); + $managerFactory = new ODS\Creator\ManagerFactory(new EntityFactory(), $helperFactory); - return new ODS\Writer($optionsManager, $styleMerger, $globalFunctionsHelper, $internalFactory); + return new ODS\Writer($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory); } } diff --git a/src/Spout/Writer/WriterMultiSheetsAbstract.php b/src/Spout/Writer/WriterMultiSheetsAbstract.php index 9d5a0c6..7ecab1a 100644 --- a/src/Spout/Writer/WriterMultiSheetsAbstract.php +++ b/src/Spout/Writer/WriterMultiSheetsAbstract.php @@ -2,6 +2,7 @@ namespace Box\Spout\Writer; +use Box\Spout\Common\Creator\HelperFactory; use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Common\Manager\OptionsManagerInterface; @@ -9,8 +10,9 @@ use Box\Spout\Writer\Common\Entity\Options; use Box\Spout\Writer\Common\Entity\Worksheet; use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Exception\SheetNotFoundException; +use Box\Spout\Writer\Exception\WriterAlreadyOpenedException; use Box\Spout\Writer\Exception\WriterNotOpenedException; -use Box\Spout\Writer\Common\Creator\InternalFactoryInterface; +use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface; /** @@ -21,9 +23,8 @@ use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface; */ abstract class WriterMultiSheetsAbstract extends WriterAbstract { - - /** @var InternalFactoryInterface */ - private $internalFactory; + /** @var ManagerFactoryInterface */ + private $managerFactory; /** @var WorkbookManagerInterface */ private $workbookManager; @@ -32,16 +33,18 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract * @param OptionsManagerInterface $optionsManager * @param StyleMerger $styleMerger * @param GlobalFunctionsHelper $globalFunctionsHelper - * @param InternalFactoryInterface $internalFactory + * @param HelperFactory $helperFactory + * @param ManagerFactoryInterface $managerFactory */ public function __construct( OptionsManagerInterface $optionsManager, StyleMerger $styleMerger, GlobalFunctionsHelper $globalFunctionsHelper, - InternalFactoryInterface $internalFactory) + HelperFactory $helperFactory, + ManagerFactoryInterface $managerFactory) { - parent::__construct($optionsManager, $styleMerger, $globalFunctionsHelper); - $this->internalFactory = $internalFactory; + parent::__construct($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory); + $this->managerFactory = $managerFactory; } /** @@ -70,7 +73,7 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract protected function openWriter() { if (!$this->workbookManager) { - $this->workbookManager = $this->internalFactory->createWorkbookManager($this->optionsManager); + $this->workbookManager = $this->managerFactory->createWorkbookManager($this->optionsManager); $this->workbookManager->addNewSheetAndMakeItCurrent(); } } diff --git a/src/Spout/Writer/XLSX/Creator/HelperFactory.php b/src/Spout/Writer/XLSX/Creator/HelperFactory.php new file mode 100644 index 0000000..1272c74 --- /dev/null +++ b/src/Spout/Writer/XLSX/Creator/HelperFactory.php @@ -0,0 +1,59 @@ +getOption(Options::TEMP_FOLDER); + $zipHelper = $this->createZipHelper($entityFactory); + $escaper = $this->createStringsEscaper(); + + return new FileSystemHelper($tempFolder, $zipHelper, $escaper); + } + + /** + * @param EntityFactory $entityFactory + * @return ZipHelper + */ + private function createZipHelper(EntityFactory $entityFactory) + { + return new ZipHelper($entityFactory); + } + + /** + * @return Escaper\XLSX + */ + public function createStringsEscaper() + { + return new Escaper\XLSX(); + } + + /** + * @return StringHelper + */ + public function createStringHelper() + { + return new StringHelper(); + } +} diff --git a/src/Spout/Writer/XLSX/Creator/InternalFactory.php b/src/Spout/Writer/XLSX/Creator/ManagerFactory.php similarity index 60% rename from src/Spout/Writer/XLSX/Creator/InternalFactory.php rename to src/Spout/Writer/XLSX/Creator/ManagerFactory.php index b96d323..f8d413c 100644 --- a/src/Spout/Writer/XLSX/Creator/InternalFactory.php +++ b/src/Spout/Writer/XLSX/Creator/ManagerFactory.php @@ -2,14 +2,11 @@ namespace Box\Spout\Writer\XLSX\Creator; -use Box\Spout\Common\Helper\Escaper; -use Box\Spout\Common\Helper\StringHelper; use Box\Spout\Writer\Common\Creator\EntityFactory; -use Box\Spout\Writer\Common\Creator\InternalFactoryInterface; +use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; use Box\Spout\Writer\Common\Entity\Options; -use Box\Spout\Writer\Common\Helper\ZipHelper; use Box\Spout\Common\Manager\OptionsManagerInterface; -use Box\Spout\Writer\XLSX\Helper\FileSystemHelper; +use Box\Spout\Writer\Common\Manager\SheetManager; use Box\Spout\Writer\XLSX\Manager\SharedStringsManager; use Box\Spout\Writer\XLSX\Manager\Style\StyleManager; use Box\Spout\Writer\XLSX\Manager\Style\StyleRegistry; @@ -17,24 +14,27 @@ use Box\Spout\Writer\XLSX\Manager\WorkbookManager; use Box\Spout\Writer\XLSX\Manager\WorksheetManager; /** - * Class InternalFactory - * Factory for all useful types of objects needed by the XLSX Writer + * Class ManagerFactory + * Factory for managers needed by the XLSX Writer * * @package Box\Spout\Writer\XLSX\Creator */ -class InternalFactory implements InternalFactoryInterface +class ManagerFactory implements ManagerFactoryInterface { /** @var EntityFactory */ - private $entityFactory; + protected $entityFactory; + + /** @var HelperFactory $helperFactory */ + protected $helperFactory; /** - * InternalFactory constructor. - * * @param EntityFactory $entityFactory + * @param HelperFactory $helperFactory */ - public function __construct(EntityFactory $entityFactory) + public function __construct(EntityFactory $entityFactory, HelperFactory $helperFactory) { $this->entityFactory = $entityFactory; + $this->helperFactory = $helperFactory; } /** @@ -45,7 +45,7 @@ class InternalFactory implements InternalFactoryInterface { $workbook = $this->entityFactory->createWorkbook(); - $fileSystemHelper = $this->createFileSystemHelper($optionsManager); + $fileSystemHelper = $this->helperFactory->createSpecificFileSystemHelper($optionsManager, $this->entityFactory); $fileSystemHelper->createBaseFilesAndFolders(); $xlFolder = $fileSystemHelper->getXlFolder(); @@ -54,7 +54,15 @@ class InternalFactory implements InternalFactoryInterface $styleManager = $this->createStyleManager($optionsManager); $worksheetManager = $this->createWorksheetManager($optionsManager, $styleManager, $sharedStringsManager); - return new WorkbookManager($workbook, $optionsManager, $worksheetManager, $styleManager, $fileSystemHelper, $this->entityFactory); + return new WorkbookManager( + $workbook, + $optionsManager, + $worksheetManager, + $styleManager, + $fileSystemHelper, + $this->entityFactory, + $this + ); } /** @@ -69,10 +77,19 @@ class InternalFactory implements InternalFactoryInterface SharedStringsManager $sharedStringsManager ) { - $stringsEscaper = $this->createStringsEscaper(); - $stringsHelper = $this->createStringHelper(); + $stringsEscaper = $this->helperFactory->createStringsEscaper(); + $stringsHelper = $this->helperFactory->createStringHelper(); - return new WorksheetManager($optionsManager, $styleManager, $sharedStringsManager, $stringsEscaper, $stringsHelper); + return new WorksheetManager($optionsManager, $styleManager, $sharedStringsManager, $stringsEscaper, $stringsHelper, $this->entityFactory); + } + + /** + * @return SheetManager + */ + public function createSheetManager() + { + $stringHelper = $this->helperFactory->createStringHelper(); + return new SheetManager($stringHelper); } /** @@ -101,44 +118,7 @@ class InternalFactory implements InternalFactoryInterface */ private function createSharedStringsManager($xlFolder) { - $stringEscaper = $this->createStringsEscaper(); + $stringEscaper = $this->helperFactory->createStringsEscaper(); return new SharedStringsManager($xlFolder, $stringEscaper); } - - /** - * @param OptionsManagerInterface $optionsManager - * @return FileSystemHelper - */ - private function createFileSystemHelper(OptionsManagerInterface $optionsManager) - { - $tempFolder = $optionsManager->getOption(Options::TEMP_FOLDER); - $zipHelper = $this->createZipHelper(); - $escaper = $this->createStringsEscaper(); - - return new FileSystemHelper($tempFolder, $zipHelper, $escaper); - } - - /** - * @return ZipHelper - */ - private function createZipHelper() - { - return new ZipHelper(); - } - - /** - * @return Escaper\XLSX - */ - private function createStringsEscaper() - { - return new Escaper\XLSX(); - } - - /** - * @return StringHelper - */ - private function createStringHelper() - { - return new StringHelper(); - } } diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index e78c2f9..ab06723 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX\Manager; use Box\Spout\Common\Exception\InvalidArgumentException; use Box\Spout\Common\Exception\IOException; use Box\Spout\Common\Helper\StringHelper; +use Box\Spout\Writer\Common\Creator\EntityFactory; use Box\Spout\Writer\Common\Helper\CellHelper; use Box\Spout\Common\Manager\OptionsManagerInterface; use Box\Spout\Writer\Common\Entity\Options; @@ -50,6 +51,9 @@ EOD; /** @var StringHelper String helper */ private $stringHelper; + /** @var EntityFactory Factory to create entities */ + private $entityFactory; + /** * WorksheetManager constructor. * @@ -58,19 +62,22 @@ EOD; * @param SharedStringsManager $sharedStringsManager * @param \Box\Spout\Common\Helper\Escaper\XLSX $stringsEscaper * @param StringHelper $stringHelper + * @param EntityFactory $entityFactory */ public function __construct( OptionsManagerInterface $optionsManager, StyleManager $styleManager, SharedStringsManager $sharedStringsManager, \Box\Spout\Common\Helper\Escaper\XLSX $stringsEscaper, - StringHelper $stringHelper) + StringHelper $stringHelper, + EntityFactory $entityFactory) { $this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS); $this->styleManager = $styleManager; $this->sharedStringsManager = $sharedStringsManager; $this->stringsEscaper = $stringsEscaper; $this->stringHelper = $stringHelper; + $this->entityFactory = $entityFactory; } /** @@ -200,7 +207,7 @@ EOD; if ($cellValue instanceof Cell) { $cell = $cellValue; } else { - $cell = new Cell($cellValue); + $cell = $this->entityFactory->createCell($cellValue); } if ($cell->isString()) {