zipHelper = $zipHelper; $this->escaper = $escaper; } /** * @return string */ public function getRootFolder() { return $this->rootFolder; } /** * @return string */ public function getXlFolder() { return $this->xlFolder; } /** * @return string */ public function getXlWorksheetsFolder() { return $this->xlWorksheetsFolder; } /** * Creates all the folders needed to create a XLSX file, as well as the files that won't change. * * @throws \OpenSpout\Common\Exception\IOException If unable to create at least one of the base folders */ public function createBaseFilesAndFolders() { $this ->createRootFolder() ->createRelsFolderAndFile() ->createDocPropsFolderAndFiles() ->createXlFolderAndSubFolders() ; } /** * Creates the "[Content_Types].xml" file under the root folder. * * @param Worksheet[] $worksheets * * @return FileSystemHelper */ public function createContentTypesFile($worksheets) { $contentTypesXmlFileContents = <<<'EOD' EOD; /** @var Worksheet $worksheet */ foreach ($worksheets as $worksheet) { $contentTypesXmlFileContents .= ''; } $contentTypesXmlFileContents .= <<<'EOD' EOD; $this->createFileWithContents($this->rootFolder, self::CONTENT_TYPES_XML_FILE_NAME, $contentTypesXmlFileContents); return $this; } /** * Creates the "workbook.xml" file under the "xl" folder. * * @param Worksheet[] $worksheets * * @return FileSystemHelper */ public function createWorkbookFile($worksheets) { $workbookXmlFileContents = <<<'EOD' EOD; /** @var Worksheet $worksheet */ foreach ($worksheets as $worksheet) { $worksheetName = $worksheet->getExternalSheet()->getName(); $worksheetVisibility = $worksheet->getExternalSheet()->isVisible() ? 'visible' : 'hidden'; $worksheetId = $worksheet->getId(); $workbookXmlFileContents .= ''; } $workbookXmlFileContents .= <<<'EOD' EOD; $this->createFileWithContents($this->xlFolder, self::WORKBOOK_XML_FILE_NAME, $workbookXmlFileContents); return $this; } /** * Creates the "workbook.xml.res" file under the "xl/_res" folder. * * @param Worksheet[] $worksheets * * @return FileSystemHelper */ public function createWorkbookRelsFile($worksheets) { $workbookRelsXmlFileContents = <<<'EOD' EOD; /** @var Worksheet $worksheet */ foreach ($worksheets as $worksheet) { $worksheetId = $worksheet->getId(); $workbookRelsXmlFileContents .= ''; } $workbookRelsXmlFileContents .= ''; $this->createFileWithContents($this->xlRelsFolder, self::WORKBOOK_RELS_XML_FILE_NAME, $workbookRelsXmlFileContents); return $this; } /** * Creates the "styles.xml" file under the "xl" folder. * * @param StyleManager $styleManager * * @return FileSystemHelper */ public function createStylesFile($styleManager) { $stylesXmlFileContents = $styleManager->getStylesXMLFileContent(); $this->createFileWithContents($this->xlFolder, self::STYLES_XML_FILE_NAME, $stylesXmlFileContents); return $this; } /** * Zips the root folder and streams the contents of the zip into the given stream. * * @param resource $streamPointer Pointer to the stream to copy the zip */ public function zipRootFolderAndCopyToStream($streamPointer) { $zip = $this->zipHelper->createZip($this->rootFolder); $zipFilePath = $this->zipHelper->getZipFilePath($zip); // In order to have the file's mime type detected properly, files need to be added // to the zip file in a particular order. // "[Content_Types].xml" then at least 2 files located in "xl" folder should be zipped first. $this->zipHelper->addFileToArchive($zip, $this->rootFolder, self::CONTENT_TYPES_XML_FILE_NAME); $this->zipHelper->addFileToArchive($zip, $this->rootFolder, self::XL_FOLDER_NAME.'/'.self::WORKBOOK_XML_FILE_NAME); $this->zipHelper->addFileToArchive($zip, $this->rootFolder, self::XL_FOLDER_NAME.'/'.self::STYLES_XML_FILE_NAME); $this->zipHelper->addFolderToArchive($zip, $this->rootFolder, ZipHelper::EXISTING_FILES_SKIP); $this->zipHelper->closeArchiveAndCopyToStream($zip, $streamPointer); // once the zip is copied, remove it $this->deleteFile($zipFilePath); } /** * Creates the folder that will be used as root. * * @throws \OpenSpout\Common\Exception\IOException If unable to create the folder * * @return FileSystemHelper */ private function createRootFolder() { $this->rootFolder = $this->createFolder($this->baseFolderRealPath, uniqid('xlsx', true)); return $this; } /** * Creates the "_rels" folder under the root folder as well as the ".rels" file in it. * * @throws \OpenSpout\Common\Exception\IOException If unable to create the folder or the ".rels" file * * @return FileSystemHelper */ private function createRelsFolderAndFile() { $this->relsFolder = $this->createFolder($this->rootFolder, self::RELS_FOLDER_NAME); $this->createRelsFile(); return $this; } /** * Creates the ".rels" file under the "_rels" folder (under root). * * @throws \OpenSpout\Common\Exception\IOException If unable to create the file * * @return FileSystemHelper */ private function createRelsFile() { $relsFileContents = <<<'EOD' EOD; $this->createFileWithContents($this->relsFolder, self::RELS_FILE_NAME, $relsFileContents); return $this; } /** * Creates the "docProps" folder under the root folder as well as the "app.xml" and "core.xml" files in it. * * @throws \OpenSpout\Common\Exception\IOException If unable to create the folder or one of the files * * @return FileSystemHelper */ private function createDocPropsFolderAndFiles() { $this->docPropsFolder = $this->createFolder($this->rootFolder, self::DOC_PROPS_FOLDER_NAME); $this->createAppXmlFile(); $this->createCoreXmlFile(); return $this; } /** * Creates the "app.xml" file under the "docProps" folder. * * @throws \OpenSpout\Common\Exception\IOException If unable to create the file * * @return FileSystemHelper */ private function createAppXmlFile() { $appName = self::APP_NAME; $appXmlFileContents = << {$appName} 0 EOD; $this->createFileWithContents($this->docPropsFolder, self::APP_XML_FILE_NAME, $appXmlFileContents); return $this; } /** * Creates the "core.xml" file under the "docProps" folder. * * @throws \OpenSpout\Common\Exception\IOException If unable to create the file * * @return FileSystemHelper */ private function createCoreXmlFile() { $createdDate = (new \DateTime())->format(\DateTime::W3C); $coreXmlFileContents = << {$createdDate} {$createdDate} 0 EOD; $this->createFileWithContents($this->docPropsFolder, self::CORE_XML_FILE_NAME, $coreXmlFileContents); return $this; } /** * Creates the "xl" folder under the root folder as well as its subfolders. * * @throws \OpenSpout\Common\Exception\IOException If unable to create at least one of the folders * * @return FileSystemHelper */ private function createXlFolderAndSubFolders() { $this->xlFolder = $this->createFolder($this->rootFolder, self::XL_FOLDER_NAME); $this->createXlRelsFolder(); $this->createXlWorksheetsFolder(); return $this; } /** * Creates the "_rels" folder under the "xl" folder. * * @throws \OpenSpout\Common\Exception\IOException If unable to create the folder * * @return FileSystemHelper */ private function createXlRelsFolder() { $this->xlRelsFolder = $this->createFolder($this->xlFolder, self::RELS_FOLDER_NAME); return $this; } /** * Creates the "worksheets" folder under the "xl" folder. * * @throws \OpenSpout\Common\Exception\IOException If unable to create the folder * * @return FileSystemHelper */ private function createXlWorksheetsFolder() { $this->xlWorksheetsFolder = $this->createFolder($this->xlFolder, self::WORKSHEETS_FOLDER_NAME); return $this; } }