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