File Source for PHPTemp.php

PHPExcel_CachedObjectStorage

# Source for file PHPTemp.php

Documentation is available at PHPTemp.php

1. <?php
2. /**
3. * PHPExcel
4. *
5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25. * @version 1.7.6, 2011-02-27
26. */
29. /**
30. * PHPExcel_CachedObjectStorage_PHPTemp
31. *
32. * @category PHPExcel
33. * @package PHPExcel_CachedObjectStorage
34. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
35. */
36. class PHPExcel_CachedObjectStorage_PHPTemp extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
38. private $_fileHandle = null;
41. private $_memoryCacheSize = null;
43. private function _storeData() {
44. $this->_currentObject->detach();
46. fseek($this->_fileHandle,0,SEEK_END);
47. $offset = ftell($this->_fileHandle);
48. fwrite($this->_fileHandle, serialize($this->_currentObject));
49. $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset,
50. 'sz' => ftell($this->_fileHandle) - $offset
51. );
52. $this->_currentObjectID = $this->_currentObject = null;
53. } // function _storeData()
56. /**
57. * Add or Update a cell in cache identified by coordinate address
58. *
59. * @param string $pCoord Coordinate address of the cell to update
60. * @param PHPExcel_Cell $cell Cell to update
61. * @return void
62. * @throws Exception
63. */
64. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
65. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
66. $this->_storeData();
67. }
69. $this->_currentObjectID = $pCoord;
70. $this->_currentObject = $cell;
72. return $cell;
73. } // function addCacheData()
76. /**
77. * Get cell at a specific coordinate
78. *
79. * @param string $pCoord Coordinate of the cell
80. * @throws Exception
81. * @return PHPExcel_Cell Cell that was found, or null if not found
82. */
83. public function getCacheData($pCoord) {
84. if ($pCoord === $this->_currentObjectID) {
85. return $this->_currentObject;
86. }
87. $this->_storeData();
89. // Check if the entry that has been requested actually exists
90. if (!isset($this->_cellCache[$pCoord])) {
91. // Return null if requested entry doesn't exist in cache
92. return null;
93. }
95. // Set current entry to the requested entry
96. $this->_currentObjectID = $pCoord;
97. fseek($this->_fileHandle,$this->_cellCache[$pCoord]['ptr']);
98. $this->_currentObject = unserialize( fread($this->_fileHandle,$this->_cellCache[$pCoord]['sz']));
99. // Re-attach the parent worksheet
100. $this->_currentObject->attach($this->_parent);
102. // Return requested entry
103. return $this->_currentObject;
104. } // function getCacheData()
107. /**
108. * Clone the cell collection
109. *
110. * @return void
111. */
112. public function copyCellCollection(PHPExcel_Worksheet $parent) {
113. parent:: copyCellCollection($parent);
114. // Open a new stream for the cell cache data
115. $newFileHandle = fopen('php://temp/maxmemory:'.$this->_memoryCacheSize,'a+');
116. // Copy the existing cell cache data to the new stream
117. fseek($this->_fileHandle,0);
118. while (! feof($this->_fileHandle)) {
119. fwrite($newFileHandle, fread($this->_fileHandle, 1024));
120. }
121. $this->_fileHandle = $newFileHandle;
122. } // function copyCellCollection()
125. public function unsetWorksheetCells() {
126. if(! is_null($this->_currentObject)) {
127. $this->_currentObject->detach();
128. $this->_currentObject = $this->_currentObjectID = null;
129. }
130. $this->_cellCache = array();
132. // detach ourself from the worksheet, so that it can then delete this object successfully
133. $this->_parent = null;
135. // Close down the php://temp file
136. $this->__destruct();
137. } // function unsetWorksheetCells()
140. public function __construct(PHPExcel_Worksheet $parent, $memoryCacheSize = '1MB') {
141. $this->_memoryCacheSize = (isset($arguments['memoryCacheSize'])) ? $arguments['memoryCacheSize'] : '1MB';
143. parent::__construct($parent);
144. if ( is_null($this->_fileHandle)) {
145. $this->_fileHandle = fopen('php://temp/maxmemory:'.$this->_memoryCacheSize,'a+');
146. }
147. } // function __construct()
150. public function __destruct() {
151. if (! is_null($this->_fileHandle)) {
152. fclose($this->_fileHandle);
153. }
154. $this->_fileHandle = null;
155. } // function __destruct()
157. }

Documentation generated on Sun, 27 Feb 2011 16:33:27 -0800 by phpDocumentor 1.4.3