PHPTemp.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_CachedObjectStorage_PHPTemp extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
  8. {
  9. /**
  10. * Name of the file for this cache
  11. *
  12. * @var string
  13. */
  14. private $fileHandle = null;
  15. /**
  16. * Memory limit to use before reverting to file cache
  17. *
  18. * @var integer
  19. */
  20. private $memoryCacheSize = null;
  21. /**
  22. * Store cell data in cache for the current cell object if it's "dirty",
  23. * and the 'nullify' the current cell object
  24. *
  25. * @return void
  26. * @throws PHPExcel_Exception
  27. */
  28. protected function storeData()
  29. {
  30. if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  31. $this->currentObject->detach();
  32. fseek($this->fileHandle, 0, SEEK_END);
  33. $this->cellCache[$this->currentObjectID] = array(
  34. 'ptr' => ftell($this->fileHandle),
  35. 'sz' => fwrite($this->fileHandle, serialize($this->currentObject))
  36. );
  37. $this->currentCellIsDirty = false;
  38. }
  39. $this->currentObjectID = $this->currentObject = null;
  40. }
  41. /**
  42. * Add or Update a cell in cache identified by coordinate address
  43. *
  44. * @param string $pCoord Coordinate address of the cell to update
  45. * @param PHPExcel_Cell $cell Cell to update
  46. * @return PHPExcel_Cell
  47. * @throws PHPExcel_Exception
  48. */
  49. public function addCacheData($pCoord, PHPExcel_Cell $cell)
  50. {
  51. if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  52. $this->storeData();
  53. }
  54. $this->currentObjectID = $pCoord;
  55. $this->currentObject = $cell;
  56. $this->currentCellIsDirty = true;
  57. return $cell;
  58. }
  59. /**
  60. * Get cell at a specific coordinate
  61. *
  62. * @param string $pCoord Coordinate of the cell
  63. * @throws PHPExcel_Exception
  64. * @return PHPExcel_Cell Cell that was found, or null if not found
  65. */
  66. public function getCacheData($pCoord)
  67. {
  68. if ($pCoord === $this->currentObjectID) {
  69. return $this->currentObject;
  70. }
  71. $this->storeData();
  72. // Check if the entry that has been requested actually exists
  73. if (!isset($this->cellCache[$pCoord])) {
  74. // Return null if requested entry doesn't exist in cache
  75. return null;
  76. }
  77. // Set current entry to the requested entry
  78. $this->currentObjectID = $pCoord;
  79. fseek($this->fileHandle, $this->cellCache[$pCoord]['ptr']);
  80. $this->currentObject = unserialize(fread($this->fileHandle, $this->cellCache[$pCoord]['sz']));
  81. // Re-attach this as the cell's parent
  82. $this->currentObject->attach($this);
  83. // Return requested entry
  84. return $this->currentObject;
  85. }
  86. /**
  87. * Get a list of all cell addresses currently held in cache
  88. *
  89. * @return string[]
  90. */
  91. public function getCellList()
  92. {
  93. if ($this->currentObjectID !== null) {
  94. $this->storeData();
  95. }
  96. return parent::getCellList();
  97. }
  98. /**
  99. * Clone the cell collection
  100. *
  101. * @param PHPExcel_Worksheet $parent The new worksheet
  102. * @return void
  103. */
  104. public function copyCellCollection(PHPExcel_Worksheet $parent)
  105. {
  106. parent::copyCellCollection($parent);
  107. // Open a new stream for the cell cache data
  108. $newFileHandle = fopen('php://temp/maxmemory:' . $this->memoryCacheSize, 'a+');
  109. // Copy the existing cell cache data to the new stream
  110. fseek($this->fileHandle, 0);
  111. while (!feof($this->fileHandle)) {
  112. fwrite($newFileHandle, fread($this->fileHandle, 1024));
  113. }
  114. $this->fileHandle = $newFileHandle;
  115. }
  116. /**
  117. * Clear the cell collection and disconnect from our parent
  118. *
  119. * @return void
  120. */
  121. public function unsetWorksheetCells()
  122. {
  123. if (!is_null($this->currentObject)) {
  124. $this->currentObject->detach();
  125. $this->currentObject = $this->currentObjectID = null;
  126. }
  127. $this->cellCache = array();
  128. // detach ourself from the worksheet, so that it can then delete this object successfully
  129. $this->parent = null;
  130. // Close down the php://temp file
  131. $this->__destruct();
  132. }
  133. /**
  134. * Initialise this new cell collection
  135. *
  136. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  137. * @param array of mixed $arguments Additional initialisation arguments
  138. */
  139. public function __construct(PHPExcel_Worksheet $parent, $arguments)
  140. {
  141. $this->memoryCacheSize = (isset($arguments['memoryCacheSize'])) ? $arguments['memoryCacheSize'] : '1MB';
  142. parent::__construct($parent);
  143. if (is_null($this->fileHandle)) {
  144. $this->fileHandle = fopen('php://temp/maxmemory:' . $this->memoryCacheSize, 'a+');
  145. }
  146. }
  147. /**
  148. * Destroy this cell collection
  149. */
  150. public function __destruct()
  151. {
  152. if (!is_null($this->fileHandle)) {
  153. fclose($this->fileHandle);
  154. }
  155. $this->fileHandle = null;
  156. }
  157. }