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:
Adrien Loison authored and GitHub committed 2017-08-27 00:01:17 +02:00
1 parent 4d6437fa77
commit b7e46740ce
66 files changed
+1095 -618

No files matched your search

@@ -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);
}
}
}