Merge d52e87d4bde2d5259425a1ba9103bdd37c006d6d into 3edb056286e9f047bcddff4350c00b00e70718b6
This commit is contained in:
commit
ce7a750746
@ -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_';
|
||||||
|
|
||||||
|
/** Disabling this will increase your memory usage but can improve your execution time */
|
||||||
|
protected $useSharedStringsFileCache;
|
||||||
|
|
||||||
/** @var string Path of the XLSX file being read */
|
/** @var string Path of the XLSX file being read */
|
||||||
protected $filePath;
|
protected $filePath;
|
||||||
|
|
||||||
@ -56,15 +59,17 @@ 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
|
||||||
* @param string|void $tempFolder Temporary folder where the temporary files to store shared strings will be stored
|
* @param string|void $tempFolder Temporary folder where the temporary files to store shared strings will be stored
|
||||||
|
* @param bool|void $useSharedStringsFileCache Disabling this will increase your memory usage but can improve your execution time
|
||||||
*/
|
*/
|
||||||
public function __construct($filePath, $tempFolder = null)
|
public function __construct($filePath, $tempFolder = null, $useSharedStringsFileCache = true)
|
||||||
{
|
{
|
||||||
$this->filePath = $filePath;
|
$this->filePath = $filePath;
|
||||||
|
$this->useSharedStringsFileCache = $useSharedStringsFileCache;
|
||||||
|
|
||||||
$rootTempFolder = ($tempFolder) ?: sys_get_temp_dir();
|
$rootTempFolder = ($tempFolder) ?: sys_get_temp_dir();
|
||||||
$this->fileSystemHelper = new FileSystemHelper($rootTempFolder);
|
$this->fileSystemHelper = new FileSystemHelper($rootTempFolder);
|
||||||
@ -141,11 +146,19 @@ class SharedStringsHelper
|
|||||||
|
|
||||||
$unescapedTextValue = $escaper->unescape($textValue);
|
$unescapedTextValue = $escaper->unescape($textValue);
|
||||||
|
|
||||||
// The shared string retrieval logic expects each cell data to be on one line only
|
if ($this->useSharedStringsFileCache) {
|
||||||
// Encoding the line feed character allows to preserve this assumption
|
// The shared string retrieval logic expects each cell data to be on one line only
|
||||||
$lineFeedEncodedTextValue = $this->escapeLineFeed($unescapedTextValue);
|
// Encoding the line feed character allows to preserve this assumption
|
||||||
|
$lineFeedEncodedTextValue = $this->escapeLineFeed($unescapedTextValue);
|
||||||
$this->writeSharedStringToTempFile($lineFeedEncodedTextValue, $sharedStringIndex);
|
|
||||||
|
$this->writeSharedStringToTempFile($lineFeedEncodedTextValue, $sharedStringIndex);
|
||||||
|
} else {
|
||||||
|
if (!isset($this->inMemoryContents)) {
|
||||||
|
$this->inMemoryContents = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->inMemoryContents[$sharedStringIndex] = $unescapedTextValue;
|
||||||
|
}
|
||||||
|
|
||||||
$sharedStringIndex++;
|
$sharedStringIndex++;
|
||||||
|
|
||||||
@ -262,25 +275,32 @@ class SharedStringsHelper
|
|||||||
*/
|
*/
|
||||||
public function getStringAtIndex($sharedStringIndex)
|
public function getStringAtIndex($sharedStringIndex)
|
||||||
{
|
{
|
||||||
$tempFilePath = $this->getSharedStringTempFilePath($sharedStringIndex);
|
|
||||||
$indexInFile = $sharedStringIndex % self::MAX_NUM_STRINGS_PER_TEMP_FILE;
|
|
||||||
|
|
||||||
if (!file_exists($tempFilePath)) {
|
|
||||||
throw new SharedStringNotFoundException("Shared string temp file not found: $tempFilePath ; for index: $sharedStringIndex");
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->inMemoryTempFilePath !== $tempFilePath) {
|
|
||||||
// free memory
|
|
||||||
unset($this->inMemoryTempFileContents);
|
|
||||||
|
|
||||||
$this->inMemoryTempFileContents = explode(PHP_EOL, file_get_contents($tempFilePath));
|
|
||||||
$this->inMemoryTempFilePath = $tempFilePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
$sharedString = null;
|
$sharedString = null;
|
||||||
if (array_key_exists($indexInFile, $this->inMemoryTempFileContents)) {
|
|
||||||
$escapedSharedString = $this->inMemoryTempFileContents[$indexInFile];
|
if ($this->useSharedStringsFileCache) {
|
||||||
$sharedString = $this->unescapeLineFeed($escapedSharedString);
|
$tempFilePath = $this->getSharedStringTempFilePath($sharedStringIndex);
|
||||||
|
$indexInFile = $sharedStringIndex % self::MAX_NUM_STRINGS_PER_TEMP_FILE;
|
||||||
|
|
||||||
|
if (!file_exists($tempFilePath)) {
|
||||||
|
throw new SharedStringNotFoundException("Shared string temp file not found: $tempFilePath ; for index: $sharedStringIndex");
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->inMemoryTempFilePath !== $tempFilePath) {
|
||||||
|
// free memory
|
||||||
|
unset($this->inMemoryContents);
|
||||||
|
|
||||||
|
$this->inMemoryContents = explode(PHP_EOL, file_get_contents($tempFilePath));
|
||||||
|
$this->inMemoryTempFilePath = $tempFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists($indexInFile, $this->inMemoryContents)) {
|
||||||
|
$escapedSharedString = $this->inMemoryContents[$indexInFile];
|
||||||
|
$sharedString = $this->unescapeLineFeed($escapedSharedString);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (is_array($this->inMemoryContents) && array_key_exists($sharedStringIndex, $this->inMemoryContents)) {
|
||||||
|
$sharedString = $this->inMemoryContents[$sharedStringIndex];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($sharedString === null) {
|
if ($sharedString === null) {
|
||||||
|
@ -32,6 +32,9 @@ class XLSX extends AbstractReader
|
|||||||
|
|
||||||
/** @var string Temporary folder where the temporary files will be created */
|
/** @var string Temporary folder where the temporary files will be created */
|
||||||
protected $tempFolder;
|
protected $tempFolder;
|
||||||
|
|
||||||
|
/** @var bool Disabling this will increase your memory usage but can improve your execution time */
|
||||||
|
protected $useSharedStringsFileCache = true;
|
||||||
|
|
||||||
/** @var \ZipArchive */
|
/** @var \ZipArchive */
|
||||||
protected $zip;
|
protected $zip;
|
||||||
@ -63,6 +66,19 @@ class XLSX extends AbstractReader
|
|||||||
$this->tempFolder = $tempFolder;
|
$this->tempFolder = $tempFolder;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disabling the shared strings file cache will increase your memory usage but can improve your execution time.
|
||||||
|
* The shared strings file cache is active by default.
|
||||||
|
*
|
||||||
|
* @param bool $useSharedStringsFileCache
|
||||||
|
* @return XLSX
|
||||||
|
*/
|
||||||
|
public function setUseSharedStringsFileCache($useSharedStringsFileCache)
|
||||||
|
{
|
||||||
|
$this->useSharedStringsFileCache = $useSharedStringsFileCache;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens the file at the given file path to make it ready to be read.
|
* Opens the file at the given file path to make it ready to be read.
|
||||||
@ -80,7 +96,7 @@ class XLSX extends AbstractReader
|
|||||||
$this->zip = new \ZipArchive();
|
$this->zip = new \ZipArchive();
|
||||||
|
|
||||||
if ($this->zip->open($filePath) === true) {
|
if ($this->zip->open($filePath) === true) {
|
||||||
$this->sharedStringsHelper = new SharedStringsHelper($filePath, $this->tempFolder);
|
$this->sharedStringsHelper = new SharedStringsHelper($filePath, $this->tempFolder, $this->useSharedStringsFileCache);
|
||||||
|
|
||||||
if ($this->sharedStringsHelper->hasSharedStrings()) {
|
if ($this->sharedStringsHelper->hasSharedStrings()) {
|
||||||
// Extracts all the strings from the worksheets for easy access in the future
|
// Extracts all the strings from the worksheets for easy access in the future
|
||||||
|
Loading…
x
Reference in New Issue
Block a user