File Source for DiscISAM.php

PHPExcel_CachedObjectStorage

# Source for file DiscISAM.php

Documentation is available at DiscISAM.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_DiscISAM
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_DiscISAM extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
38. private $_fileName = null;
39. private $_fileHandle = null;
42. private function _storeData() {
43. $this->_currentObject->detach();
45. fseek($this->_fileHandle,0,SEEK_END);
46. $offset = ftell($this->_fileHandle);
47. fwrite($this->_fileHandle, serialize($this->_currentObject));
48. $this->_cellCache[$this->_currentObjectID] = array('ptr' => $offset,
49. 'sz' => ftell($this->_fileHandle) - $offset
50. );
51. $this->_currentObjectID = $this->_currentObject = null;
52. } // function _storeData()
55. /**
56. * Add or Update a cell in cache identified by coordinate address
57. *
58. * @param string $pCoord Coordinate address of the cell to update
59. * @param PHPExcel_Cell $cell Cell to update
60. * @return void
61. * @throws Exception
62. */
63. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
64. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
65. $this->_storeData();
66. }
68. $this->_currentObjectID = $pCoord;
69. $this->_currentObject = $cell;
71. return $cell;
72. } // function addCacheData()
75. /**
76. * Get cell at a specific coordinate
77. *
78. * @param string $pCoord Coordinate of the cell
79. * @throws Exception
80. * @return PHPExcel_Cell Cell that was found, or null if not found
81. */
82. public function getCacheData($pCoord) {
83. if ($pCoord === $this->_currentObjectID) {
84. return $this->_currentObject;
85. }
86. $this->_storeData();
88. // Check if the entry that has been requested actually exists
89. if (!isset($this->_cellCache[$pCoord])) {
90. // Return null if requested entry doesn't exist in cache
91. return null;
92. }
94. // Set current entry to the requested entry
95. $this->_currentObjectID = $pCoord;
96. fseek($this->_fileHandle,$this->_cellCache[$pCoord]['ptr']);
97. $this->_currentObject = unserialize( fread($this->_fileHandle,$this->_cellCache[$pCoord]['sz']));
98. // Re-attach the parent worksheet
99. $this->_currentObject->attach($this->_parent);
101. // Return requested entry
102. return $this->_currentObject;
103. } // function getCacheData()
106. /**
107. * Clone the cell collection
108. *
109. * @return void
110. */
111. public function copyCellCollection(PHPExcel_Worksheet $parent) {
112. parent:: copyCellCollection($parent);
113. // Get a new id for the new file name
114. $baseUnique = $this->_getUniqueID();
115. $newFileName = PHPExcel_Shared_File:: sys_get_temp_dir().'/PHPExcel.'.$baseUnique.'.cache';
116. // Copy the existing cell cache file
117. copy ($this->_fileName,$newFileName);
118. $this->_fileName = $newFileName;
119. // Open the copied cell cache file
120. $this->_fileHandle = fopen($this->_fileName,'a+');
121. } // function copyCellCollection()
124. public function unsetWorksheetCells() {
125. if(! is_null($this->_currentObject)) {
126. $this->_currentObject->detach();
127. $this->_currentObject = $this->_currentObjectID = null;
128. }
129. $this->_cellCache = array();
131. // detach ourself from the worksheet, so that it can then delete this object successfully
132. $this->_parent = null;
134. // Close down the temporary cache file
135. $this->__destruct();
136. } // function unsetWorksheetCells()
139. public function __construct(PHPExcel_Worksheet $parent) {
140. parent::__construct($parent);
141. if ( is_null($this->_fileHandle)) {
142. $baseUnique = $this->_getUniqueID();
143. $this->_fileName = PHPExcel_Shared_File:: sys_get_temp_dir().'/PHPExcel.'.$baseUnique.'.cache';
144. $this->_fileHandle = fopen($this->_fileName,'a+');
145. }
146. } // function __construct()
149. public function __destruct() {
150. if (! is_null($this->_fileHandle)) {
151. fclose($this->_fileHandle);
152. unlink($this->_fileName);
153. }
154. $this->_fileHandle = null;
155. } // function __destruct()
157. }

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