PHPTemp.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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_PHPTemp
  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_PHPTemp extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. /**
  36. * Name of the file for this cache
  37. *
  38. * @var string
  39. */
  40. private $_fileHandle = null;
  41. /**
  42. * Memory limit to use before reverting to file cache
  43. *
  44. * @var integer
  45. */
  46. private $_memoryCacheSize = null;
  47. /**
  48. * Store cell data in cache for the current cell object if it's "dirty",
  49. * and the 'nullify' the current cell object
  50. *
  51. * @return void
  52. * @throws PHPExcel_Exception
  53. */
  54. protected function _storeData() {
  55. if ($this->_currentCellIsDirty && !empty($this->_currentObjectID)) {
  56. $this->_currentObject->detach();
  57. fseek($this->_fileHandle,0,SEEK_END);
  58. $offset = ftell($this->_fileHandle);
  59. fwrite($this->_fileHandle, serialize($this->_currentObject));
  60. $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset,
  61. 'sz' => ftell($this->_fileHandle) - $offset
  62. );
  63. $this->_currentCellIsDirty = false;
  64. }
  65. $this->_currentObjectID = $this->_currentObject = null;
  66. } // function _storeData()
  67. /**
  68. * Add or Update a cell in cache identified by coordinate address
  69. *
  70. * @param string $pCoord Coordinate address of the cell to update
  71. * @param PHPExcel_Cell $cell Cell to update
  72. * @return void
  73. * @throws PHPExcel_Exception
  74. */
  75. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  76. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  77. $this->_storeData();
  78. }
  79. $this->_currentObjectID = $pCoord;
  80. $this->_currentObject = $cell;
  81. $this->_currentCellIsDirty = true;
  82. return $cell;
  83. } // function addCacheData()
  84. /**
  85. * Get cell at a specific coordinate
  86. *
  87. * @param string $pCoord Coordinate of the cell
  88. * @throws PHPExcel_Exception
  89. * @return PHPExcel_Cell Cell that was found, or null if not found
  90. */
  91. public function getCacheData($pCoord) {
  92. if ($pCoord === $this->_currentObjectID) {
  93. return $this->_currentObject;
  94. }
  95. $this->_storeData();
  96. // Check if the entry that has been requested actually exists
  97. if (!isset($this->_cellCache[$pCoord])) {
  98. // Return null if requested entry doesn't exist in cache
  99. return null;
  100. }
  101. // Set current entry to the requested entry
  102. $this->_currentObjectID = $pCoord;
  103. fseek($this->_fileHandle,$this->_cellCache[$pCoord]['ptr']);
  104. $this->_currentObject = unserialize(fread($this->_fileHandle,$this->_cellCache[$pCoord]['sz']));
  105. // Re-attach this as the cell's parent
  106. $this->_currentObject->attach($this);
  107. // Return requested entry
  108. return $this->_currentObject;
  109. } // function getCacheData()
  110. /**
  111. * Get a list of all cell addresses currently held in cache
  112. *
  113. * @return array of string
  114. */
  115. public function getCellList() {
  116. if ($this->_currentObjectID !== null) {
  117. $this->_storeData();
  118. }
  119. return parent::getCellList();
  120. }
  121. /**
  122. * Clone the cell collection
  123. *
  124. * @param PHPExcel_Worksheet $parent The new worksheet
  125. * @return void
  126. */
  127. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  128. parent::copyCellCollection($parent);
  129. // Open a new stream for the cell cache data
  130. $newFileHandle = fopen('php://temp/maxmemory:'.$this->_memoryCacheSize,'a+');
  131. // Copy the existing cell cache data to the new stream
  132. fseek($this->_fileHandle,0);
  133. while (!feof($this->_fileHandle)) {
  134. fwrite($newFileHandle,fread($this->_fileHandle, 1024));
  135. }
  136. $this->_fileHandle = $newFileHandle;
  137. } // function copyCellCollection()
  138. /**
  139. * Clear the cell collection and disconnect from our parent
  140. *
  141. * @return void
  142. */
  143. public function unsetWorksheetCells() {
  144. if(!is_null($this->_currentObject)) {
  145. $this->_currentObject->detach();
  146. $this->_currentObject = $this->_currentObjectID = null;
  147. }
  148. $this->_cellCache = array();
  149. // detach ourself from the worksheet, so that it can then delete this object successfully
  150. $this->_parent = null;
  151. // Close down the php://temp file
  152. $this->__destruct();
  153. } // function unsetWorksheetCells()
  154. /**
  155. * Initialise this new cell collection
  156. *
  157. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  158. * @param array of mixed $arguments Additional initialisation arguments
  159. */
  160. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  161. $this->_memoryCacheSize = (isset($arguments['memoryCacheSize'])) ? $arguments['memoryCacheSize'] : '1MB';
  162. parent::__construct($parent);
  163. if (is_null($this->_fileHandle)) {
  164. $this->_fileHandle = fopen('php://temp/maxmemory:'.$this->_memoryCacheSize,'a+');
  165. }
  166. } // function __construct()
  167. /**
  168. * Destroy this cell collection
  169. */
  170. public function __destruct() {
  171. if (!is_null($this->_fileHandle)) {
  172. fclose($this->_fileHandle);
  173. }
  174. $this->_fileHandle = null;
  175. } // function __destruct()
  176. }