Refactory Writer Styles to match new code organization (#433)
Decomposed old StyleHelper into StyleManager, StyleRegistry and StyleMerger.
This commit is contained in:
1 parent
238756ab6e
commit
cc9a0b526b
34 files changed
+1005
-762
No files matched your search
@@ -7,9 +7,9 @@ use Box\Spout\Writer\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Writer\Common\Entity\Options;
|
||||
use Box\Spout\Writer\Common\Creator\EntityFactory;
|
||||
use Box\Spout\Writer\Common\Creator\InternalFactoryInterface;
|
||||
use Box\Spout\Writer\Common\Manager\StyleManager;
|
||||
use Box\Spout\Writer\ODS\Helper\FileSystemHelper;
|
||||
use Box\Spout\Writer\ODS\Helper\StyleHelper;
|
||||
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;
|
||||
@@ -46,22 +46,41 @@ class InternalFactory implements InternalFactoryInterface
|
||||
$fileSystemHelper = $this->createFileSystemHelper($optionsManager);
|
||||
$fileSystemHelper->createBaseFilesAndFolders();
|
||||
|
||||
$styleHelper = $this->createStyleHelper($optionsManager);
|
||||
$worksheetManager = $this->createWorksheetManager($styleHelper);
|
||||
$styleManager = $this->createStyleManager($optionsManager);
|
||||
$worksheetManager = $this->createWorksheetManager();
|
||||
|
||||
return new WorkbookManager($workbook, $optionsManager, $worksheetManager, $styleHelper, $fileSystemHelper, $this->entityFactory);
|
||||
return new WorkbookManager($workbook, $optionsManager, $worksheetManager, $styleManager, $fileSystemHelper, $this->entityFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StyleHelper $styleHelper
|
||||
* @return WorksheetManager
|
||||
*/
|
||||
private function createWorksheetManager(StyleHelper $styleHelper)
|
||||
private function createWorksheetManager()
|
||||
{
|
||||
$stringsEscaper = $this->createStringsEscaper();
|
||||
$stringsHelper = $this->createStringHelper();
|
||||
|
||||
return new WorksheetManager($styleHelper, $stringsEscaper, $stringsHelper);
|
||||
return new WorksheetManager($stringsEscaper, $stringsHelper);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsManagerInterface $optionsManager
|
||||
* @return StyleManager
|
||||
*/
|
||||
private function createStyleManager(OptionsManagerInterface $optionsManager)
|
||||
{
|
||||
$styleRegistry = $this->createStyleRegistry($optionsManager);
|
||||
return new StyleManager($styleRegistry);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsManagerInterface $optionsManager
|
||||
* @return StyleRegistry
|
||||
*/
|
||||
private function createStyleRegistry(OptionsManagerInterface $optionsManager)
|
||||
{
|
||||
$defaultRowStyle = $optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
|
||||
return new StyleRegistry($defaultRowStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,26 +93,6 @@ class InternalFactory implements InternalFactoryInterface
|
||||
return new FileSystemHelper($tempFolder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OptionsManagerInterface $optionsManager
|
||||
* @return StyleHelper
|
||||
*/
|
||||
private function createStyleHelper(OptionsManagerInterface $optionsManager)
|
||||
{
|
||||
$defaultRowStyle = $optionsManager->getOption(Options::DEFAULT_ROW_STYLE);
|
||||
$styleManager = $this->createStyleManager();
|
||||
|
||||
return new StyleHelper($defaultRowStyle, $styleManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return StyleManager
|
||||
*/
|
||||
private function createStyleManager()
|
||||
{
|
||||
return new StyleManager();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Escaper\ODS
|
||||
*/
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Box\Spout\Writer\ODS\Helper;
|
||||
use Box\Spout\Writer\Common\Helper\FileSystemWithRootFolderHelperInterface;
|
||||
use Box\Spout\Writer\Common\Helper\ZipHelper;
|
||||
use Box\Spout\Writer\Common\Entity\Worksheet;
|
||||
use Box\Spout\Writer\ODS\Manager\Style\StyleManager;
|
||||
use Box\Spout\Writer\ODS\Manager\WorksheetManager;
|
||||
|
||||
/**
|
||||
@@ -175,19 +176,19 @@ EOD;
|
||||
* Creates the "content.xml" file under the root folder
|
||||
*
|
||||
* @param WorksheetManager $worksheetManager
|
||||
* @param StyleManager $styleManager
|
||||
* @param Worksheet[] $worksheets
|
||||
* @param StyleHelper $styleHelper
|
||||
* @return FileSystemHelper
|
||||
*/
|
||||
public function createContentFile($worksheetManager, $worksheets, $styleHelper)
|
||||
public function createContentFile($worksheetManager, $styleManager, $worksheets)
|
||||
{
|
||||
$contentXmlFileContents = <<<EOD
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<office:document-content office:version="1.2" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:msoxl="http://schemas.microsoft.com/office/excel/formula" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
EOD;
|
||||
|
||||
$contentXmlFileContents .= $styleHelper->getContentXmlFontFaceSectionContent();
|
||||
$contentXmlFileContents .= $styleHelper->getContentXmlAutomaticStylesSectionContent(count($worksheets));
|
||||
$contentXmlFileContents .= $styleManager->getContentXmlFontFaceSectionContent();
|
||||
$contentXmlFileContents .= $styleManager->getContentXmlAutomaticStylesSectionContent(count($worksheets));
|
||||
|
||||
$contentXmlFileContents .= '<office:body><office:spreadsheet>';
|
||||
|
||||
@@ -246,13 +247,13 @@ EOD;
|
||||
/**
|
||||
* Creates the "styles.xml" file under the root folder
|
||||
*
|
||||
* @param StyleHelper $styleHelper
|
||||
* @param StyleManager $styleManager
|
||||
* @param int $numWorksheets Number of created worksheets
|
||||
* @return FileSystemHelper
|
||||
*/
|
||||
public function createStylesFile($styleHelper, $numWorksheets)
|
||||
public function createStylesFile($styleManager, $numWorksheets)
|
||||
{
|
||||
$stylesXmlFileContents = $styleHelper->getStylesXMLFileContent($numWorksheets);
|
||||
$stylesXmlFileContents = $styleManager->getStylesXMLFileContent($numWorksheets);
|
||||
$this->createFileWithContents($this->rootFolder, self::STYLES_XML_FILE_NAME, $stylesXmlFileContents);
|
||||
|
||||
return $this;
|
||||
|
||||
+11
-32
@@ -1,41 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\ODS\Helper;
|
||||
namespace Box\Spout\Writer\ODS\Manager\Style;
|
||||
|
||||
use Box\Spout\Writer\Common\Helper\StyleHelperAbstract;
|
||||
use Box\Spout\Writer\Common\Entity\Style\BorderPart;
|
||||
use Box\Spout\Writer\ODS\Helper\BorderHelper;
|
||||
|
||||
/**
|
||||
* Class StyleHelper
|
||||
* This class provides helper functions to manage styles
|
||||
* Class StyleManager
|
||||
* Manages styles to be applied to a cell
|
||||
*
|
||||
* @package Box\Spout\Writer\ODS\Helper
|
||||
* @package Box\Spout\Writer\ODS\Manager\Style
|
||||
*/
|
||||
class StyleHelper extends StyleHelperAbstract
|
||||
class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager
|
||||
{
|
||||
/** @var string[] [FONT_NAME] => [] Map whose keys contain all the fonts used */
|
||||
protected $usedFontsSet = [];
|
||||
|
||||
/**
|
||||
* Registers the given style as a used style.
|
||||
* Duplicate styles won't be registered more than once.
|
||||
*
|
||||
* @param \Box\Spout\Writer\Common\Entity\Style\Style $style The style to be registered
|
||||
* @return \Box\Spout\Writer\Common\Entity\Style\Style The registered style, updated with an internal ID.
|
||||
*/
|
||||
public function registerStyle($style)
|
||||
{
|
||||
$this->usedFontsSet[$style->getFontName()] = true;
|
||||
return parent::registerStyle($style);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[] List of used fonts name
|
||||
*/
|
||||
protected function getUsedFonts()
|
||||
{
|
||||
return array_keys($this->usedFontsSet);
|
||||
}
|
||||
/** @var StyleRegistry */
|
||||
protected $styleRegistry;
|
||||
|
||||
/**
|
||||
* Returns the content of the "styles.xml" file, given a list of styles.
|
||||
@@ -70,7 +49,7 @@ EOD;
|
||||
protected function getFontFaceSectionContent()
|
||||
{
|
||||
$content = '<office:font-face-decls>';
|
||||
foreach ($this->getUsedFonts() as $fontName) {
|
||||
foreach ($this->styleRegistry->getUsedFonts() as $fontName) {
|
||||
$content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>';
|
||||
}
|
||||
$content .= '</office:font-face-decls>';
|
||||
@@ -162,7 +141,7 @@ EOD;
|
||||
public function getContentXmlFontFaceSectionContent()
|
||||
{
|
||||
$content = '<office:font-face-decls>';
|
||||
foreach ($this->getUsedFonts() as $fontName) {
|
||||
foreach ($this->styleRegistry->getUsedFonts() as $fontName) {
|
||||
$content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>';
|
||||
}
|
||||
$content .= '</office:font-face-decls>';
|
||||
@@ -180,7 +159,7 @@ EOD;
|
||||
{
|
||||
$content = '<office:automatic-styles>';
|
||||
|
||||
foreach ($this->getRegisteredStyles() as $style) {
|
||||
foreach ($this->styleRegistry->getRegisteredStyles() as $style) {
|
||||
$content .= $this->getStyleSectionContent($style);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\ODS\Manager\Style;
|
||||
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
|
||||
/**
|
||||
* Class StyleRegistry
|
||||
* Registry for all used styles
|
||||
*
|
||||
* @package Box\Spout\Writer\ODS\Manager\Style
|
||||
*/
|
||||
class StyleRegistry extends \Box\Spout\Writer\Common\Manager\Style\StyleRegistry
|
||||
{
|
||||
/** @var array [FONT_NAME] => [] Map whose keys contain all the fonts used */
|
||||
protected $usedFontsSet = [];
|
||||
|
||||
/**
|
||||
* Registers the given style as a used style.
|
||||
* Duplicate styles won't be registered more than once.
|
||||
*
|
||||
* @param Style $style The style to be registered
|
||||
* @return Style The registered style, updated with an internal ID.
|
||||
*/
|
||||
public function registerStyle(Style $style)
|
||||
{
|
||||
$registeredStyle = parent::registerStyle($style);
|
||||
$this->usedFontsSet[$style->getFontName()] = true;
|
||||
|
||||
return $registeredStyle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[] List of used fonts name
|
||||
*/
|
||||
public function getUsedFonts()
|
||||
{
|
||||
return array_keys($this->usedFontsSet);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ use Box\Spout\Writer\Common\Sheet;
|
||||
use Box\Spout\Writer\Common\Manager\WorkbookManagerAbstract;
|
||||
use Box\Spout\Writer\ODS\Helper\FileSystemHelper;
|
||||
use Box\Spout\Writer\ODS\Helper\StyleHelper;
|
||||
use Box\Spout\Writer\ODS\Manager\Style\StyleManager;
|
||||
|
||||
/**
|
||||
* Class WorkbookManager
|
||||
@@ -27,11 +28,8 @@ class WorkbookManager extends WorkbookManagerAbstract
|
||||
/** @var FileSystemHelper Helper to perform file system operations */
|
||||
protected $fileSystemHelper;
|
||||
|
||||
/** @var StyleHelper Helper to apply styles */
|
||||
protected $styleHelper;
|
||||
|
||||
/** @var int Maximum number of columns among all the written rows */
|
||||
protected $maxNumColumns = 1;
|
||||
/** @var StyleManager Manages styles */
|
||||
protected $styleManager;
|
||||
|
||||
/**
|
||||
* @return int Maximum number of rows/columns a sheet can contain
|
||||
@@ -63,9 +61,9 @@ class WorkbookManager extends WorkbookManagerAbstract
|
||||
$numWorksheets = count($worksheets);
|
||||
|
||||
$this->fileSystemHelper
|
||||
->createContentFile($this->worksheetManager, $worksheets, $this->styleHelper)
|
||||
->createContentFile($this->worksheetManager, $this->styleManager, $worksheets)
|
||||
->deleteWorksheetTempFolder()
|
||||
->createStylesFile($this->styleHelper, $numWorksheets)
|
||||
->createStylesFile($this->styleManager, $numWorksheets)
|
||||
->zipRootFolderAndCopyToStream($finalFilePointer);
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,8 @@ use Box\Spout\Common\Helper\StringHelper;
|
||||
use Box\Spout\Writer\Common\Entity\Cell;
|
||||
use Box\Spout\Writer\Common\Entity\Worksheet;
|
||||
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface;
|
||||
use Box\Spout\Writer\ODS\Helper\StyleHelper;
|
||||
use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
use Box\Spout\Writer\ODS\Manager\Style\StyleManager;
|
||||
|
||||
/**
|
||||
* Class WorksheetManager
|
||||
@@ -19,9 +19,6 @@ use Box\Spout\Writer\Common\Entity\Style\Style;
|
||||
*/
|
||||
class WorksheetManager implements WorksheetManagerInterface
|
||||
{
|
||||
/** @var StyleHelper Helper to work with styles */
|
||||
private $styleHelper;
|
||||
|
||||
/** @var \Box\Spout\Common\Escaper\ODS Strings escaper */
|
||||
private $stringsEscaper;
|
||||
|
||||
@@ -31,16 +28,13 @@ class WorksheetManager implements WorksheetManagerInterface
|
||||
/**
|
||||
* WorksheetManager constructor.
|
||||
*
|
||||
* @param StyleHelper $styleHelper
|
||||
* @param \Box\Spout\Common\Escaper\ODS $stringsEscaper
|
||||
* @param StringHelper $stringHelper
|
||||
*/
|
||||
public function __construct(
|
||||
StyleHelper $styleHelper,
|
||||
\Box\Spout\Common\Escaper\ODS $stringsEscaper,
|
||||
StringHelper $stringHelper)
|
||||
{
|
||||
$this->styleHelper = $styleHelper;
|
||||
$this->stringsEscaper = $stringsEscaper;
|
||||
$this->stringHelper = $stringHelper;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user