No Description

PHPTemp.php 6.0KB

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