From f99eb8dbbaa3f70cc60f4ccf5493c54e4fa0afe4 Mon Sep 17 00:00:00 2001 From: ankitm123 Date: Mon, 6 Mar 2017 08:58:19 -0500 Subject: [PATCH] Cloned worksheet updates were reflected on original (MemoryDrawing) We need to make note of two things: - The old code was trying to clone an objectArray, and hence was performing a shallow copy of memoryDrawing objects (the __clone magic function was not getting invoked). Instead, if one loops over the objectArray, and then clones the individual memory drawing objects, then the __clone function for the MemoryDrawing object is invoked. - The __clone function for memory drawing was using the clone keyword which does not deal with circular references (Since memoryDrawing object had references to worksheet object, it was encountering an infinite loop). However, serializing and unserializing objects deals with circular references pretty well. Fixes #92 Closes #106 --- src/PhpSpreadsheet/Worksheet.php | 9 ++++++++- src/PhpSpreadsheet/Worksheet/MemoryDrawing.php | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/PhpSpreadsheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet.php index 65219664..4111bddb 100644 --- a/src/PhpSpreadsheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheet; +use ArrayObject; + /** * Copyright (c) 2006 - 2016 PhpSpreadsheet. * @@ -2975,7 +2977,12 @@ class Worksheet implements IComparable $newCollection->copyCellCollection($this); $this->cellCollection = $newCollection; } elseif ($key == 'drawingCollection') { - $newCollection = clone $this->drawingCollection; + $newCollection = new ArrayObject(); + foreach ($this->drawingCollection as $id => $item) { + if (is_object($item)) { + $newCollection[$id] = clone $this->drawingCollection[$id]; + } + } $this->drawingCollection = $newCollection; } elseif (($key == 'autoFilter') && ($this->autoFilter instanceof Worksheet\AutoFilter)) { $newAutoFilter = clone $this->autoFilter; diff --git a/src/PhpSpreadsheet/Worksheet/MemoryDrawing.php b/src/PhpSpreadsheet/Worksheet/MemoryDrawing.php index 130f0129..4011aa59 100644 --- a/src/PhpSpreadsheet/Worksheet/MemoryDrawing.php +++ b/src/PhpSpreadsheet/Worksheet/MemoryDrawing.php @@ -197,7 +197,7 @@ class MemoryDrawing extends BaseDrawing implements \PhpOffice\PhpSpreadsheet\ICo $vars = get_object_vars($this); foreach ($vars as $key => $value) { if (is_object($value)) { - $this->$key = clone $value; + $this->$key = unserialize(serialize($value)); } else { $this->$key = $value; }