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

+7 -5
View File
@@ -3,6 +3,8 @@
namespace Box\Spout\Writer\ODS\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\ODS\Helper\FileSystemHelper;
use Box\Spout\Writer\ODS\Helper\StyleHelper;
use Box\Spout\Writer\Common\Sheet;
@@ -29,18 +31,18 @@ class Workbook extends AbstractWorkbook
protected $styleHelper;
/**
* @param string $tempFolder
* @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, $shouldCreateNewSheetsAutomatically, $defaultRowStyle)
public function __construct(OptionsManagerInterface $optionsManager)
{
parent::__construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle);
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);
}
@@ -0,0 +1,50 @@
<?php
namespace Box\Spout\Writer\ODS\Manager;
use Box\Spout\Writer\Common\Options;
use Box\Spout\Writer\Style\StyleBuilder;
/**
* Class OptionsManager
* ODS Writer options manager
*
* @package Box\Spout\Writer\ODS\Manager
*/
class OptionsManager extends \Box\Spout\Writer\Common\Manager\OptionsManager
{
/** @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,
];
}
/**
* @inheritdoc
*/
protected function setDefaultOptions()
{
$this->setOption(Options::TEMP_FOLDER, sys_get_temp_dir());
$this->setOption(Options::DEFAULT_ROW_STYLE, $this->styleBuilder->build());
$this->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, true);
}
}
+3 -6
View File
@@ -4,6 +4,7 @@ namespace Box\Spout\Writer\ODS;
use Box\Spout\Writer\AbstractMultiSheetsWriter;
use Box\Spout\Writer\Common;
use Box\Spout\Writer\Common\Options;
use Box\Spout\Writer\ODS\Internal\Workbook;
/**
@@ -17,9 +18,6 @@ class Writer extends AbstractMultiSheetsWriter
/** @var string Content-Type value for the header */
protected static $headerContentType = 'application/vnd.oasis.opendocument.spreadsheet';
/** @var string Temporary folder where the files to create the ODS will be stored */
protected $tempFolder;
/** @var Internal\Workbook The workbook for the ODS file */
protected $book;
@@ -36,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;
}
@@ -48,8 +46,7 @@ class Writer extends AbstractMultiSheetsWriter
*/
protected function openWriter()
{
$tempFolder = ($this->tempFolder) ? : sys_get_temp_dir();
$this->book = new Workbook($tempFolder, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle);
$this->book = new Workbook($this->optionsManager);
$this->book->addNewSheetAndMakeItCurrent();
}