CacheBase.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_CacheBase
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. 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. * An array of cells or cell pointers for the worksheet cells held in this cache,
  55. * and indexed by their coordinate address within the worksheet
  56. *
  57. * @var array of mixed
  58. */
  59. protected $_cellCache = array();
  60. public function __construct(PHPExcel_Worksheet $parent) {
  61. // Set our parent worksheet.
  62. // This is maintained within the cache controller to facilitate re-attaching it to PHPExcel_Cell objects when
  63. // they are woken from a serialized state
  64. $this->_parent = $parent;
  65. } // function __construct()
  66. /**
  67. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  68. *
  69. * @param string $pCoord Coordinate address of the cell to check
  70. * @return void
  71. * @return boolean
  72. */
  73. public function isDataSet($pCoord) {
  74. if ($pCoord === $this->_currentObjectID) {
  75. return true;
  76. }
  77. // Check if the requested entry exists in the cache
  78. return isset($this->_cellCache[$pCoord]);
  79. } // function isDataSet()
  80. /**
  81. * Add or Update a cell in cache
  82. *
  83. * @param PHPExcel_Cell $cell Cell to update
  84. * @return void
  85. * @throws Exception
  86. */
  87. public function updateCacheData(PHPExcel_Cell $cell) {
  88. return $this->addCacheData($cell->getCoordinate(),$cell);
  89. } // function updateCacheData()
  90. /**
  91. * Delete a cell in cache identified by coordinate address
  92. *
  93. * @param string $pCoord Coordinate address of the cell to delete
  94. * @throws Exception
  95. */
  96. public function deleteCacheData($pCoord) {
  97. if ($pCoord === $this->_currentObjectID) {
  98. $this->_currentObject->detach();
  99. $this->_currentObjectID = $this->_currentObject = null;
  100. }
  101. if (is_object($this->_cellCache[$pCoord])) {
  102. $this->_cellCache[$pCoord]->detach();
  103. unset($this->_cellCache[$pCoord]);
  104. }
  105. } // function deleteCacheData()
  106. /**
  107. * Get a list of all cell addresses currently held in cache
  108. *
  109. * @return array of string
  110. */
  111. public function getCellList() {
  112. return array_keys($this->_cellCache);
  113. } // function getCellList()
  114. /**
  115. * Sort the list of all cell addresses currently held in cache by row and column
  116. *
  117. * @return void
  118. */
  119. public function getSortedCellList() {
  120. $sortKeys = array();
  121. foreach (array_keys($this->_cellCache) as $coord) {
  122. list($column,$row) = sscanf($coord,'%[A-Z]%d');
  123. $sortKeys[sprintf('%09d%3s',$row,$column)] = $coord;
  124. }
  125. ksort($sortKeys);
  126. return array_values($sortKeys);
  127. } // function sortCellList()
  128. protected function _getUniqueID() {
  129. if (function_exists('posix_getpid')) {
  130. $baseUnique = posix_getpid();
  131. } else {
  132. $baseUnique = mt_rand();
  133. }
  134. return uniqid($baseUnique,true);
  135. }
  136. /**
  137. * Clone the cell collection
  138. *
  139. * @return void
  140. */
  141. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  142. $this->_parent = $parent;
  143. if ((!is_null($this->_currentObject)) && (is_object($this->_currentObject))) {
  144. $this->_currentObject->attach($parent);
  145. }
  146. } // function copyCellCollection()
  147. }