CacheBase.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. abstract class PHPExcel_CachedObjectStorage_CacheBase
  8. {
  9. /**
  10. * Parent worksheet
  11. *
  12. * @var PHPExcel_Worksheet
  13. */
  14. protected $parent;
  15. /**
  16. * The currently active Cell
  17. *
  18. * @var PHPExcel_Cell
  19. */
  20. protected $currentObject = null;
  21. /**
  22. * Coordinate address of the currently active Cell
  23. *
  24. * @var string
  25. */
  26. protected $currentObjectID = null;
  27. /**
  28. * Flag indicating whether the currently active Cell requires saving
  29. *
  30. * @var boolean
  31. */
  32. protected $currentCellIsDirty = true;
  33. /**
  34. * An array of cells or cell pointers for the worksheet cells held in this cache,
  35. * and indexed by their coordinate address within the worksheet
  36. *
  37. * @var array of mixed
  38. */
  39. protected $cellCache = array();
  40. /**
  41. * Initialise this new cell collection
  42. *
  43. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  44. */
  45. public function __construct(PHPExcel_Worksheet $parent)
  46. {
  47. // Set our parent worksheet.
  48. // This is maintained within the cache controller to facilitate re-attaching it to PHPExcel_Cell objects when
  49. // they are woken from a serialized state
  50. $this->parent = $parent;
  51. }
  52. /**
  53. * Return the parent worksheet for this cell collection
  54. *
  55. * @return PHPExcel_Worksheet
  56. */
  57. public function getParent()
  58. {
  59. return $this->parent;
  60. }
  61. /**
  62. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  63. *
  64. * @param string $pCoord Coordinate address of the cell to check
  65. * @return boolean
  66. */
  67. public function isDataSet($pCoord)
  68. {
  69. if ($pCoord === $this->currentObjectID) {
  70. return true;
  71. }
  72. // Check if the requested entry exists in the cache
  73. return isset($this->cellCache[$pCoord]);
  74. }
  75. /**
  76. * Move a cell object from one address to another
  77. *
  78. * @param string $fromAddress Current address of the cell to move
  79. * @param string $toAddress Destination address of the cell to move
  80. * @return boolean
  81. */
  82. public function moveCell($fromAddress, $toAddress)
  83. {
  84. if ($fromAddress === $this->currentObjectID) {
  85. $this->currentObjectID = $toAddress;
  86. }
  87. $this->currentCellIsDirty = true;
  88. if (isset($this->cellCache[$fromAddress])) {
  89. $this->cellCache[$toAddress] = &$this->cellCache[$fromAddress];
  90. unset($this->cellCache[$fromAddress]);
  91. }
  92. return true;
  93. }
  94. /**
  95. * Add or Update a cell in cache
  96. *
  97. * @param PHPExcel_Cell $cell Cell to update
  98. * @return PHPExcel_Cell
  99. * @throws PHPExcel_Exception
  100. */
  101. public function updateCacheData(PHPExcel_Cell $cell)
  102. {
  103. return $this->addCacheData($cell->getCoordinate(), $cell);
  104. }
  105. /**
  106. * Delete a cell in cache identified by coordinate address
  107. *
  108. * @param string $pCoord Coordinate address of the cell to delete
  109. * @throws PHPExcel_Exception
  110. */
  111. public function deleteCacheData($pCoord)
  112. {
  113. if ($pCoord === $this->currentObjectID && !is_null($this->currentObject)) {
  114. $this->currentObject->detach();
  115. $this->currentObjectID = $this->currentObject = null;
  116. }
  117. if (is_object($this->cellCache[$pCoord])) {
  118. $this->cellCache[$pCoord]->detach();
  119. unset($this->cellCache[$pCoord]);
  120. }
  121. $this->currentCellIsDirty = false;
  122. }
  123. /**
  124. * Get a list of all cell addresses currently held in cache
  125. *
  126. * @return string[]
  127. */
  128. public function getCellList()
  129. {
  130. return array_keys($this->cellCache);
  131. }
  132. /**
  133. * Sort the list of all cell addresses currently held in cache by row and column
  134. *
  135. * @return string[]
  136. */
  137. public function getSortedCellList()
  138. {
  139. $sortKeys = array();
  140. foreach ($this->getCellList() as $coord) {
  141. sscanf($coord, '%[A-Z]%d', $column, $row);
  142. $sortKeys[sprintf('%09d%3s', $row, $column)] = $coord;
  143. }
  144. ksort($sortKeys);
  145. return array_values($sortKeys);
  146. }
  147. /**
  148. * Get highest worksheet column and highest row that have cell records
  149. *
  150. * @return array Highest column name and highest row number
  151. */
  152. public function getHighestRowAndColumn()
  153. {
  154. // Lookup highest column and highest row
  155. $col = array('A' => '1A');
  156. $row = array(1);
  157. foreach ($this->getCellList() as $coord) {
  158. sscanf($coord, '%[A-Z]%d', $c, $r);
  159. $row[$r] = $r;
  160. $col[$c] = strlen($c).$c;
  161. }
  162. if (!empty($row)) {
  163. // Determine highest column and row
  164. $highestRow = max($row);
  165. $highestColumn = substr(max($col), 1);
  166. }
  167. return array(
  168. 'row' => $highestRow,
  169. 'column' => $highestColumn
  170. );
  171. }
  172. /**
  173. * Return the cell address of the currently active cell object
  174. *
  175. * @return string
  176. */
  177. public function getCurrentAddress()
  178. {
  179. return $this->currentObjectID;
  180. }
  181. /**
  182. * Return the column address of the currently active cell object
  183. *
  184. * @return string
  185. */
  186. public function getCurrentColumn()
  187. {
  188. sscanf($this->currentObjectID, '%[A-Z]%d', $column, $row);
  189. return $column;
  190. }
  191. /**
  192. * Return the row address of the currently active cell object
  193. *
  194. * @return integer
  195. */
  196. public function getCurrentRow()
  197. {
  198. sscanf($this->currentObjectID, '%[A-Z]%d', $column, $row);
  199. return (integer) $row;
  200. }
  201. /**
  202. * Get highest worksheet column
  203. *
  204. * @param string $row Return the highest column for the specified row,
  205. * or the highest column of any row if no row number is passed
  206. * @return string Highest column name
  207. */
  208. public function getHighestColumn($row = null)
  209. {
  210. if ($row == null) {
  211. $colRow = $this->getHighestRowAndColumn();
  212. return $colRow['column'];
  213. }
  214. $columnList = array(1);
  215. foreach ($this->getCellList() as $coord) {
  216. sscanf($coord, '%[A-Z]%d', $c, $r);
  217. if ($r != $row) {
  218. continue;
  219. }
  220. $columnList[] = PHPExcel_Cell::columnIndexFromString($c);
  221. }
  222. return PHPExcel_Cell::stringFromColumnIndex(max($columnList) - 1);
  223. }
  224. /**
  225. * Get highest worksheet row
  226. *
  227. * @param string $column Return the highest row for the specified column,
  228. * or the highest row of any column if no column letter is passed
  229. * @return int Highest row number
  230. */
  231. public function getHighestRow($column = null)
  232. {
  233. if ($column == null) {
  234. $colRow = $this->getHighestRowAndColumn();
  235. return $colRow['row'];
  236. }
  237. $rowList = array(0);
  238. foreach ($this->getCellList() as $coord) {
  239. sscanf($coord, '%[A-Z]%d', $c, $r);
  240. if ($c != $column) {
  241. continue;
  242. }
  243. $rowList[] = $r;
  244. }
  245. return max($rowList);
  246. }
  247. /**
  248. * Generate a unique ID for cache referencing
  249. *
  250. * @return string Unique Reference
  251. */
  252. protected function getUniqueID()
  253. {
  254. if (function_exists('posix_getpid')) {
  255. $baseUnique = posix_getpid();
  256. } else {
  257. $baseUnique = mt_rand();
  258. }
  259. return uniqid($baseUnique, true);
  260. }
  261. /**
  262. * Clone the cell collection
  263. *
  264. * @param PHPExcel_Worksheet $parent The new worksheet
  265. * @return void
  266. */
  267. public function copyCellCollection(PHPExcel_Worksheet $parent)
  268. {
  269. $this->currentCellIsDirty;
  270. $this->storeData();
  271. $this->parent = $parent;
  272. if (($this->currentObject !== null) && (is_object($this->currentObject))) {
  273. $this->currentObject->attach($this);
  274. }
  275. } // function copyCellCollection()
  276. /**
  277. * Remove a row, deleting all cells in that row
  278. *
  279. * @param string $row Row number to remove
  280. * @return void
  281. */
  282. public function removeRow($row)
  283. {
  284. foreach ($this->getCellList() as $coord) {
  285. sscanf($coord, '%[A-Z]%d', $c, $r);
  286. if ($r == $row) {
  287. $this->deleteCacheData($coord);
  288. }
  289. }
  290. }
  291. /**
  292. * Remove a column, deleting all cells in that column
  293. *
  294. * @param string $column Column ID to remove
  295. * @return void
  296. */
  297. public function removeColumn($column)
  298. {
  299. foreach ($this->getCellList() as $coord) {
  300. sscanf($coord, '%[A-Z]%d', $c, $r);
  301. if ($c == $column) {
  302. $this->deleteCacheData($coord);
  303. }
  304. }
  305. }
  306. /**
  307. * Identify whether the caching method is currently available
  308. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  309. *
  310. * @return boolean
  311. */
  312. public static function cacheMethodIsAvailable()
  313. {
  314. return true;
  315. }
  316. }