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