MemoryGZip.php 3.4 KB

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