Refactor readers for better di (#457)
* Refactor readers to get a proper DI Similar to what was done with writers, readers also needed to be updated to match the new way of doing things. This commits promotes a better DI (factories, injection through constructors). * Escapers should not be singletons Instead, they should be proper object that can be injected where needed.
This commit is contained in:
1 parent
4d6437fa77
commit
b7e46740ce
66 files changed
+1095
-618
No files matched your search
@@ -3,7 +3,7 @@
|
||||
namespace Box\Spout\Writer\CSV\Manager;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Manager\OptionsManagerAbstract;
|
||||
use Box\Spout\Common\Manager\OptionsManagerAbstract;
|
||||
|
||||
/**
|
||||
* Class OptionsManager
|
||||
@@ -34,4 +34,4 @@ class OptionsManager extends OptionsManagerAbstract
|
||||
$this->setOption(Options::FIELD_ENCLOSURE, '"');
|
||||
$this->setOption(Options::SHOULD_ADD_BOM, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Box\Spout\Writer\Common\Creator;
|
||||
|
||||
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface;
|
||||
|
||||
/**
|
||||
@@ -17,4 +17,4 @@ interface InternalFactoryInterface
|
||||
* @return WorkbookManagerInterface
|
||||
*/
|
||||
public function createWorkbookManager(OptionsManagerInterface $optionsManager);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace Box\Spout\Writer\Common\Entity;
|
||||
|
||||
/**
|
||||
* Class Options
|
||||
* Writer' options holder
|
||||
* Writers' options holder
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Entity
|
||||
*/
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager;
|
||||
|
||||
/**
|
||||
* Class OptionsManager
|
||||
* Writer' options manager
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Manager
|
||||
*/
|
||||
abstract class OptionsManagerAbstract implements OptionsManagerInterface
|
||||
{
|
||||
const PREFIX_OPTION = 'OPTION_';
|
||||
|
||||
/** @var string[] List of all supported option names */
|
||||
private $supportedOptions = [];
|
||||
|
||||
/** @var array Associative array [OPTION_NAME => OPTION_VALUE] */
|
||||
private $options = [];
|
||||
|
||||
/**
|
||||
* WriterOptions constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->supportedOptions = $this->getSupportedOptions();
|
||||
$this->setDefaultOptions();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array List of supported options
|
||||
*/
|
||||
abstract protected function getSupportedOptions();
|
||||
|
||||
/**
|
||||
* Sets the default options.
|
||||
* To be overriden by child classes
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function setDefaultOptions();
|
||||
|
||||
/**
|
||||
* Sets the given option, if this option is supported.
|
||||
*
|
||||
* @param string $optionName
|
||||
* @param mixed $optionValue
|
||||
* @return void
|
||||
*/
|
||||
public function setOption($optionName, $optionValue)
|
||||
{
|
||||
if (in_array($optionName, $this->supportedOptions)) {
|
||||
$this->options[$optionName] = $optionValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $optionName
|
||||
* @return mixed|null The set option or NULL if no option with given name found
|
||||
*/
|
||||
public function getOption($optionName)
|
||||
{
|
||||
$optionValue = null;
|
||||
|
||||
if (isset($this->options[$optionName])) {
|
||||
$optionValue = $this->options[$optionName];
|
||||
}
|
||||
|
||||
return $optionValue;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\Common\Manager;
|
||||
|
||||
/**
|
||||
* Interface OptionsManagerInterface
|
||||
* Writer' options interface
|
||||
*
|
||||
* @package Box\Spout\Writer\Common\Manager
|
||||
*/
|
||||
interface OptionsManagerInterface
|
||||
{
|
||||
/**
|
||||
* @param string $optionName
|
||||
* @param mixed $optionValue
|
||||
* @return void
|
||||
*/
|
||||
public function setOption($optionName, $optionValue);
|
||||
|
||||
/**
|
||||
* @param string $optionName
|
||||
* @return mixed|null The set option or NULL if no option with given name found
|
||||
*/
|
||||
public function getOption($optionName);
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Box\Spout\Writer\Common\Manager;
|
||||
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Writer\Common\Helper\FileSystemWithRootFolderHelperInterface;
|
||||
use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleManagerInterface;
|
||||
@@ -312,4 +313,4 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface
|
||||
$rootFolder = $this->fileSystemHelper->getRootFolder();
|
||||
$this->fileSystemHelper->deleteFolderRecursively($rootFolder);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace Box\Spout\Writer\ODS\Creator;
|
||||
|
||||
use Box\Spout\Common\Helper\StringHelper;
|
||||
use Box\Spout\Writer\Common\Helper\ZipHelper;
|
||||
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
|
||||
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;
|
||||
@@ -13,7 +13,7 @@ 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\Escaper;
|
||||
use Box\Spout\Common\Helper\Escaper;
|
||||
|
||||
/**
|
||||
* Class InternalFactory
|
||||
@@ -109,7 +109,7 @@ class InternalFactory implements InternalFactoryInterface
|
||||
*/
|
||||
private function createStringsEscaper()
|
||||
{
|
||||
return Escaper\ODS::getInstance();
|
||||
return new Escaper\ODS();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,4 +119,4 @@ class InternalFactory implements InternalFactoryInterface
|
||||
{
|
||||
return new StringHelper();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Box\Spout\Writer\ODS\Manager;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Manager\OptionsManagerAbstract;
|
||||
use Box\Spout\Common\Manager\OptionsManagerAbstract;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
|
||||
/**
|
||||
@@ -48,4 +48,4 @@ class OptionsManager extends OptionsManagerAbstract
|
||||
$this->setOption(Options::DEFAULT_ROW_STYLE, $this->styleBuilder->build());
|
||||
$this->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ use Box\Spout\Writer\ODS\Manager\Style\StyleManager;
|
||||
*/
|
||||
class WorksheetManager implements WorksheetManagerInterface
|
||||
{
|
||||
/** @var \Box\Spout\Common\Escaper\ODS Strings escaper */
|
||||
/** @var \Box\Spout\Common\Helper\Escaper\ODS Strings escaper */
|
||||
private $stringsEscaper;
|
||||
|
||||
/** @var StringHelper String helper */
|
||||
@@ -28,11 +28,11 @@ class WorksheetManager implements WorksheetManagerInterface
|
||||
/**
|
||||
* WorksheetManager constructor.
|
||||
*
|
||||
* @param \Box\Spout\Common\Escaper\ODS $stringsEscaper
|
||||
* @param \Box\Spout\Common\Helper\Escaper\ODS $stringsEscaper
|
||||
* @param StringHelper $stringHelper
|
||||
*/
|
||||
public function __construct(
|
||||
\Box\Spout\Common\Escaper\ODS $stringsEscaper,
|
||||
\Box\Spout\Common\Helper\Escaper\ODS $stringsEscaper,
|
||||
StringHelper $stringHelper)
|
||||
{
|
||||
$this->stringsEscaper = $stringsEscaper;
|
||||
@@ -205,4 +205,4 @@ class WorksheetManager implements WorksheetManagerInterface
|
||||
|
||||
fclose($worksheetFilePointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ 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;
|
||||
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
|
||||
use Box\Spout\Writer\Exception\WriterNotOpenedException;
|
||||
|
||||
@@ -4,7 +4,7 @@ namespace Box\Spout\Writer;
|
||||
|
||||
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
|
||||
use Box\Spout\Writer\Common\Entity\Sheet;
|
||||
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Entity\Worksheet;
|
||||
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
namespace Box\Spout\Writer\XLSX\Creator;
|
||||
|
||||
use Box\Spout\Common\Escaper;
|
||||
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\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Helper\ZipHelper;
|
||||
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Writer\XLSX\Helper\FileSystemHelper;
|
||||
use Box\Spout\Writer\XLSX\Manager\SharedStringsManager;
|
||||
use Box\Spout\Writer\XLSX\Manager\Style\StyleManager;
|
||||
@@ -113,8 +113,9 @@ class InternalFactory implements InternalFactoryInterface
|
||||
{
|
||||
$tempFolder = $optionsManager->getOption(Options::TEMP_FOLDER);
|
||||
$zipHelper = $this->createZipHelper();
|
||||
$escaper = $this->createStringsEscaper();
|
||||
|
||||
return new FileSystemHelper($tempFolder, $zipHelper);
|
||||
return new FileSystemHelper($tempFolder, $zipHelper, $escaper);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,7 +131,7 @@ class InternalFactory implements InternalFactoryInterface
|
||||
*/
|
||||
private function createStringsEscaper()
|
||||
{
|
||||
return Escaper\XLSX::getInstance();
|
||||
return new Escaper\XLSX();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,4 +141,4 @@ class InternalFactory implements InternalFactoryInterface
|
||||
{
|
||||
return new StringHelper();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,9 @@ class FileSystemHelper extends \Box\Spout\Common\Helper\FileSystemHelper impleme
|
||||
/** @var ZipHelper Helper to perform tasks with Zip archive */
|
||||
private $zipHelper;
|
||||
|
||||
/** @var \Box\Spout\Common\Helper\Escaper\XLSX Used to escape XML data */
|
||||
private $escaper;
|
||||
|
||||
/** @var string Path to the root folder inside the temp folder where the files to create the XLSX will be stored */
|
||||
private $rootFolder;
|
||||
|
||||
@@ -55,11 +58,13 @@ class FileSystemHelper extends \Box\Spout\Common\Helper\FileSystemHelper impleme
|
||||
/**
|
||||
* @param string $baseFolderPath The path of the base folder where all the I/O can occur
|
||||
* @param ZipHelper $zipHelper Helper to perform tasks with Zip archive
|
||||
* @param \Box\Spout\Common\Helper\Escaper\XLSX $escaper Used to escape XML data
|
||||
*/
|
||||
public function __construct($baseFolderPath, $zipHelper)
|
||||
public function __construct($baseFolderPath, $zipHelper, $escaper)
|
||||
{
|
||||
parent::__construct($baseFolderPath);
|
||||
$this->zipHelper = $zipHelper;
|
||||
$this->escaper = $escaper;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -298,14 +303,11 @@ EOD;
|
||||
<sheets>
|
||||
EOD;
|
||||
|
||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||
$escaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
|
||||
|
||||
/** @var Worksheet $worksheet */
|
||||
foreach ($worksheets as $worksheet) {
|
||||
$worksheetName = $worksheet->getExternalSheet()->getName();
|
||||
$worksheetId = $worksheet->getId();
|
||||
$workbookXmlFileContents .= '<sheet name="' . $escaper->escape($worksheetName) . '" sheetId="' . $worksheetId . '" r:id="rIdSheet' . $worksheetId . '"/>';
|
||||
$workbookXmlFileContents .= '<sheet name="' . $this->escaper->escape($worksheetName) . '" sheetId="' . $worksheetId . '" r:id="rIdSheet' . $worksheetId . '"/>';
|
||||
}
|
||||
|
||||
$workbookXmlFileContents .= <<<EOD
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Box\Spout\Writer\XLSX\Manager;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Manager\OptionsManagerAbstract;
|
||||
use Box\Spout\Common\Manager\OptionsManagerAbstract;
|
||||
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
|
||||
|
||||
/**
|
||||
@@ -59,4 +59,4 @@ class OptionsManager extends OptionsManagerAbstract
|
||||
$this->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, true);
|
||||
$this->setOption(Options::SHOULD_USE_INLINE_STRINGS, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace Box\Spout\Writer\XLSX\Manager;
|
||||
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Escaper;
|
||||
use Box\Spout\Common\Helper\Escaper;
|
||||
|
||||
/**
|
||||
* Class SharedStringsManager
|
||||
|
||||
@@ -6,7 +6,7 @@ use Box\Spout\Common\Exception\InvalidArgumentException;
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Helper\StringHelper;
|
||||
use Box\Spout\Writer\Common\Helper\CellHelper;
|
||||
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Worksheet;
|
||||
@@ -44,7 +44,7 @@ EOD;
|
||||
/** @var SharedStringsManager Helper to write shared strings */
|
||||
private $sharedStringsManager;
|
||||
|
||||
/** @var \Box\Spout\Common\Escaper\XLSX Strings escaper */
|
||||
/** @var \Box\Spout\Common\Helper\Escaper\XLSX Strings escaper */
|
||||
private $stringsEscaper;
|
||||
|
||||
/** @var StringHelper String helper */
|
||||
@@ -56,14 +56,14 @@ EOD;
|
||||
* @param OptionsManagerInterface $optionsManager
|
||||
* @param StyleManager $styleManager
|
||||
* @param SharedStringsManager $sharedStringsManager
|
||||
* @param \Box\Spout\Common\Escaper\XLSX $stringsEscaper
|
||||
* @param \Box\Spout\Common\Helper\Escaper\XLSX $stringsEscaper
|
||||
* @param StringHelper $stringHelper
|
||||
*/
|
||||
public function __construct(
|
||||
OptionsManagerInterface $optionsManager,
|
||||
StyleManager $styleManager,
|
||||
SharedStringsManager $sharedStringsManager,
|
||||
\Box\Spout\Common\Escaper\XLSX $stringsEscaper,
|
||||
\Box\Spout\Common\Helper\Escaper\XLSX $stringsEscaper,
|
||||
StringHelper $stringHelper)
|
||||
{
|
||||
$this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS);
|
||||
@@ -265,4 +265,4 @@ EOD;
|
||||
fwrite($worksheetFilePointer, '</worksheet>');
|
||||
fclose($worksheetFilePointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user