DiscISAM.php 5.9 KB

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