1 | <?php |
||
18 | class FileSystemHelper extends \Box\Spout\Common\Helper\FileSystemHelper implements FileSystemWithRootFolderHelperInterface |
||
19 | { |
||
20 | const APP_NAME = 'Spout'; |
||
21 | const MIMETYPE = 'application/vnd.oasis.opendocument.spreadsheet'; |
||
22 | |||
23 | const META_INF_FOLDER_NAME = 'META-INF'; |
||
24 | const SHEETS_CONTENT_TEMP_FOLDER_NAME = 'worksheets-temp'; |
||
25 | |||
26 | const MANIFEST_XML_FILE_NAME = 'manifest.xml'; |
||
27 | const CONTENT_XML_FILE_NAME = 'content.xml'; |
||
28 | const META_XML_FILE_NAME = 'meta.xml'; |
||
29 | const MIMETYPE_FILE_NAME = 'mimetype'; |
||
30 | const STYLES_XML_FILE_NAME = 'styles.xml'; |
||
31 | |||
32 | /** @var ZipHelper Helper to perform tasks with Zip archive */ |
||
33 | private $zipHelper; |
||
34 | |||
35 | /** @var string Path to the root folder inside the temp folder where the files to create the ODS will be stored */ |
||
36 | protected $rootFolder; |
||
37 | |||
38 | /** @var string Path to the "META-INF" folder inside the root folder */ |
||
39 | protected $metaInfFolder; |
||
40 | |||
41 | /** @var string Path to the temp folder, inside the root folder, where specific sheets content will be written to */ |
||
42 | protected $sheetsContentTempFolder; |
||
43 | |||
44 | /** |
||
45 | * @param string $baseFolderPath The path of the base folder where all the I/O can occur |
||
46 | * @param ZipHelper $zipHelper Helper to perform tasks with Zip archive |
||
47 | */ |
||
48 | 43 | public function __construct($baseFolderPath, $zipHelper) |
|
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | 34 | public function getRootFolder() |
|
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | 43 | public function getSheetsContentTempFolder() |
|
69 | |||
70 | /** |
||
71 | * Creates all the folders needed to create a ODS file, as well as the files that won't change. |
||
72 | * |
||
73 | * @return void |
||
74 | * @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders |
||
75 | */ |
||
76 | 43 | public function createBaseFilesAndFolders() |
|
85 | |||
86 | /** |
||
87 | * Creates the folder that will be used as root |
||
88 | * |
||
89 | * @return FileSystemHelper |
||
90 | * @throws \Box\Spout\Common\Exception\IOException If unable to create the folder |
||
91 | */ |
||
92 | 43 | protected function createRootFolder() |
|
97 | |||
98 | /** |
||
99 | * Creates the "META-INF" folder under the root folder as well as the "manifest.xml" file in it |
||
100 | * |
||
101 | * @return FileSystemHelper |
||
102 | * @throws \Box\Spout\Common\Exception\IOException If unable to create the folder or the "manifest.xml" file |
||
103 | */ |
||
104 | 43 | protected function createMetaInfoFolderAndFile() |
|
112 | |||
113 | /** |
||
114 | * Creates the "manifest.xml" file under the "META-INF" folder (under root) |
||
115 | * |
||
116 | * @return FileSystemHelper |
||
117 | * @throws \Box\Spout\Common\Exception\IOException If unable to create the file |
||
118 | */ |
||
119 | 43 | protected function createManifestFile() |
|
135 | |||
136 | /** |
||
137 | * Creates the temp folder where specific sheets content will be written to. |
||
138 | * This folder is not part of the final ODS file and is only used to be able to jump between sheets. |
||
139 | * |
||
140 | * @return FileSystemHelper |
||
141 | * @throws \Box\Spout\Common\Exception\IOException If unable to create the folder |
||
142 | */ |
||
143 | 43 | protected function createSheetsContentTempFolder() |
|
148 | |||
149 | /** |
||
150 | * Creates the "meta.xml" file under the root folder |
||
151 | * |
||
152 | * @return FileSystemHelper |
||
153 | * @throws \Box\Spout\Common\Exception\IOException If unable to create the file |
||
154 | */ |
||
155 | 43 | protected function createMetaFile() |
|
175 | |||
176 | /** |
||
177 | * Creates the "mimetype" file under the root folder |
||
178 | * |
||
179 | * @return FileSystemHelper |
||
180 | * @throws \Box\Spout\Common\Exception\IOException If unable to create the file |
||
181 | */ |
||
182 | 43 | protected function createMimetypeFile() |
|
187 | |||
188 | /** |
||
189 | * Creates the "content.xml" file under the root folder |
||
190 | * |
||
191 | * @param WorksheetManager $worksheetManager |
||
192 | * @param StyleManager $styleManager |
||
193 | * @param Worksheet[] $worksheets |
||
194 | * @return FileSystemHelper |
||
195 | */ |
||
196 | 34 | public function createContentFile($worksheetManager, $styleManager, $worksheets) |
|
231 | |||
232 | /** |
||
233 | * Streams the content of the file at the given path into the target resource. |
||
234 | * Depending on which mode the target resource was created with, it will truncate then copy |
||
235 | * or append the content to the target file. |
||
236 | * |
||
237 | * @param string $sourceFilePath Path of the file whose content will be copied |
||
238 | * @param resource $targetResource Target resource that will receive the content |
||
239 | * @return void |
||
240 | */ |
||
241 | 34 | protected function copyFileContentsToTarget($sourceFilePath, $targetResource) |
|
247 | |||
248 | /** |
||
249 | * Deletes the temporary folder where sheets content was stored. |
||
250 | * |
||
251 | * @return FileSystemHelper |
||
252 | */ |
||
253 | 34 | public function deleteWorksheetTempFolder() |
|
258 | |||
259 | |||
260 | /** |
||
261 | * Creates the "styles.xml" file under the root folder |
||
262 | * |
||
263 | * @param StyleManager $styleManager |
||
264 | * @param int $numWorksheets Number of created worksheets |
||
265 | * @return FileSystemHelper |
||
266 | */ |
||
267 | 34 | public function createStylesFile($styleManager, $numWorksheets) |
|
274 | |||
275 | /** |
||
276 | * Zips the root folder and streams the contents of the zip into the given stream |
||
277 | * |
||
278 | * @param resource $streamPointer Pointer to the stream to copy the zip |
||
279 | * @return void |
||
280 | */ |
||
281 | 34 | public function zipRootFolderAndCopyToStream($streamPointer) |
|
298 | } |
||
299 |