Memory.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * PHPExcel_CachedObjectStorage_Memory
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_CachedObjectStorage
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_CachedObjectStorage_Memory extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
  28. {
  29. /**
  30. * Dummy method callable from CacheBase, but unused by Memory cache
  31. *
  32. * @return void
  33. */
  34. protected function storeData()
  35. {
  36. }
  37. /**
  38. * Add or Update a cell in cache identified by coordinate address
  39. *
  40. * @param string $pCoord Coordinate address of the cell to update
  41. * @param PHPExcel_Cell $cell Cell to update
  42. * @return PHPExcel_Cell
  43. * @throws PHPExcel_Exception
  44. */
  45. public function addCacheData($pCoord, PHPExcel_Cell $cell)
  46. {
  47. $this->cellCache[$pCoord] = $cell;
  48. // Set current entry to the new/updated entry
  49. $this->currentObjectID = $pCoord;
  50. return $cell;
  51. }
  52. /**
  53. * Get cell at a specific coordinate
  54. *
  55. * @param string $pCoord Coordinate of the cell
  56. * @throws PHPExcel_Exception
  57. * @return PHPExcel_Cell Cell that was found, or null if not found
  58. */
  59. public function getCacheData($pCoord)
  60. {
  61. // Check if the entry that has been requested actually exists
  62. if (!isset($this->cellCache[$pCoord])) {
  63. $this->currentObjectID = null;
  64. // Return null if requested entry doesn't exist in cache
  65. return null;
  66. }
  67. // Set current entry to the requested entry
  68. $this->currentObjectID = $pCoord;
  69. // Return requested entry
  70. return $this->cellCache[$pCoord];
  71. }
  72. /**
  73. * Clone the cell collection
  74. *
  75. * @param PHPExcel_Worksheet $parent The new worksheet
  76. */
  77. public function copyCellCollection(PHPExcel_Worksheet $parent)
  78. {
  79. parent::copyCellCollection($parent);
  80. $newCollection = array();
  81. foreach ($this->cellCache as $k => &$cell) {
  82. $newCollection[$k] = clone $cell;
  83. $newCollection[$k]->attach($this);
  84. }
  85. $this->cellCache = $newCollection;
  86. }
  87. /**
  88. * Clear the cell collection and disconnect from our parent
  89. *
  90. */
  91. public function unsetWorksheetCells()
  92. {
  93. // Because cells are all stored as intact objects in memory, we need to detach each one from the parent
  94. foreach ($this->cellCache as $k => &$cell) {
  95. $cell->detach();
  96. $this->cellCache[$k] = null;
  97. }
  98. unset($cell);
  99. $this->cellCache = array();
  100. // detach ourself from the worksheet, so that it can then delete this object successfully
  101. $this->parent = null;
  102. }
  103. }