DiscISAM.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_DiscISAM
  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_DiscISAM extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. private $_fileName = null;
  36. private $_fileHandle = null;
  37. private function _storeData() {
  38. $this->_currentObject->detach();
  39. fseek($this->_fileHandle,0,SEEK_END);
  40. $offset = ftell($this->_fileHandle);
  41. fwrite($this->_fileHandle, serialize($this->_currentObject));
  42. $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset,
  43. 'sz' => ftell($this->_fileHandle) - $offset
  44. );
  45. $this->_currentObjectID = $this->_currentObject = null;
  46. } // function _storeData()
  47. /**
  48. * Add or Update a cell in cache identified by coordinate address
  49. *
  50. * @param string $pCoord Coordinate address of the cell to update
  51. * @param PHPExcel_Cell $cell Cell to update
  52. * @return void
  53. * @throws Exception
  54. */
  55. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  56. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  57. $this->_storeData();
  58. }
  59. $this->_currentObjectID = $pCoord;
  60. $this->_currentObject = $cell;
  61. return $cell;
  62. } // function addCacheData()
  63. /**
  64. * Get cell at a specific coordinate
  65. *
  66. * @param string $pCoord Coordinate of the cell
  67. * @throws Exception
  68. * @return PHPExcel_Cell Cell that was found, or null if not found
  69. */
  70. public function getCacheData($pCoord) {
  71. if ($pCoord === $this->_currentObjectID) {
  72. return $this->_currentObject;
  73. }
  74. $this->_storeData();
  75. // Check if the entry that has been requested actually exists
  76. if (!isset($this->_cellCache[$pCoord])) {
  77. // Return null if requested entry doesn't exist in cache
  78. return null;
  79. }
  80. // Set current entry to the requested entry
  81. $this->_currentObjectID = $pCoord;
  82. fseek($this->_fileHandle,$this->_cellCache[$pCoord]['ptr']);
  83. $this->_currentObject = unserialize(fread($this->_fileHandle,$this->_cellCache[$pCoord]['sz']));
  84. // Re-attach the parent worksheet
  85. $this->_currentObject->attach($this->_parent);
  86. // Return requested entry
  87. return $this->_currentObject;
  88. } // function getCacheData()
  89. /**
  90. * Clone the cell collection
  91. *
  92. * @return void
  93. */
  94. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  95. parent::copyCellCollection($parent);
  96. // Get a new id for the new file name
  97. $baseUnique = $this->_getUniqueID();
  98. $newFileName = PHPExcel_Shared_File::sys_get_temp_dir().'/PHPExcel.'.$baseUnique.'.cache';
  99. // Copy the existing cell cache file
  100. copy ($this->_fileName,$newFileName);
  101. $this->_fileName = $newFileName;
  102. // Open the copied cell cache file
  103. $this->_fileHandle = fopen($this->_fileName,'a+');
  104. } // function copyCellCollection()
  105. public function unsetWorksheetCells() {
  106. if(!is_null($this->_currentObject)) {
  107. $this->_currentObject->detach();
  108. $this->_currentObject = $this->_currentObjectID = null;
  109. }
  110. $this->_cellCache = array();
  111. // detach ourself from the worksheet, so that it can then delete this object successfully
  112. $this->_parent = null;
  113. // Close down the temporary cache file
  114. $this->__destruct();
  115. } // function unsetWorksheetCells()
  116. public function __construct(PHPExcel_Worksheet $parent) {
  117. parent::__construct($parent);
  118. if (is_null($this->_fileHandle)) {
  119. $baseUnique = $this->_getUniqueID();
  120. $this->_fileName = PHPExcel_Shared_File::sys_get_temp_dir().'/PHPExcel.'.$baseUnique.'.cache';
  121. $this->_fileHandle = fopen($this->_fileName,'a+');
  122. }
  123. } // function __construct()
  124. public function __destruct() {
  125. if (!is_null($this->_fileHandle)) {
  126. fclose($this->_fileHandle);
  127. unlink($this->_fileName);
  128. }
  129. $this->_fileHandle = null;
  130. } // function __destruct()
  131. }