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

/vendor/phpoffice/phpexcel/Classes/PHPExcel/CachedObjectStorage/PHPTemp.php

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