From d5e4a67d651d34a73da910cf010c92db86e17aea Mon Sep 17 00:00:00 2001 From: Adrien Loison Date: Sat, 26 Aug 2017 23:41:25 +0200 Subject: [PATCH] Add random DI improvements Fixing things that were previously missed --- .../Reader/ODS/Creator/HelperFactory.php | 5 +++-- .../Reader/ODS/Helper/SettingsHelper.php | 14 ++++++++++++- src/Spout/Reader/ODS/SheetIterator.php | 2 +- .../Writer/Common/Creator/EntityFactory.php | 20 ++++++++++++++++++- src/Spout/Writer/Common/Helper/ZipHelper.php | 15 +++++++++++++- .../Writer/ODS/Creator/InternalFactory.php | 4 ++-- .../Writer/ODS/Manager/WorksheetManager.php | 11 ++++++++-- .../Writer/XLSX/Creator/InternalFactory.php | 4 ++-- .../Writer/XLSX/Manager/WorksheetManager.php | 11 ++++++++-- 9 files changed, 72 insertions(+), 14 deletions(-) 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..5d52890 100644 --- a/src/Spout/Writer/Common/Creator/EntityFactory.php +++ b/src/Spout/Writer/Common/Creator/EntityFactory.php @@ -2,6 +2,7 @@ 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; @@ -55,4 +56,21 @@ class EntityFactory $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/Helper/ZipHelper.php b/src/Spout/Writer/Common/Helper/ZipHelper.php index f6fec55..f47a240 100644 --- a/src/Spout/Writer/Common/Helper/ZipHelper.php +++ b/src/Spout/Writer/Common/Helper/ZipHelper.php @@ -2,6 +2,8 @@ namespace Box\Spout\Writer\Common\Helper; +use Box\Spout\Writer\Common\Creator\EntityFactory; + /** * Class ZipHelper * This class provides helper functions to create zip files @@ -16,6 +18,17 @@ class ZipHelper const EXISTING_FILES_SKIP = 'skip'; const EXISTING_FILES_OVERWRITE = 'overwrite'; + /** @var EntityFactory Factory to create entities */ + private $entityFactory; + + /** + * @param EntityFactory $entityFactory Factory to create entities + */ + public function __construct($entityFactory) + { + $this->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/ODS/Creator/InternalFactory.php b/src/Spout/Writer/ODS/Creator/InternalFactory.php index 49ac6d2..6920b4a 100644 --- a/src/Spout/Writer/ODS/Creator/InternalFactory.php +++ b/src/Spout/Writer/ODS/Creator/InternalFactory.php @@ -61,7 +61,7 @@ class InternalFactory implements InternalFactoryInterface $stringsEscaper = $this->createStringsEscaper(); $stringsHelper = $this->createStringHelper(); - return new WorksheetManager($stringsEscaper, $stringsHelper); + return new WorksheetManager($stringsEscaper, $stringsHelper, $this->entityFactory); } /** @@ -101,7 +101,7 @@ class InternalFactory implements InternalFactoryInterface */ private function createZipHelper() { - return new ZipHelper(); + return new ZipHelper($this->entityFactory); } /** 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/XLSX/Creator/InternalFactory.php b/src/Spout/Writer/XLSX/Creator/InternalFactory.php index b96d323..867c4a4 100644 --- a/src/Spout/Writer/XLSX/Creator/InternalFactory.php +++ b/src/Spout/Writer/XLSX/Creator/InternalFactory.php @@ -72,7 +72,7 @@ class InternalFactory implements InternalFactoryInterface $stringsEscaper = $this->createStringsEscaper(); $stringsHelper = $this->createStringHelper(); - return new WorksheetManager($optionsManager, $styleManager, $sharedStringsManager, $stringsEscaper, $stringsHelper); + return new WorksheetManager($optionsManager, $styleManager, $sharedStringsManager, $stringsEscaper, $stringsHelper, $this->entityFactory); } /** @@ -123,7 +123,7 @@ class InternalFactory implements InternalFactoryInterface */ private function createZipHelper() { - return new ZipHelper(); + return new ZipHelper($this->entityFactory); } /** 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()) {