MemorySerialized.Class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/8/10
  6. * Time: 6:18 PM
  7. */
  8. namespace Util\PHPExcel\CachedObjectStorage;
  9. use Util\PHPExcel\Cell;
  10. use Util\PHPExcel\CachedObjectStorage\CacheBase;
  11. use Util\PHPExcel\CachedObjectStorage\ICache;
  12. class MemorySerialized extends CacheBase implements ICache
  13. {
  14. /**
  15. * Add or Update a cell in cache identified by coordinate address
  16. *
  17. * @param string $pCoord Coordinate address of the cell to update
  18. * @param PHPExcel_Cell $cell Cell to update
  19. * @return PHPExcel_Cell
  20. */
  21. public function addCacheData($pCoord, Cell $cell)
  22. {
  23. if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  24. $this->storeData();
  25. }
  26. $this->currentObjectID = $pCoord;
  27. $this->currentObject = $cell;
  28. $this->currentCellIsDirty = true;
  29. return $cell;
  30. }
  31. /**
  32. * Store cell data in cache for the current cell object if it's "dirty",
  33. * and the 'nullify' the current cell object
  34. *
  35. * @return void
  36. * @throws PHPExcel_Exception
  37. */
  38. protected function storeData()
  39. {
  40. if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  41. $this->currentObject->detach();
  42. $this->cellCache[$this->currentObjectID] = serialize($this->currentObject);
  43. $this->currentCellIsDirty = false;
  44. }
  45. $this->currentObjectID = $this->currentObject = null;
  46. }
  47. /**
  48. * Get cell at a specific coordinate
  49. *
  50. * @param string $pCoord Coordinate of the cell
  51. * @throws PHPExcel_Exception
  52. * @return PHPExcel_Cell Cell that was found, or null if not found
  53. */
  54. public function getCacheData($pCoord)
  55. {
  56. if ($pCoord === $this->currentObjectID) {
  57. return $this->currentObject;
  58. }
  59. $this->storeData();
  60. // Check if the entry that has been requested actually exists
  61. if (!isset($this->cellCache[$pCoord])) {
  62. // Return null if requested entry doesn't exist in cache
  63. return null;
  64. }
  65. // Set current entry to the requested entry
  66. $this->currentObjectID = $pCoord;
  67. $this->currentObject = unserialize($this->cellCache[$pCoord]);
  68. // Re-attach this as the cell's parent
  69. $this->currentObject->attach($this);
  70. // Return requested entry
  71. return $this->currentObject;
  72. }
  73. /**
  74. * Get a list of all cell addresses currently held in cache
  75. *
  76. * @return string[]
  77. */
  78. public function getCellList()
  79. {
  80. if ($this->currentObjectID !== null) {
  81. $this->storeData();
  82. }
  83. return parent::getCellList();
  84. }
  85. /**
  86. * Clear the cell collection and disconnect from our parent
  87. *
  88. * @return void
  89. */
  90. public function unsetWorksheetCells()
  91. {
  92. if (!is_null($this->currentObject)) {
  93. $this->currentObject->detach();
  94. $this->currentObject = $this->currentObjectID = null;
  95. }
  96. $this->cellCache = array();
  97. // detach ourself from the worksheet, so that it can then delete this object successfully
  98. $this->parent = null;
  99. }
  100. }