PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/include/PHPExcel/CachedObjectStorage/PHPTemp.php

https://bitbucket.org/sleininger/stock_online
PHP | 192 lines | 72 code | 27 blank | 93 comment | 9 complexity | e22adcc3f0c1a2335045920ac4c344ee MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.7, 2012-05-19
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_PHPTemp
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2012 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 Exception
  53. */
  54. private function _storeData() {
  55. if ($this->_currentCellIsDirty) {
  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 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 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 the parent worksheet
  106. $this->_currentObject->attach($this->_parent);
  107. // Return requested entry
  108. return $this->_currentObject;
  109. } // function getCacheData()
  110. /**
  111. * Clone the cell collection
  112. *
  113. * @param PHPExcel_Worksheet $parent The new worksheet
  114. * @return void
  115. */
  116. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  117. parent::copyCellCollection($parent);
  118. // Open a new stream for the cell cache data
  119. $newFileHandle = fopen('php://temp/maxmemory:'.$this->_memoryCacheSize,'a+');
  120. // Copy the existing cell cache data to the new stream
  121. fseek($this->_fileHandle,0);
  122. while (!feof($this->_fileHandle)) {
  123. fwrite($newFileHandle,fread($this->_fileHandle, 1024));
  124. }
  125. $this->_fileHandle = $newFileHandle;
  126. } // function copyCellCollection()
  127. /**
  128. * Clear the cell collection and disconnect from our parent
  129. *
  130. * @return void
  131. */
  132. public function unsetWorksheetCells() {
  133. if(!is_null($this->_currentObject)) {
  134. $this->_currentObject->detach();
  135. $this->_currentObject = $this->_currentObjectID = null;
  136. }
  137. $this->_cellCache = array();
  138. // detach ourself from the worksheet, so that it can then delete this object successfully
  139. $this->_parent = null;
  140. // Close down the php://temp file
  141. $this->__destruct();
  142. } // function unsetWorksheetCells()
  143. /**
  144. * Initialise this new cell collection
  145. *
  146. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  147. * @param array of mixed $arguments Additional initialisation arguments
  148. */
  149. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  150. $this->_memoryCacheSize = (isset($arguments['memoryCacheSize'])) ? $arguments['memoryCacheSize'] : '1MB';
  151. parent::__construct($parent);
  152. if (is_null($this->_fileHandle)) {
  153. $this->_fileHandle = fopen('php://temp/maxmemory:'.$this->_memoryCacheSize,'a+');
  154. }
  155. } // function __construct()
  156. /**
  157. * Destroy this cell collection
  158. */
  159. public function __destruct() {
  160. if (!is_null($this->_fileHandle)) {
  161. fclose($this->_fileHandle);
  162. }
  163. $this->_fileHandle = null;
  164. } // function __destruct()
  165. }