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