PHPTemp.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * PHPExcel_CachedObjectStorage_PHPTemp
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 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. class PHPExcel_CachedObjectStorage_PHPTemp extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
  28. {
  29. /**
  30. * Name of the file for this cache
  31. *
  32. * @var string
  33. */
  34. private $fileHandle = null;
  35. /**
  36. * Memory limit to use before reverting to file cache
  37. *
  38. * @var integer
  39. */
  40. private $memoryCacheSize = null;
  41. /**
  42. * Store cell data in cache for the current cell object if it's "dirty",
  43. * and the 'nullify' the current cell object
  44. *
  45. * @return void
  46. * @throws PHPExcel_Exception
  47. */
  48. protected function storeData()
  49. {
  50. if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  51. $this->currentObject->detach();
  52. fseek($this->fileHandle, 0, SEEK_END);
  53. $this->cellCache[$this->currentObjectID] = array(
  54. 'ptr' => ftell($this->fileHandle),
  55. 'sz' => fwrite($this->fileHandle, serialize($this->currentObject))
  56. );
  57. $this->currentCellIsDirty = false;
  58. }
  59. $this->currentObjectID = $this->currentObject = null;
  60. }
  61. /**
  62. * Add or Update a cell in cache identified by coordinate address
  63. *
  64. * @param string $pCoord Coordinate address of the cell to update
  65. * @param PHPExcel_Cell $cell Cell to update
  66. * @return PHPExcel_Cell
  67. * @throws PHPExcel_Exception
  68. */
  69. public function addCacheData($pCoord, PHPExcel_Cell $cell)
  70. {
  71. if (($pCoord !== $this->currentObjectID) && ($this->currentObjectID !== null)) {
  72. $this->storeData();
  73. }
  74. $this->currentObjectID = $pCoord;
  75. $this->currentObject = $cell;
  76. $this->currentCellIsDirty = true;
  77. return $cell;
  78. }
  79. /**
  80. * Get cell at a specific coordinate
  81. *
  82. * @param string $pCoord Coordinate of the cell
  83. * @throws PHPExcel_Exception
  84. * @return PHPExcel_Cell Cell that was found, or null if not found
  85. */
  86. public function getCacheData($pCoord)
  87. {
  88. if ($pCoord === $this->currentObjectID) {
  89. return $this->currentObject;
  90. }
  91. $this->storeData();
  92. // Check if the entry that has been requested actually exists
  93. if (!isset($this->cellCache[$pCoord])) {
  94. // Return null if requested entry doesn't exist in cache
  95. return null;
  96. }
  97. // Set current entry to the requested entry
  98. $this->currentObjectID = $pCoord;
  99. fseek($this->fileHandle, $this->cellCache[$pCoord]['ptr']);
  100. $this->currentObject = unserialize(fread($this->fileHandle, $this->cellCache[$pCoord]['sz']));
  101. // Re-attach this as the cell's parent
  102. $this->currentObject->attach($this);
  103. // Return requested entry
  104. return $this->currentObject;
  105. }
  106. /**
  107. * Get a list of all cell addresses currently held in cache
  108. *
  109. * @return string[]
  110. */
  111. public function getCellList()
  112. {
  113. if ($this->currentObjectID !== null) {
  114. $this->storeData();
  115. }
  116. return parent::getCellList();
  117. }
  118. /**
  119. * Clone the cell collection
  120. *
  121. * @param PHPExcel_Worksheet $parent The new worksheet
  122. * @return void
  123. */
  124. public function copyCellCollection(PHPExcel_Worksheet $parent)
  125. {
  126. parent::copyCellCollection($parent);
  127. // Open a new stream for the cell cache data
  128. $newFileHandle = fopen('php://temp/maxmemory:' . $this->memoryCacheSize, 'a+');
  129. // Copy the existing cell cache data to the new stream
  130. fseek($this->fileHandle, 0);
  131. while (!feof($this->fileHandle)) {
  132. fwrite($newFileHandle, fread($this->fileHandle, 1024));
  133. }
  134. $this->fileHandle = $newFileHandle;
  135. }
  136. /**
  137. * Clear the cell collection and disconnect from our parent
  138. *
  139. * @return void
  140. */
  141. public function unsetWorksheetCells()
  142. {
  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. }
  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. {
  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. }
  167. /**
  168. * Destroy this cell collection
  169. */
  170. public function __destruct()
  171. {
  172. if (!is_null($this->fileHandle)) {
  173. fclose($this->fileHandle);
  174. }
  175. $this->fileHandle = null;
  176. }
  177. }