e0f508fb89efc94f7673ac8cf355852a5ee4bcd5.svn-base 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_DiscISAM
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorage_DiscISAM extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. /**
  36. * Name of the file for this cache
  37. *
  38. * @var string
  39. */
  40. private $_fileName = NULL;
  41. /**
  42. * File handle for this cache file
  43. *
  44. * @var resource
  45. */
  46. private $_fileHandle = NULL;
  47. /**
  48. * Directory/Folder where the cache file is located
  49. *
  50. * @var string
  51. */
  52. private $_cacheDirectory = NULL;
  53. /**
  54. * Store cell data in cache for the current cell object if it's "dirty",
  55. * and the 'nullify' the current cell object
  56. *
  57. * @return void
  58. * @throws PHPExcel_Exception
  59. */
  60. protected function _storeData() {
  61. if ($this->_currentCellIsDirty && !empty($this->_currentObjectID)) {
  62. $this->_currentObject->detach();
  63. fseek($this->_fileHandle,0,SEEK_END);
  64. $this->_cellCache[$this->_currentObjectID] = array(
  65. 'ptr' => ftell($this->_fileHandle),
  66. 'sz' => fwrite($this->_fileHandle, serialize($this->_currentObject))
  67. );
  68. $this->_currentCellIsDirty = false;
  69. }
  70. $this->_currentObjectID = $this->_currentObject = null;
  71. } // function _storeData()
  72. /**
  73. * Add or Update a cell in cache identified by coordinate address
  74. *
  75. * @param string $pCoord Coordinate address of the cell to update
  76. * @param PHPExcel_Cell $cell Cell to update
  77. * @return PHPExcel_Cell
  78. * @throws PHPExcel_Exception
  79. */
  80. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  81. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  82. $this->_storeData();
  83. }
  84. $this->_currentObjectID = $pCoord;
  85. $this->_currentObject = $cell;
  86. $this->_currentCellIsDirty = true;
  87. return $cell;
  88. } // function addCacheData()
  89. /**
  90. * Get cell at a specific coordinate
  91. *
  92. * @param string $pCoord Coordinate of the cell
  93. * @throws PHPExcel_Exception
  94. * @return PHPExcel_Cell Cell that was found, or null if not found
  95. */
  96. public function getCacheData($pCoord) {
  97. if ($pCoord === $this->_currentObjectID) {
  98. return $this->_currentObject;
  99. }
  100. $this->_storeData();
  101. // Check if the entry that has been requested actually exists
  102. if (!isset($this->_cellCache[$pCoord])) {
  103. // Return null if requested entry doesn't exist in cache
  104. return null;
  105. }
  106. // Set current entry to the requested entry
  107. $this->_currentObjectID = $pCoord;
  108. fseek($this->_fileHandle, $this->_cellCache[$pCoord]['ptr']);
  109. $this->_currentObject = unserialize(fread($this->_fileHandle, $this->_cellCache[$pCoord]['sz']));
  110. // Re-attach this as the cell's parent
  111. $this->_currentObject->attach($this);
  112. // Return requested entry
  113. return $this->_currentObject;
  114. } // function getCacheData()
  115. /**
  116. * Get a list of all cell addresses currently held in cache
  117. *
  118. * @return string[]
  119. */
  120. public function getCellList() {
  121. if ($this->_currentObjectID !== null) {
  122. $this->_storeData();
  123. }
  124. return parent::getCellList();
  125. }
  126. /**
  127. * Clone the cell collection
  128. *
  129. * @param PHPExcel_Worksheet $parent The new worksheet
  130. * @return void
  131. */
  132. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  133. parent::copyCellCollection($parent);
  134. // Get a new id for the new file name
  135. $baseUnique = $this->_getUniqueID();
  136. $newFileName = $this->_cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache';
  137. // Copy the existing cell cache file
  138. copy ($this->_fileName,$newFileName);
  139. $this->_fileName = $newFileName;
  140. // Open the copied cell cache file
  141. $this->_fileHandle = fopen($this->_fileName,'a+');
  142. } // function copyCellCollection()
  143. /**
  144. * Clear the cell collection and disconnect from our parent
  145. *
  146. * @return void
  147. */
  148. public function unsetWorksheetCells() {
  149. if(!is_null($this->_currentObject)) {
  150. $this->_currentObject->detach();
  151. $this->_currentObject = $this->_currentObjectID = null;
  152. }
  153. $this->_cellCache = array();
  154. // detach ourself from the worksheet, so that it can then delete this object successfully
  155. $this->_parent = null;
  156. // Close down the temporary cache file
  157. $this->__destruct();
  158. } // function unsetWorksheetCells()
  159. /**
  160. * Initialise this new cell collection
  161. *
  162. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  163. * @param array of mixed $arguments Additional initialisation arguments
  164. */
  165. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  166. $this->_cacheDirectory = ((isset($arguments['dir'])) && ($arguments['dir'] !== NULL))
  167. ? $arguments['dir']
  168. : PHPExcel_Shared_File::sys_get_temp_dir();
  169. parent::__construct($parent);
  170. if (is_null($this->_fileHandle)) {
  171. $baseUnique = $this->_getUniqueID();
  172. $this->_fileName = $this->_cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache';
  173. $this->_fileHandle = fopen($this->_fileName,'a+');
  174. }
  175. } // function __construct()
  176. /**
  177. * Destroy this cell collection
  178. */
  179. public function __destruct() {
  180. if (!is_null($this->_fileHandle)) {
  181. fclose($this->_fileHandle);
  182. unlink($this->_fileName);
  183. }
  184. $this->_fileHandle = null;
  185. } // function __destruct()
  186. }