Update to cloning the Cell Collection

This commit is contained in:
MarkBaker 2022-05-01 12:55:05 +02:00
parent 5112503a06
commit 3db129dc52
1 changed files with 10 additions and 21 deletions

View File

@ -297,28 +297,17 @@ class Cells
$newCollection = clone $this; $newCollection = clone $this;
$newCollection->parent = $worksheet; $newCollection->parent = $worksheet;
if (is_object($newCollection->currentCell)) {
$newCollection->currentCell->attach($this);
}
// Get old values
$oldKeys = $newCollection->getAllCacheKeys();
$oldValues = $newCollection->cache->getMultiple($oldKeys);
$newValues = [];
$oldCachePrefix = $newCollection->cachePrefix;
// Change prefix
$newCollection->cachePrefix = $newCollection->getUniqueID(); $newCollection->cachePrefix = $newCollection->getUniqueID();
foreach ($oldValues as $oldKey => $value) {
/** @var string $newKey */
$newKey = str_replace($oldCachePrefix, $newCollection->cachePrefix, $oldKey);
$newValues[$newKey] = clone $value;
}
// Store new values foreach ($this->index as $key => $value) {
$stored = $newCollection->cache->setMultiple($newValues); $newCollection->index[$key] = $value;
if ($stored === false) { $stored = $newCollection->cache->set(
$this->destructIfNeeded($newCollection, 'Failed to copy cells in cache'); $newCollection->cachePrefix . $key,
clone $this->cache->get($this->cachePrefix . $key)
);
if ($stored === false) {
$this->destructIfNeeded($newCollection, 'Failed to copy cells in cache');
}
} }
return $newCollection; return $newCollection;
@ -436,7 +425,7 @@ class Cells
return null; return null;
} }
// Check if the entry that has been requested actually exists // Check if the entry that has been requested actually exists in the cache
$cell = $this->cache->get($this->cachePrefix . $cellCoordinate); $cell = $this->cache->get($this->cachePrefix . $cellCoordinate);
if ($cell === null) { if ($cell === null) {
throw new PhpSpreadsheetException("Cell entry {$cellCoordinate} no longer exists in cache. This probably means that the cache was cleared by someone else."); throw new PhpSpreadsheetException("Cell entry {$cellCoordinate} no longer exists in cache. This probably means that the cache was cleared by someone else.");