CacheBase.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_CachedObjectStorage
  23. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_CacheBase
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. abstract class PHPExcel_CachedObjectStorage_CacheBase {
  35. /**
  36. * Parent worksheet
  37. *
  38. * @var PHPExcel_Worksheet
  39. */
  40. protected $_parent;
  41. /**
  42. * The currently active Cell
  43. *
  44. * @var PHPExcel_Cell
  45. */
  46. protected $_currentObject = null;
  47. /**
  48. * Coordinate address of the currently active Cell
  49. *
  50. * @var string
  51. */
  52. protected $_currentObjectID = null;
  53. /**
  54. * Flag indicating whether the currently active Cell requires saving
  55. *
  56. * @var boolean
  57. */
  58. protected $_currentCellIsDirty = true;
  59. /**
  60. * An array of cells or cell pointers for the worksheet cells held in this cache,
  61. * and indexed by their coordinate address within the worksheet
  62. *
  63. * @var array of mixed
  64. */
  65. protected $_cellCache = array();
  66. /**
  67. * Initialise this new cell collection
  68. *
  69. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  70. */
  71. public function __construct(PHPExcel_Worksheet $parent) {
  72. // Set our parent worksheet.
  73. // This is maintained within the cache controller to facilitate re-attaching it to PHPExcel_Cell objects when
  74. // they are woken from a serialized state
  75. $this->_parent = $parent;
  76. } // function __construct()
  77. /**
  78. * Return the parent worksheet for this cell collection
  79. *
  80. * @return PHPExcel_Worksheet
  81. */
  82. public function getParent()
  83. {
  84. return $this->_parent;
  85. }
  86. /**
  87. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  88. *
  89. * @param string $pCoord Coordinate address of the cell to check
  90. * @return boolean
  91. */
  92. public function isDataSet($pCoord) {
  93. if ($pCoord === $this->_currentObjectID) {
  94. return true;
  95. }
  96. // Check if the requested entry exists in the cache
  97. return isset($this->_cellCache[$pCoord]);
  98. } // function isDataSet()
  99. /**
  100. * Move a cell object from one address to another
  101. *
  102. * @param string $fromAddress Current address of the cell to move
  103. * @param string $toAddress Destination address of the cell to move
  104. * @return boolean
  105. */
  106. public function moveCell($fromAddress, $toAddress) {
  107. if ($fromAddress === $this->_currentObjectID) {
  108. $this->_currentObjectID = $toAddress;
  109. }
  110. $this->_currentCellIsDirty = true;
  111. if (isset($this->_cellCache[$fromAddress])) {
  112. $this->_cellCache[$toAddress] = &$this->_cellCache[$fromAddress];
  113. unset($this->_cellCache[$fromAddress]);
  114. }
  115. return TRUE;
  116. } // function moveCell()
  117. /**
  118. * Add or Update a cell in cache
  119. *
  120. * @param PHPExcel_Cell $cell Cell to update
  121. * @return void
  122. * @throws PHPExcel_Exception
  123. */
  124. public function updateCacheData(PHPExcel_Cell $cell) {
  125. return $this->addCacheData($cell->getCoordinate(),$cell);
  126. } // function updateCacheData()
  127. /**
  128. * Delete a cell in cache identified by coordinate address
  129. *
  130. * @param string $pCoord Coordinate address of the cell to delete
  131. * @throws PHPExcel_Exception
  132. */
  133. public function deleteCacheData($pCoord) {
  134. if ($pCoord === $this->_currentObjectID) {
  135. $this->_currentObject->detach();
  136. $this->_currentObjectID = $this->_currentObject = null;
  137. }
  138. if (is_object($this->_cellCache[$pCoord])) {
  139. $this->_cellCache[$pCoord]->detach();
  140. unset($this->_cellCache[$pCoord]);
  141. }
  142. $this->_currentCellIsDirty = false;
  143. } // function deleteCacheData()
  144. /**
  145. * Get a list of all cell addresses currently held in cache
  146. *
  147. * @return array of string
  148. */
  149. public function getCellList() {
  150. return array_keys($this->_cellCache);
  151. } // function getCellList()
  152. /**
  153. * Sort the list of all cell addresses currently held in cache by row and column
  154. *
  155. * @return void
  156. */
  157. public function getSortedCellList() {
  158. $sortKeys = array();
  159. foreach ($this->getCellList() as $coord) {
  160. sscanf($coord,'%[A-Z]%d', $column, $row);
  161. $sortKeys[sprintf('%09d%3s',$row,$column)] = $coord;
  162. }
  163. ksort($sortKeys);
  164. return array_values($sortKeys);
  165. } // function sortCellList()
  166. /**
  167. * Get highest worksheet column and highest row that have cell records
  168. *
  169. * @return array Highest column name and highest row number
  170. */
  171. public function getHighestRowAndColumn()
  172. {
  173. // Lookup highest column and highest row
  174. $col = array('A' => '1A');
  175. $row = array(1);
  176. foreach ($this->getCellList() as $coord) {
  177. sscanf($coord,'%[A-Z]%d', $c, $r);
  178. $row[$r] = $r;
  179. $col[$c] = strlen($c).$c;
  180. }
  181. if (!empty($row)) {
  182. // Determine highest column and row
  183. $highestRow = max($row);
  184. $highestColumn = substr(max($col),1);
  185. }
  186. return array( 'row' => $highestRow,
  187. 'column' => $highestColumn
  188. );
  189. }
  190. /**
  191. * Return the cell address of the currently active cell object
  192. *
  193. * @return string
  194. */
  195. public function getCurrentAddress()
  196. {
  197. return $this->_currentObjectID;
  198. }
  199. /**
  200. * Return the column address of the currently active cell object
  201. *
  202. * @return string
  203. */
  204. public function getCurrentColumn()
  205. {
  206. sscanf($this->_currentObjectID, '%[A-Z]%d', $column, $row);
  207. return $column;
  208. }
  209. /**
  210. * Return the row address of the currently active cell object
  211. *
  212. * @return string
  213. */
  214. public function getCurrentRow()
  215. {
  216. sscanf($this->_currentObjectID, '%[A-Z]%d', $column, $row);
  217. return $row;
  218. }
  219. /**
  220. * Get highest worksheet column
  221. *
  222. * @return string Highest column name
  223. */
  224. public function getHighestColumn()
  225. {
  226. $colRow = $this->getHighestRowAndColumn();
  227. return $colRow['column'];
  228. }
  229. /**
  230. * Get highest worksheet row
  231. *
  232. * @return int Highest row number
  233. */
  234. public function getHighestRow()
  235. {
  236. $colRow = $this->getHighestRowAndColumn();
  237. return $colRow['row'];
  238. }
  239. /**
  240. * Generate a unique ID for cache referencing
  241. *
  242. * @return string Unique Reference
  243. */
  244. protected function _getUniqueID() {
  245. if (function_exists('posix_getpid')) {
  246. $baseUnique = posix_getpid();
  247. } else {
  248. $baseUnique = mt_rand();
  249. }
  250. return uniqid($baseUnique,true);
  251. }
  252. /**
  253. * Clone the cell collection
  254. *
  255. * @param PHPExcel_Worksheet $parent The new worksheet
  256. * @return void
  257. */
  258. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  259. $this->_currentCellIsDirty;
  260. $this->_storeData();
  261. $this->_parent = $parent;
  262. if (($this->_currentObject !== NULL) && (is_object($this->_currentObject))) {
  263. $this->_currentObject->attach($this);
  264. }
  265. } // function copyCellCollection()
  266. /**
  267. * Identify whether the caching method is currently available
  268. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  269. *
  270. * @return boolean
  271. */
  272. public static function cacheMethodIsAvailable() {
  273. return true;
  274. }
  275. }