Introduce an options manager for all writers (#423)

This will improve the management of options and simplify some methods' signatures.
This commit will also help moving the code to a better state regarding Dependency Injection.
This commit is contained in:
Adrien Loison authored and GitHub committed 2017-05-24 13:17:50 +02:00
1 parent f9d8ad8be3
commit a366d0d0af
22 files changed
+447 -113

No files matched your search

+9 -9
View File
@@ -3,10 +3,13 @@
namespace Box\Spout\Writer\XLSX\Internal;
use Box\Spout\Writer\Common\Internal\AbstractWorkbook;
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Options;
use Box\Spout\Writer\XLSX\Helper\FileSystemHelper;
use Box\Spout\Writer\XLSX\Helper\SharedStringsHelper;
use Box\Spout\Writer\XLSX\Helper\StyleHelper;
use Box\Spout\Writer\Common\Sheet;
use Box\Spout\Writer\XLSX\Manager\OptionsManager;
/**
* Class Workbook
@@ -36,21 +39,18 @@ class Workbook extends AbstractWorkbook
protected $styleHelper;
/**
* @param string $tempFolder
* @param bool $shouldUseInlineStrings
* @param bool $shouldCreateNewSheetsAutomatically
* @param \Box\Spout\Writer\Style\Style $defaultRowStyle
* @param \Box\Spout\Writer\Common\Manager\OptionsManagerInterface $optionsManager Options manager
* @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders
*/
public function __construct($tempFolder, $shouldUseInlineStrings, $shouldCreateNewSheetsAutomatically, $defaultRowStyle)
public function __construct(OptionsManagerInterface $optionsManager)
{
parent::__construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle);
$this->shouldUseInlineStrings = $shouldUseInlineStrings;
parent::__construct($optionsManager);
$tempFolder = $optionsManager->getOption(Options::TEMP_FOLDER);
$this->fileSystemHelper = new FileSystemHelper($tempFolder);
$this->fileSystemHelper->createBaseFilesAndFolders();
$defaultRowStyle = $optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
$this->styleHelper = new StyleHelper($defaultRowStyle);
// This helper will be shared by all sheets
@@ -86,7 +86,7 @@ class Workbook extends AbstractWorkbook
$sheet = new Sheet($newSheetIndex, $this->internalId);
$worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder();
$worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->styleHelper, $this->shouldUseInlineStrings);
$worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->styleHelper, $this->optionManager);
$this->worksheets[] = $worksheet;
return $worksheet;
+6 -4
View File
@@ -8,6 +8,8 @@ use Box\Spout\Common\Helper\StringHelper;
use Box\Spout\Writer\Common\Cell;
use Box\Spout\Writer\Common\Helper\CellHelper;
use Box\Spout\Writer\Common\Internal\WorksheetInterface;
use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
use Box\Spout\Writer\Common\Options;
/**
* Class Worksheet
@@ -62,16 +64,16 @@ EOD;
* @param \Box\Spout\Writer\Common\Sheet $externalSheet The associated "external" sheet
* @param string $worksheetFilesFolder Temporary folder where the files to create the XLSX will be stored
* @param \Box\Spout\Writer\XLSX\Helper\SharedStringsHelper $sharedStringsHelper Helper for shared strings
* @param \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to work with styles
* @param bool $shouldUseInlineStrings Whether inline or shared strings should be used
* @param \Box\Spout\Writer\XLSX\Helper\StyleHelper $styleHelper Helper to work with styles
* @param \Box\Spout\Writer\Common\Manager\OptionsManagerInterface $optionsManager Options manager
* @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing
*/
public function __construct($externalSheet, $worksheetFilesFolder, $sharedStringsHelper, $styleHelper, $shouldUseInlineStrings)
public function __construct($externalSheet, $worksheetFilesFolder, $sharedStringsHelper, $styleHelper, OptionsManagerInterface $optionsManager)
{
$this->externalSheet = $externalSheet;
$this->sharedStringsHelper = $sharedStringsHelper;
$this->styleHelper = $styleHelper;
$this->shouldUseInlineStrings = $shouldUseInlineStrings;
$this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS);
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
$this->stringsEscaper = \Box\Spout\Common\Escaper\XLSX::getInstance();
@@ -0,0 +1,61 @@
<?php
namespace Box\Spout\Writer\XLSX\Manager;
use Box\Spout\Writer\Common\Options;
use Box\Spout\Writer\Style\StyleBuilder;
/**
* Class OptionsManager
* XLSX Writer options manager
*
* @package Box\Spout\Writer\XLSX\Manager
*/
class OptionsManager extends \Box\Spout\Writer\Common\Manager\OptionsManager
{
/** Default style font values */
const DEFAULT_FONT_SIZE = 12;
const DEFAULT_FONT_NAME = 'Calibri';
/** @var StyleBuilder Style builder */
protected $styleBuilder;
/**
* OptionsManager constructor.
* @param StyleBuilder $styleBuilder
*/
public function __construct(StyleBuilder $styleBuilder)
{
$this->styleBuilder = $styleBuilder;
parent::__construct();
}
/**
* @inheritdoc
*/
protected function getSupportedOptions()
{
return [
Options::TEMP_FOLDER,
Options::DEFAULT_ROW_STYLE,
Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY,
Options::SHOULD_USE_INLINE_STRINGS,
];
}
/**
* @inheritdoc
*/
protected function setDefaultOptions()
{
$defaultRowStyle = $this->styleBuilder
->setFontSize(self::DEFAULT_FONT_SIZE)
->setFontName(self::DEFAULT_FONT_NAME)
->build();
$this->setOption(Options::TEMP_FOLDER, sys_get_temp_dir());
$this->setOption(Options::DEFAULT_ROW_STYLE, $defaultRowStyle);
$this->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, true);
$this->setOption(Options::SHOULD_USE_INLINE_STRINGS, true);
}
}
+4 -27
View File
@@ -3,6 +3,7 @@
namespace Box\Spout\Writer\XLSX;
use Box\Spout\Writer\AbstractMultiSheetsWriter;
use Box\Spout\Writer\Common\Options;
use Box\Spout\Writer\Style\StyleBuilder;
use Box\Spout\Writer\XLSX\Internal\Workbook;
@@ -14,19 +15,9 @@ use Box\Spout\Writer\XLSX\Internal\Workbook;
*/
class Writer extends AbstractMultiSheetsWriter
{
/** Default style font values */
const DEFAULT_FONT_SIZE = 12;
const DEFAULT_FONT_NAME = 'Calibri';
/** @var string Content-Type value for the header */
protected static $headerContentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
/** @var string Temporary folder where the files to create the XLSX will be stored */
protected $tempFolder;
/** @var bool Whether inline or shared strings should be used - inline string is more memory efficient */
protected $shouldUseInlineStrings = true;
/** @var Internal\Workbook The workbook for the XLSX file */
protected $book;
@@ -43,7 +34,7 @@ class Writer extends AbstractMultiSheetsWriter
{
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
$this->tempFolder = $tempFolder;
$this->optionsManager->setOption(Options::TEMP_FOLDER, $tempFolder);
return $this;
}
@@ -60,7 +51,7 @@ class Writer extends AbstractMultiSheetsWriter
{
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
$this->shouldUseInlineStrings = $shouldUseInlineStrings;
$this->optionsManager->setOption(Options::SHOULD_USE_INLINE_STRINGS, $shouldUseInlineStrings);
return $this;
}
@@ -73,8 +64,7 @@ class Writer extends AbstractMultiSheetsWriter
protected function openWriter()
{
if (!$this->book) {
$tempFolder = ($this->tempFolder) ? : sys_get_temp_dir();
$this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle);
$this->book = new Workbook($this->optionsManager);
$this->book->addNewSheetAndMakeItCurrent();
}
}
@@ -105,19 +95,6 @@ class Writer extends AbstractMultiSheetsWriter
$this->book->addRowToCurrentWorksheet($dataRow, $style);
}
/**
* Returns the default style to be applied to rows.
*
* @return \Box\Spout\Writer\Style\Style
*/
protected function getDefaultRowStyle()
{
return (new StyleBuilder())
->setFontSize(self::DEFAULT_FONT_SIZE)
->setFontName(self::DEFAULT_FONT_NAME)
->build();
}
/**
* Closes the writer, preventing any additional writing.
*