1 | <?php |
||
18 | class FileBasedStrategy implements CachingStrategyInterface |
||
19 | { |
||
20 | /** Value to use to escape the line feed character ("\n") */ |
||
21 | const ESCAPED_LINE_FEED_CHARACTER = '_x000A_'; |
||
22 | |||
23 | /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ |
||
24 | protected $globalFunctionsHelper; |
||
25 | |||
26 | /** @var \Box\Spout\Common\Helper\FileSystemHelper Helper to perform file system operations */ |
||
27 | protected $fileSystemHelper; |
||
28 | |||
29 | /** @var string Temporary folder where the temporary files will be created */ |
||
30 | protected $tempFolder; |
||
31 | |||
32 | /** |
||
33 | * @var int Maximum number of strings that can be stored in one temp file |
||
34 | * @see CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE |
||
35 | */ |
||
36 | protected $maxNumStringsPerTempFile; |
||
37 | |||
38 | /** @var resource Pointer to the last temp file a shared string was written to */ |
||
39 | protected $tempFilePointer; |
||
40 | |||
41 | /** |
||
42 | * @var string Path of the temporary file whose contents is currently stored in memory |
||
43 | * @see CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE |
||
44 | */ |
||
45 | protected $inMemoryTempFilePath; |
||
46 | |||
47 | /** |
||
48 | * @var array Contents of the temporary file that was last read |
||
49 | * @see CachingStrategyFactory::MAX_NUM_STRINGS_PER_TEMP_FILE |
||
50 | */ |
||
51 | protected $inMemoryTempFileContents; |
||
52 | |||
53 | /** |
||
54 | * @param string|null $tempFolder Temporary folder where the temporary files to store shared strings will be stored |
||
55 | * @param int $maxNumStringsPerTempFile Maximum number of strings that can be stored in one temp file |
||
56 | */ |
||
57 | public function __construct($tempFolder, $maxNumStringsPerTempFile) |
||
68 | |||
69 | /** |
||
70 | * Adds the given string to the cache. |
||
71 | * |
||
72 | * @param string $sharedString The string to be added to the cache |
||
73 | * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file |
||
74 | * @return void |
||
75 | */ |
||
76 | public function addStringForIndex($sharedString, $sharedStringIndex) |
||
93 | |||
94 | /** |
||
95 | * Returns the path for the temp file that should contain the string for the given index |
||
96 | * |
||
97 | * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file |
||
98 | * @return string The temp file path for the given index |
||
99 | */ |
||
100 | protected function getSharedStringTempFilePath($sharedStringIndex) |
||
105 | |||
106 | /** |
||
107 | * Closes the cache after the last shared string was added. |
||
108 | * This prevents any additional string from being added to the cache. |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | public function closeCache() |
||
119 | |||
120 | |||
121 | /** |
||
122 | * Returns the string located at the given index from the cache. |
||
123 | * |
||
124 | * @param int $sharedStringIndex Index of the shared string in the sharedStrings.xml file |
||
125 | * @return string The shared string at the given index |
||
126 | * @throws \Box\Spout\Reader\Exception\SharedStringNotFoundException If no shared string found for the given index |
||
127 | */ |
||
128 | public function getStringAtIndex($sharedStringIndex) |
||
159 | |||
160 | /** |
||
161 | * Escapes the line feed characters (\n) |
||
162 | * |
||
163 | * @param string $unescapedString |
||
164 | * @return string |
||
165 | */ |
||
166 | private function escapeLineFeed($unescapedString) |
||
170 | |||
171 | /** |
||
172 | * Unescapes the line feed characters (\n) |
||
173 | * |
||
174 | * @param string $escapedString |
||
175 | * @return string |
||
176 | */ |
||
177 | private function unescapeLineFeed($escapedString) |
||
181 | |||
182 | /** |
||
183 | * Destroys the cache, freeing memory and removing any created artifacts |
||
184 | * |
||
185 | * @return void |
||
186 | */ |
||
187 | public function clearCache() |
||
193 | } |
||
194 |