Added option for SharedStringsHelper to turn off the file cache
This commit is contained in:
parent
d4cf853270
commit
2a5f4f973e
@ -34,6 +34,9 @@ class SharedStringsHelper
|
|||||||
/** Value to use to escape the line feed character ("\n") */
|
/** Value to use to escape the line feed character ("\n") */
|
||||||
const ESCAPED_LINE_FEED_CHARACTER = '_x000A_';
|
const ESCAPED_LINE_FEED_CHARACTER = '_x000A_';
|
||||||
|
|
||||||
|
/** This will increase your memory usage but can improve your execution time */
|
||||||
|
static public $KEEP_ALL_STRINGS_IN_MEMORY = false;
|
||||||
|
|
||||||
/** @var string Path of the XLSX file being read */
|
/** @var string Path of the XLSX file being read */
|
||||||
protected $filePath;
|
protected $filePath;
|
||||||
|
|
||||||
@ -56,7 +59,7 @@ class SharedStringsHelper
|
|||||||
* @var string Contents of the temporary file that was last read
|
* @var string Contents of the temporary file that was last read
|
||||||
* @see MAX_NUM_STRINGS_PER_TEMP_FILE
|
* @see MAX_NUM_STRINGS_PER_TEMP_FILE
|
||||||
*/
|
*/
|
||||||
protected $inMemoryTempFileContents;
|
protected $inMemoryContents;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $filePath Path of the XLSX file being read
|
* @param string $filePath Path of the XLSX file being read
|
||||||
@ -141,11 +144,19 @@ class SharedStringsHelper
|
|||||||
|
|
||||||
$unescapedTextValue = $escaper->unescape($textValue);
|
$unescapedTextValue = $escaper->unescape($textValue);
|
||||||
|
|
||||||
|
if (self::$KEEP_ALL_STRINGS_IN_MEMORY) {
|
||||||
|
if (!Isset($this->inMemoryContents)) {
|
||||||
|
$this->inMemoryContents = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->inMemoryContents[$sharedStringIndex] = $unescapedTextValue;
|
||||||
|
} else {
|
||||||
// The shared string retrieval logic expects each cell data to be on one line only
|
// The shared string retrieval logic expects each cell data to be on one line only
|
||||||
// Encoding the line feed character allows to preserve this assumption
|
// Encoding the line feed character allows to preserve this assumption
|
||||||
$lineFeedEncodedTextValue = $this->escapeLineFeed($unescapedTextValue);
|
$lineFeedEncodedTextValue = $this->escapeLineFeed($unescapedTextValue);
|
||||||
|
|
||||||
$this->writeSharedStringToTempFile($lineFeedEncodedTextValue, $sharedStringIndex);
|
$this->writeSharedStringToTempFile($lineFeedEncodedTextValue, $sharedStringIndex);
|
||||||
|
}
|
||||||
|
|
||||||
$sharedStringIndex++;
|
$sharedStringIndex++;
|
||||||
|
|
||||||
@ -263,6 +274,13 @@ class SharedStringsHelper
|
|||||||
*/
|
*/
|
||||||
public function getStringAtIndex($sharedStringIndex)
|
public function getStringAtIndex($sharedStringIndex)
|
||||||
{
|
{
|
||||||
|
$sharedString = null;
|
||||||
|
|
||||||
|
if (self::$KEEP_ALL_STRINGS_IN_MEMORY) {
|
||||||
|
if (isset($this->inMemoryContents) && array_key_exists($sharedStringIndex, $this->inMemoryContents)) {
|
||||||
|
$sharedString = $this->inMemoryContents[$sharedStringIndex];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
$tempFilePath = $this->getSharedStringTempFilePath($sharedStringIndex);
|
$tempFilePath = $this->getSharedStringTempFilePath($sharedStringIndex);
|
||||||
$indexInFile = $sharedStringIndex % self::MAX_NUM_STRINGS_PER_TEMP_FILE;
|
$indexInFile = $sharedStringIndex % self::MAX_NUM_STRINGS_PER_TEMP_FILE;
|
||||||
|
|
||||||
@ -272,17 +290,17 @@ class SharedStringsHelper
|
|||||||
|
|
||||||
if ($this->inMemoryTempFilePath !== $tempFilePath) {
|
if ($this->inMemoryTempFilePath !== $tempFilePath) {
|
||||||
// free memory
|
// free memory
|
||||||
unset($this->inMemoryTempFileContents);
|
unset($this->inMemoryContents);
|
||||||
|
|
||||||
$this->inMemoryTempFileContents = explode(PHP_EOL, file_get_contents($tempFilePath));
|
$this->inMemoryContents = explode(PHP_EOL, file_get_contents($tempFilePath));
|
||||||
$this->inMemoryTempFilePath = $tempFilePath;
|
$this->inMemoryTempFilePath = $tempFilePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
$sharedString = null;
|
if (array_key_exists($indexInFile, $this->inMemoryContents)) {
|
||||||
if (array_key_exists($indexInFile, $this->inMemoryTempFileContents)) {
|
$escapedSharedString = $this->inMemoryContents[$indexInFile];
|
||||||
$escapedSharedString = $this->inMemoryTempFileContents[$indexInFile];
|
|
||||||
$sharedString = $this->unescapeLineFeed($escapedSharedString);
|
$sharedString = $this->unescapeLineFeed($escapedSharedString);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($sharedString === null) {
|
if ($sharedString === null) {
|
||||||
throw new SharedStringNotFoundException("Shared string not found for index: $sharedStringIndex");
|
throw new SharedStringNotFoundException("Shared string not found for index: $sharedStringIndex");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user