CacheBase.Class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/8/10
  6. * Time: 6:19 PM
  7. */
  8. namespace Util\PHPExcel\CachedObjectStorage;
  9. use Util\PHPExcel\Worksheet;
  10. use Util\PHPExcel\Cell;
  11. abstract class CacheBase
  12. {
  13. /**
  14. * Coordinate address of the currently active Cell
  15. *
  16. * @var string
  17. */
  18. protected $currentObjectID = null;
  19. /**
  20. * The currently active Cell
  21. *
  22. * @var PHPExcel_Cell
  23. */
  24. protected $currentObject = null;
  25. /**
  26. * Flag indicating whether the currently active Cell requires saving
  27. *
  28. * @var boolean
  29. */
  30. protected $currentCellIsDirty = true;
  31. /**
  32. * An array of cells or cell pointers for the worksheet cells held in this cache,
  33. * and indexed by their coordinate address within the worksheet
  34. *
  35. * @var array of mixed
  36. */
  37. protected $cellCache = array();
  38. /**
  39. * Parent worksheet
  40. *
  41. * @var PHPExcel_Worksheet
  42. */
  43. protected $parent;
  44. /**
  45. * Initialise this new cell collection
  46. *
  47. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  48. */
  49. public function __construct(Worksheet $parent)
  50. {
  51. // Set our parent worksheet.
  52. // This is maintained within the cache controller to facilitate re-attaching it to PHPExcel_Cell objects when
  53. // they are woken from a serialized state
  54. $this->parent = $parent;
  55. }
  56. /**
  57. * Return the parent worksheet for this cell collection
  58. *
  59. * @return PHPExcel_Worksheet
  60. */
  61. public function getParent()
  62. {
  63. return $this->parent;
  64. }
  65. /**
  66. * Identify whether the caching method is currently available
  67. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  68. *
  69. * @return boolean
  70. */
  71. public static function cacheMethodIsAvailable()
  72. {
  73. return true;
  74. }
  75. /**
  76. * Add or Update a cell in cache
  77. *
  78. * @param PHPExcel_Cell $cell Cell to update
  79. * @return PHPExcel_Cell
  80. * @throws PHPExcel_Exception
  81. */
  82. public function updateCacheData(Cell $cell)
  83. {
  84. return $this->addCacheData($cell->getCoordinate(), $cell);
  85. }
  86. /**
  87. * Delete a cell in cache identified by coordinate address
  88. *
  89. * @param string $pCoord Coordinate address of the cell to delete
  90. * @throws PHPExcel_Exception
  91. */
  92. public function deleteCacheData($pCoord)
  93. {
  94. if ($pCoord === $this->currentObjectID && !is_null($this->currentObject)) {
  95. $this->currentObject->detach();
  96. $this->currentObjectID = $this->currentObject = null;
  97. }
  98. if (is_object($this->cellCache[$pCoord])) {
  99. $this->cellCache[$pCoord]->detach();
  100. unset($this->cellCache[$pCoord]);
  101. }
  102. $this->currentCellIsDirty = false;
  103. }
  104. /**
  105. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  106. *
  107. * @param string $pCoord Coordinate address of the cell to check
  108. * @return boolean
  109. */
  110. public function isDataSet($pCoord)
  111. {
  112. if ($pCoord === $this->currentObjectID) {
  113. return true;
  114. }
  115. // Check if the requested entry exists in the cache
  116. return isset($this->cellCache[$pCoord]);
  117. }
  118. /**
  119. * Clone the cell collection
  120. *
  121. * @param PHPExcel_Worksheet $parent The new worksheet
  122. * @return void
  123. */
  124. public function copyCellCollection(Worksheet $parent)
  125. {
  126. $this->currentCellIsDirty;
  127. $this->storeData();
  128. $this->parent = $parent;
  129. if (($this->currentObject !== null) && (is_object($this->currentObject))) {
  130. $this->currentObject->attach($this);
  131. }
  132. }
  133. /**
  134. * Sort the list of all cell addresses currently held in cache by row and column
  135. *
  136. * @return string[]
  137. */
  138. public function getSortedCellList()
  139. {
  140. $sortKeys = array();
  141. foreach ($this->getCellList() as $coord) {
  142. sscanf($coord, '%[A-Z]%d', $column, $row);
  143. $sortKeys[sprintf('%09d%3s', $row, $column)] = $coord;
  144. }
  145. ksort($sortKeys);
  146. return array_values($sortKeys);
  147. }
  148. /**
  149. * Get a list of all cell addresses currently held in cache
  150. *
  151. * @return string[]
  152. */
  153. public function getCellList()
  154. {
  155. return array_keys($this->cellCache);
  156. }
  157. /**
  158. * Return the cell address of the currently active cell object
  159. *
  160. * @return string
  161. */
  162. public function getCurrentAddress()
  163. {
  164. return $this->currentObjectID;
  165. }
  166. }