DiscISAM.php 6.3 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 1.8.0, 2014-03-02
  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. $offset = ftell($this->_fileHandle);
  65. fwrite($this->_fileHandle, serialize($this->_currentObject));
  66. $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset,
  67. 'sz' => ftell($this->_fileHandle) - $offset
  68. );
  69. $this->_currentCellIsDirty = false;
  70. }
  71. $this->_currentObjectID = $this->_currentObject = null;
  72. } // function _storeData()
  73. /**
  74. * Add or Update a cell in cache identified by coordinate address
  75. *
  76. * @param string $pCoord Coordinate address of the cell to update
  77. * @param PHPExcel_Cell $cell Cell to update
  78. * @return void
  79. * @throws PHPExcel_Exception
  80. */
  81. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  82. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  83. $this->_storeData();
  84. }
  85. $this->_currentObjectID = $pCoord;
  86. $this->_currentObject = $cell;
  87. $this->_currentCellIsDirty = true;
  88. return $cell;
  89. } // function addCacheData()
  90. /**
  91. * Get cell at a specific coordinate
  92. *
  93. * @param string $pCoord Coordinate of the cell
  94. * @throws PHPExcel_Exception
  95. * @return PHPExcel_Cell Cell that was found, or null if not found
  96. */
  97. public function getCacheData($pCoord) {
  98. if ($pCoord === $this->_currentObjectID) {
  99. return $this->_currentObject;
  100. }
  101. $this->_storeData();
  102. // Check if the entry that has been requested actually exists
  103. if (!isset($this->_cellCache[$pCoord])) {
  104. // Return null if requested entry doesn't exist in cache
  105. return null;
  106. }
  107. // Set current entry to the requested entry
  108. $this->_currentObjectID = $pCoord;
  109. fseek($this->_fileHandle,$this->_cellCache[$pCoord]['ptr']);
  110. $this->_currentObject = unserialize(fread($this->_fileHandle,$this->_cellCache[$pCoord]['sz']));
  111. // Re-attach this as the cell's parent
  112. $this->_currentObject->attach($this);
  113. // Return requested entry
  114. return $this->_currentObject;
  115. } // function getCacheData()
  116. /**
  117. * Get a list of all cell addresses currently held in cache
  118. *
  119. * @return array of string
  120. */
  121. public function getCellList() {
  122. if ($this->_currentObjectID !== null) {
  123. $this->_storeData();
  124. }
  125. return parent::getCellList();
  126. }
  127. /**
  128. * Clone the cell collection
  129. *
  130. * @param PHPExcel_Worksheet $parent The new worksheet
  131. * @return void
  132. */
  133. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  134. parent::copyCellCollection($parent);
  135. // Get a new id for the new file name
  136. $baseUnique = $this->_getUniqueID();
  137. $newFileName = $this->_cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache';
  138. // Copy the existing cell cache file
  139. copy ($this->_fileName,$newFileName);
  140. $this->_fileName = $newFileName;
  141. // Open the copied cell cache file
  142. $this->_fileHandle = fopen($this->_fileName,'a+');
  143. } // function copyCellCollection()
  144. /**
  145. * Clear the cell collection and disconnect from our parent
  146. *
  147. * @return void
  148. */
  149. public function unsetWorksheetCells() {
  150. if(!is_null($this->_currentObject)) {
  151. $this->_currentObject->detach();
  152. $this->_currentObject = $this->_currentObjectID = null;
  153. }
  154. $this->_cellCache = array();
  155. // detach ourself from the worksheet, so that it can then delete this object successfully
  156. $this->_parent = null;
  157. // Close down the temporary cache file
  158. $this->__destruct();
  159. } // function unsetWorksheetCells()
  160. /**
  161. * Initialise this new cell collection
  162. *
  163. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  164. * @param array of mixed $arguments Additional initialisation arguments
  165. */
  166. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  167. $this->_cacheDirectory = ((isset($arguments['dir'])) && ($arguments['dir'] !== NULL))
  168. ? $arguments['dir']
  169. : PHPExcel_Shared_File::sys_get_temp_dir();
  170. parent::__construct($parent);
  171. if (is_null($this->_fileHandle)) {
  172. $baseUnique = $this->_getUniqueID();
  173. $this->_fileName = $this->_cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache';
  174. $this->_fileHandle = fopen($this->_fileName,'a+');
  175. }
  176. } // function __construct()
  177. /**
  178. * Destroy this cell collection
  179. */
  180. public function __destruct() {
  181. if (!is_null($this->_fileHandle)) {
  182. fclose($this->_fileHandle);
  183. unlink($this->_fileName);
  184. }
  185. $this->_fileHandle = null;
  186. } // function __destruct()
  187. }