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