Favor object creation in factories (#459)

Instead of passing factories in the constructors and let objects call the factory method, create all dependencies directly in the factories.
This commit is contained in:
Adrien Loison 2017-08-27 02:40:39 +02:00 committed by GitHub
parent 4ec3a21170
commit 61f2addefa
13 changed files with 84 additions and 114 deletions

View File

@ -37,18 +37,19 @@ class EntityFactory implements EntityFactoryInterface
*/
public function createSheetIterator($filePointer, $optionsManager, $globalFunctionsHelper)
{
return new SheetIterator($filePointer, $optionsManager, $globalFunctionsHelper, $this);
$rowIterator = $this->createRowIterator($filePointer, $optionsManager, $globalFunctionsHelper);
$sheet = $this->createSheet($rowIterator);
return new SheetIterator($sheet);
}
/**
* @param resource $filePointer Pointer to the CSV file to read
* @param OptionsManagerInterface $optionsManager
* @param GlobalFunctionsHelper $globalFunctionsHelper
* @param RowIterator $rowIterator
* @return Sheet
*/
public function createSheet($filePointer, $optionsManager, $globalFunctionsHelper)
private function createSheet($rowIterator)
{
return new Sheet($filePointer, $optionsManager, $globalFunctionsHelper, $this);
return new Sheet($rowIterator);
}
/**
@ -57,7 +58,7 @@ class EntityFactory implements EntityFactoryInterface
* @param GlobalFunctionsHelper $globalFunctionsHelper
* @return RowIterator
*/
public function createRowIterator($filePointer, $optionsManager, $globalFunctionsHelper)
private function createRowIterator($filePointer, $optionsManager, $globalFunctionsHelper)
{
$encodingHelper = $this->helperFactory->createEncodingHelper($globalFunctionsHelper);
return new RowIterator($filePointer, $optionsManager, $encodingHelper, $globalFunctionsHelper);

View File

@ -16,14 +16,11 @@ class Sheet implements SheetInterface
protected $rowIterator;
/**
* @param resource $filePointer Pointer to the CSV file to read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @param EntityFactory $entityFactory Factory to create entities
* @param RowIterator $rowIterator Corresponding row iterator
*/
public function __construct($filePointer, $optionsManager, $globalFunctionsHelper, $entityFactory)
public function __construct(RowIterator $rowIterator)
{
$this->rowIterator = $entityFactory->createRowIterator($filePointer, $optionsManager, $globalFunctionsHelper);
$this->rowIterator = $rowIterator;
}
/**

View File

@ -20,14 +20,11 @@ class SheetIterator implements IteratorInterface
protected $hasReadUniqueSheet = false;
/**
* @param resource $filePointer
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @param EntityFactory $entityFactory Factory to create entities
* @param Sheet $sheet Corresponding unique sheet
*/
public function __construct($filePointer, $optionsManager, $globalFunctionsHelper, $entityFactory)
public function __construct($sheet)
{
$this->sheet = $entityFactory->createSheet($filePointer, $optionsManager, $globalFunctionsHelper);
$this->sheet = $sheet;
}
/**

View File

@ -5,6 +5,7 @@ namespace Box\Spout\Reader\ODS\Creator;
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Manager\OptionsManagerInterface;
use Box\Spout\Reader\Common\Creator\EntityFactoryInterface;
use Box\Spout\Reader\Common\Entity\Options;
use Box\Spout\Reader\Common\XMLProcessor;
use Box\Spout\Reader\ODS\RowIterator;
use Box\Spout\Reader\ODS\Sheet;
@ -37,7 +38,10 @@ class EntityFactory implements EntityFactoryInterface
*/
public function createSheetIterator($filePath, $optionsManager)
{
return new SheetIterator($filePath, $optionsManager, $this, $this->helperFactory);
$escaper = $this->helperFactory->createStringsEscaper();
$settingsHelper = $this->helperFactory->createSettingsHelper($this);
return new SheetIterator($filePath, $optionsManager, $escaper, $settingsHelper, $this);
}
/**
@ -50,7 +54,9 @@ class EntityFactory implements EntityFactoryInterface
*/
public function createSheet($xmlReader, $sheetIndex, $sheetName, $isSheetActive, $optionsManager)
{
return new Sheet($xmlReader, $sheetIndex, $sheetName, $isSheetActive, $optionsManager, $this);
$rowIterator = $this->createRowIterator($xmlReader, $optionsManager);
return new Sheet($rowIterator, $sheetIndex, $sheetName, $isSheetActive);
}
/**
@ -58,9 +64,13 @@ class EntityFactory implements EntityFactoryInterface
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @return RowIterator
*/
public function createRowIterator($xmlReader, $optionsManager)
private function createRowIterator($xmlReader, $optionsManager)
{
return new RowIterator($xmlReader, $optionsManager, $this, $this->helperFactory);
$shouldFormatDates = $optionsManager->getOption(Options::SHOULD_FORMAT_DATES);
$cellValueFormatter = $this->helperFactory->createCellValueFormatter($shouldFormatDates);
$xmlProcessor = $this->createXMLProcessor($xmlReader);
return new RowIterator($xmlReader, $optionsManager, $cellValueFormatter, $xmlProcessor);
}
/**
@ -75,7 +85,7 @@ class EntityFactory implements EntityFactoryInterface
* @param $xmlReader
* @return XMLProcessor
*/
public function createXMLProcessor($xmlReader)
private function createXMLProcessor($xmlReader)
{
return new XMLProcessor($xmlReader);
}

View File

@ -9,6 +9,7 @@ use Box\Spout\Reader\Exception\XMLProcessingException;
use Box\Spout\Reader\IteratorInterface;
use Box\Spout\Reader\ODS\Creator\EntityFactory;
use Box\Spout\Reader\ODS\Creator\HelperFactory;
use Box\Spout\Reader\ODS\Helper\CellValueFormatter;
use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\Reader\Common\XMLProcessor;
@ -75,17 +76,17 @@ class RowIterator implements IteratorInterface
/**
* @param XMLReader $xmlReader XML Reader, positioned on the "<table:table>" element
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param EntityFactory $entityFactory Factory to create entities
* @param HelperFactory $helperFactory Factory to create helpers
* @param CellValueFormatter $cellValueFormatter Helper to format cell values
* @param XMLProcessor $xmlProcessor Helper to process XML files
*/
public function __construct($xmlReader, $optionsManager, $entityFactory, $helperFactory)
public function __construct($xmlReader, $optionsManager, $cellValueFormatter, $xmlProcessor)
{
$this->xmlReader = $xmlReader;
$this->shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS);
$this->cellValueFormatter = $helperFactory->createCellValueFormatter($optionsManager->getOption(Options::SHOULD_FORMAT_DATES));
$this->cellValueFormatter = $cellValueFormatter;
// Register all callbacks to process different nodes when reading the XML file
$this->xmlProcessor = $entityFactory->createXMLProcessor($this->xmlReader);
$this->xmlProcessor = $xmlProcessor;
$this->xmlProcessor->registerCallback(self::XML_NODE_ROW, XMLProcessor::NODE_TYPE_START, [$this, 'processRowStartingNode']);
$this->xmlProcessor->registerCallback(self::XML_NODE_CELL, XMLProcessor::NODE_TYPE_START, [$this, 'processCellStartingNode']);
$this->xmlProcessor->registerCallback(self::XML_NODE_ROW, XMLProcessor::NODE_TYPE_END, [$this, 'processRowEndingNode']);

View File

@ -30,16 +30,14 @@ class Sheet implements SheetInterface
protected $isActive;
/**
* @param XMLReader $xmlReader XML Reader, positioned on the "<table:table>" element
* @param RowIterator $rowIterator The corresponding row iterator
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
* @param string $sheetName Name of the sheet
* @param bool $isSheetActive Whether the sheet was defined as active
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param EntityFactory $entityFactory Factory to create entities
*/
public function __construct($xmlReader, $sheetIndex, $sheetName, $isSheetActive, $optionsManager, $entityFactory)
public function __construct($rowIterator, $sheetIndex, $sheetName, $isSheetActive)
{
$this->rowIterator = $entityFactory->createRowIterator($xmlReader, $optionsManager);
$this->rowIterator = $rowIterator;
$this->index = $sheetIndex;
$this->name = $sheetName;
$this->isActive = $isSheetActive;

View File

@ -51,19 +51,17 @@ class SheetIterator implements IteratorInterface
/**
* @param string $filePath Path of the file to be read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager
* @param \Box\Spout\Common\Helper\Escaper\ODS $escaper Used to unescape XML data
* @param SettingsHelper $settingsHelper Helper to get data from "settings.xml"
* @param EntityFactory $entityFactory Factory to create entities
* @param HelperFactory $helperFactory Factory to create helpers
*/
public function __construct($filePath, $optionsManager, $entityFactory, $helperFactory)
public function __construct($filePath, $optionsManager, $escaper, $settingsHelper, $entityFactory)
{
$this->filePath = $filePath;
$this->optionsManager = $optionsManager;
$this->entityFactory = $entityFactory;
$this->xmlReader = $entityFactory->createXMLReader();
$this->escaper = $helperFactory->createStringsEscaper();
$settingsHelper = $helperFactory->createSettingsHelper($entityFactory);
$this->escaper = $escaper;
$this->activeSheetName = $settingsHelper->getActiveSheetName($filePath);
}

View File

@ -3,6 +3,7 @@
namespace Box\Spout\Reader\XLSX\Creator;
use Box\Spout\Reader\Common\Creator\EntityFactoryInterface;
use Box\Spout\Reader\Common\Entity\Options;
use Box\Spout\Reader\Common\XMLProcessor;
use Box\Spout\Reader\XLSX\Helper\SharedStringsHelper;
use Box\Spout\Reader\XLSX\RowIterator;
@ -33,19 +34,12 @@ class EntityFactory implements EntityFactoryInterface
* @param string $filePath Path of the file to be read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @return SheetIterator
*/
public function createSheetIterator($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper)
public function createSheetIterator($filePath, $optionsManager, $sharedStringsHelper)
{
return new SheetIterator(
$filePath,
$optionsManager,
$sharedStringsHelper,
$globalFunctionsHelper,
$this,
$this->helperFactory
);
$sheetHelper = $this->helperFactory->createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $this);
return new SheetIterator($sheetHelper);
}
/**
@ -67,16 +61,8 @@ class EntityFactory implements EntityFactoryInterface
$optionsManager,
$sharedStringsHelper)
{
return new Sheet(
$filePath,
$sheetDataXMLFilePath,
$sheetIndex,
$sheetName,
$isSheetActive,
$optionsManager,
$sharedStringsHelper,
$this
);
$rowIterator = $this->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper);
return new Sheet($rowIterator, $sheetIndex, $sheetName, $isSheetActive);
}
/**
@ -86,9 +72,25 @@ class EntityFactory implements EntityFactoryInterface
* @param SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
* @return RowIterator
*/
public function createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper)
private function createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper)
{
return new RowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper, $this, $this->helperFactory);
$xmlReader = $this->createXMLReader();
$xmlProcessor = $this->createXMLProcessor($xmlReader);
$styleHelper = $this->helperFactory->createStyleHelper($filePath, $this);
$shouldFormatDates = $optionsManager->getOption(Options::SHOULD_FORMAT_DATES);
$cellValueFormatter = $this->helperFactory->createCellValueFormatter($sharedStringsHelper, $styleHelper, $shouldFormatDates);
$shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS);
return new RowIterator(
$filePath,
$sheetDataXMLFilePath,
$shouldPreserveEmptyRows,
$xmlReader,
$xmlProcessor,
$cellValueFormatter
);
}
/**

View File

@ -44,14 +44,13 @@ class HelperFactory extends \Box\Spout\Common\Creator\HelperFactory
* @param string $filePath Path of the XLSX file being read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @param EntityFactory $entityFactory Factory to create entities
* @return SheetHelper
*/
public function createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $entityFactory)
public function createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $entityFactory)
{
$escaper = $this->createStringsEscaper();
return new SheetHelper($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $escaper, $entityFactory);
return new SheetHelper($filePath, $optionsManager, $sharedStringsHelper, $escaper, $entityFactory);
}
/**

View File

@ -53,16 +53,14 @@ class SheetHelper
* @param string $filePath Path of the XLSX file being read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper Helper to work with shared strings
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to unescape XML data
* @param EntityFactory $entityFactory Factory to create entities
*/
public function __construct($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $escaper, $entityFactory)
public function __construct($filePath, $optionsManager, $sharedStringsHelper, $escaper, $entityFactory)
{
$this->filePath = $filePath;
$this->optionsManager = $optionsManager;
$this->sharedStringsHelper = $sharedStringsHelper;
$this->globalFunctionsHelper = $globalFunctionsHelper;
$this->escaper = $escaper;
$this->entityFactory = $entityFactory;
}

View File

@ -10,9 +10,8 @@ use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\Reader\XLSX\Creator\EntityFactory;
use Box\Spout\Reader\XLSX\Creator\HelperFactory;
use Box\Spout\Reader\XLSX\Helper\CellHelper;
use Box\Spout\Reader\XLSX\Helper\CellValueFormatter;
use Box\Spout\Reader\XLSX\Helper\StyleHelper;
use Box\Spout\Reader\Common\XMLProcessor;
use Box\Spout\Reader\XLSX\Helper\CellValueFormatter;
/**
* Class RowIterator
@ -48,9 +47,6 @@ class RowIterator implements IteratorInterface
/** @var Helper\CellValueFormatter Helper to format cell values */
protected $cellValueFormatter;
/** @var Helper\StyleHelper $styleHelper Helper to work with styles */
protected $styleHelper;
/**
* TODO: This variable can be deleted when row indices get preserved
* @var int Number of read rows
@ -84,25 +80,21 @@ class RowIterator implements IteratorInterface
/**
* @param string $filePath Path of the XLSX file being read
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param Helper\SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
* @param EntityFactory $entityFactory Factory to create entities
* @param HelperFactory $helperFactory Factory to create helpers
* @param bool $shouldPreserveEmptyRows Whether empty rows should be preserved
* @param XMLReader $xmlReader XML Reader
* @param XMLProcessor $xmlProcessor Helper to process XML files
* @param CellValueFormatter $cellValueFormatter Helper to format cell values
*/
public function __construct($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper, $entityFactory, $helperFactory)
public function __construct($filePath, $sheetDataXMLFilePath, $shouldPreserveEmptyRows, $xmlReader, $xmlProcessor, $cellValueFormatter)
{
$this->filePath = $filePath;
$this->sheetDataXMLFilePath = $this->normalizeSheetDataXMLFilePath($sheetDataXMLFilePath);
$this->xmlReader = $entityFactory->createXMLReader();
$this->styleHelper = $helperFactory->createStyleHelper($filePath, $entityFactory);
$this->cellValueFormatter = $helperFactory->createCellValueFormatter($sharedStringsHelper, $this->styleHelper, $optionsManager->getOption(Options::SHOULD_FORMAT_DATES));
$this->shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS);
$this->xmlReader = $xmlReader;
$this->cellValueFormatter = $cellValueFormatter;
$this->shouldPreserveEmptyRows = $shouldPreserveEmptyRows;
// Register all callbacks to process different nodes when reading the XML file
$this->xmlProcessor = $entityFactory->createXMLProcessor($this->xmlReader);
$this->xmlProcessor = $xmlProcessor;
$this->xmlProcessor->registerCallback(self::XML_NODE_DIMENSION, XMLProcessor::NODE_TYPE_START, [$this, 'processDimensionStartingNode']);
$this->xmlProcessor->registerCallback(self::XML_NODE_ROW, XMLProcessor::NODE_TYPE_START, [$this, 'processRowStartingNode']);
$this->xmlProcessor->registerCallback(self::XML_NODE_CELL, XMLProcessor::NODE_TYPE_START, [$this, 'processCellStartingNode']);

View File

@ -26,25 +26,14 @@ class Sheet implements SheetInterface
protected $isActive;
/**
* @param string $filePath Path of the XLSX file being read
* @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
* @param RowIterator $rowIterator The corresponding row iterator
* @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
* @param string $sheetName Name of the sheet
* @param bool $isSheetActive Whether the sheet was defined as active
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param Helper\SharedStringsHelper $sharedStringsHelper Helper to work with shared strings
* @param EntityFactory $entityFactory Factory to create entities
*/
public function __construct(
$filePath,
$sheetDataXMLFilePath,
$sheetIndex, $sheetName,
$isSheetActive,
$optionsManager,
$sharedStringsHelper,
$entityFactory)
public function __construct($rowIterator, $sheetIndex, $sheetName, $isSheetActive)
{
$this->rowIterator = $entityFactory->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsHelper);
$this->rowIterator = $rowIterator;
$this->index = $sheetIndex;
$this->name = $sheetName;
$this->isActive = $isSheetActive;

View File

@ -23,24 +23,12 @@ class SheetIterator implements IteratorInterface
protected $currentSheetIndex;
/**
* @param string $filePath Path of the file to be read
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
* @param \Box\Spout\Reader\XLSX\Helper\SharedStringsHelper $sharedStringsHelper
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
* @param EntityFactory $entityFactory Factory to create entities
* @param HelperFactory $helperFactory Factory to create helpers
* @param SheetHelper $sheetHelper Helper to work with sheets
* @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
*/
public function __construct(
$filePath,
$optionsManager,
$sharedStringsHelper,
$globalFunctionsHelper,
$entityFactory,
$helperFactory)
public function __construct($sheetHelper)
{
// Fetch all available sheets
$sheetHelper = $helperFactory->createSheetHelper($filePath, $optionsManager, $sharedStringsHelper, $globalFunctionsHelper, $entityFactory);
$this->sheets = $sheetHelper->getSheets();
if (count($this->sheets) === 0) {