PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/PHPExcel/CachedObjectStorage/DiscISAM.php

https://github.com/iGroup/PHPExcel
PHP | 205 lines | 77 code | 29 blank | 99 comment | 9 complexity | bf7601ec0049a7e611e3c839b7c9e636 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 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 - 2012 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 Exception
  59. */
  60. private function _storeData() {
  61. if ($this->_currentCellIsDirty) {
  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 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 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 the parent worksheet
  112. $this->_currentObject->attach($this->_parent);
  113. // Return requested entry
  114. return $this->_currentObject;
  115. } // function getCacheData()
  116. /**
  117. * Clone the cell collection
  118. *
  119. * @param PHPExcel_Worksheet $parent The new worksheet
  120. * @return void
  121. */
  122. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  123. parent::copyCellCollection($parent);
  124. // Get a new id for the new file name
  125. $baseUnique = $this->_getUniqueID();
  126. $newFileName = $this->_cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache';
  127. // Copy the existing cell cache file
  128. copy ($this->_fileName,$newFileName);
  129. $this->_fileName = $newFileName;
  130. // Open the copied cell cache file
  131. $this->_fileHandle = fopen($this->_fileName,'a+');
  132. } // function copyCellCollection()
  133. /**
  134. * Clear the cell collection and disconnect from our parent
  135. *
  136. * @return void
  137. */
  138. public function unsetWorksheetCells() {
  139. if(!is_null($this->_currentObject)) {
  140. $this->_currentObject->detach();
  141. $this->_currentObject = $this->_currentObjectID = null;
  142. }
  143. $this->_cellCache = array();
  144. // detach ourself from the worksheet, so that it can then delete this object successfully
  145. $this->_parent = null;
  146. // Close down the temporary cache file
  147. $this->__destruct();
  148. } // function unsetWorksheetCells()
  149. /**
  150. * Initialise this new cell collection
  151. *
  152. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  153. * @param array of mixed $arguments Additional initialisation arguments
  154. */
  155. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  156. $this->_cacheDirectory = ((isset($arguments['dir'])) && ($arguments['dir'] !== NULL))
  157. ? $arguments['dir']
  158. : PHPExcel_Shared_File::sys_get_temp_dir();
  159. parent::__construct($parent);
  160. if (is_null($this->_fileHandle)) {
  161. $baseUnique = $this->_getUniqueID();
  162. $this->_fileName = $this->_cacheDirectory.'/PHPExcel.'.$baseUnique.'.cache';
  163. $this->_fileHandle = fopen($this->_fileName,'a+');
  164. }
  165. } // function __construct()
  166. /**
  167. * Destroy this cell collection
  168. */
  169. public function __destruct() {
  170. if (!is_null($this->_fileHandle)) {
  171. fclose($this->_fileHandle);
  172. unlink($this->_fileName);
  173. }
  174. $this->_fileHandle = null;
  175. } // function __destruct()
  176. }