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

/PHPExcel_1.7.8-with_documentation-msoffice_format/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/CachedObjectStorage/Wincache.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 280 lines | 124 code | 39 blank | 117 comment | 22 complexity | 0662f69ddc43bc8d8a4368264bfa446f MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  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.8, 2012-10-12
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_Wincache
  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_Wincache extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. /**
  36. * Prefix used to uniquely identify cache data for this worksheet
  37. *
  38. * @var string
  39. */
  40. private $_cachePrefix = null;
  41. /**
  42. * Cache timeout
  43. *
  44. * @var integer
  45. */
  46. private $_cacheTime = 600;
  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. $obj = serialize($this->_currentObject);
  58. if (wincache_ucache_exists($this->_cachePrefix.$this->_currentObjectID.'.cache')) {
  59. if (!wincache_ucache_set($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime)) {
  60. $this->__destruct();
  61. throw new Exception('Failed to store cell '.$this->_currentObjectID.' in WinCache');
  62. }
  63. } else {
  64. if (!wincache_ucache_add($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime)) {
  65. $this->__destruct();
  66. throw new Exception('Failed to store cell '.$this->_currentObjectID.' in WinCache');
  67. }
  68. }
  69. $this->_currentCellIsDirty = false;
  70. }
  71. $this->_currentObjectID = $this->_currentObject = null;
  72. } // function _storeData()
  73. /**
  74. * Add or Update a cell in cache identified by coordinate address
  75. *
  76. * @param string $pCoord Coordinate address of the cell to update
  77. * @param PHPExcel_Cell $cell Cell to update
  78. * @return void
  79. * @throws Exception
  80. */
  81. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  82. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  83. $this->_storeData();
  84. }
  85. $this->_cellCache[$pCoord] = true;
  86. $this->_currentObjectID = $pCoord;
  87. $this->_currentObject = $cell;
  88. $this->_currentCellIsDirty = true;
  89. return $cell;
  90. } // function addCacheData()
  91. /**
  92. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  93. *
  94. * @param string $pCoord Coordinate address of the cell to check
  95. * @return boolean
  96. */
  97. public function isDataSet($pCoord) {
  98. // Check if the requested entry is the current object, or exists in the cache
  99. if (parent::isDataSet($pCoord)) {
  100. if ($this->_currentObjectID == $pCoord) {
  101. return true;
  102. }
  103. // Check if the requested entry still exists in cache
  104. $success = wincache_ucache_exists($this->_cachePrefix.$pCoord.'.cache');
  105. if ($success === false) {
  106. // Entry no longer exists in Wincache, so clear it from the cache array
  107. parent::deleteCacheData($pCoord);
  108. throw new Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
  109. }
  110. return true;
  111. }
  112. return false;
  113. } // function isDataSet()
  114. /**
  115. * Get cell at a specific coordinate
  116. *
  117. * @param string $pCoord Coordinate of the cell
  118. * @throws Exception
  119. * @return PHPExcel_Cell Cell that was found, or null if not found
  120. */
  121. public function getCacheData($pCoord) {
  122. if ($pCoord === $this->_currentObjectID) {
  123. return $this->_currentObject;
  124. }
  125. $this->_storeData();
  126. // Check if the entry that has been requested actually exists
  127. $obj = null;
  128. if (parent::isDataSet($pCoord)) {
  129. $success = false;
  130. $obj = wincache_ucache_get($this->_cachePrefix.$pCoord.'.cache', $success);
  131. if ($success === false) {
  132. // Entry no longer exists in WinCache, so clear it from the cache array
  133. parent::deleteCacheData($pCoord);
  134. throw new Exception('Cell entry '.$pCoord.' no longer exists in WinCache');
  135. }
  136. } else {
  137. // Return null if requested entry doesn't exist in cache
  138. return null;
  139. }
  140. // Set current entry to the requested entry
  141. $this->_currentObjectID = $pCoord;
  142. $this->_currentObject = unserialize($obj);
  143. // Re-attach the parent worksheet
  144. $this->_currentObject->attach($this->_parent);
  145. // Return requested entry
  146. return $this->_currentObject;
  147. } // function getCacheData()
  148. /**
  149. * Delete a cell in cache identified by coordinate address
  150. *
  151. * @param string $pCoord Coordinate address of the cell to delete
  152. * @throws Exception
  153. */
  154. public function deleteCacheData($pCoord) {
  155. // Delete the entry from Wincache
  156. wincache_ucache_delete($this->_cachePrefix.$pCoord.'.cache');
  157. // Delete the entry from our cell address array
  158. parent::deleteCacheData($pCoord);
  159. } // function deleteCacheData()
  160. /**
  161. * Clone the cell collection
  162. *
  163. * @param PHPExcel_Worksheet $parent The new worksheet
  164. * @return void
  165. */
  166. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  167. parent::copyCellCollection($parent);
  168. // Get a new id for the new file name
  169. $baseUnique = $this->_getUniqueID();
  170. $newCachePrefix = substr(md5($baseUnique),0,8).'.';
  171. $cacheList = $this->getCellList();
  172. foreach($cacheList as $cellID) {
  173. if ($cellID != $this->_currentObjectID) {
  174. $success = false;
  175. $obj = wincache_ucache_get($this->_cachePrefix.$cellID.'.cache', $success);
  176. if ($success === false) {
  177. // Entry no longer exists in WinCache, so clear it from the cache array
  178. parent::deleteCacheData($cellID);
  179. throw new Exception('Cell entry '.$cellID.' no longer exists in Wincache');
  180. }
  181. if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->_cacheTime)) {
  182. $this->__destruct();
  183. throw new Exception('Failed to store cell '.$cellID.' in Wincache');
  184. }
  185. }
  186. }
  187. $this->_cachePrefix = $newCachePrefix;
  188. } // function copyCellCollection()
  189. /**
  190. * Clear the cell collection and disconnect from our parent
  191. *
  192. * @return void
  193. */
  194. public function unsetWorksheetCells() {
  195. if(!is_null($this->_currentObject)) {
  196. $this->_currentObject->detach();
  197. $this->_currentObject = $this->_currentObjectID = null;
  198. }
  199. // Flush the WinCache cache
  200. $this->__destruct();
  201. $this->_cellCache = array();
  202. // detach ourself from the worksheet, so that it can then delete this object successfully
  203. $this->_parent = null;
  204. } // function unsetWorksheetCells()
  205. /**
  206. * Initialise this new cell collection
  207. *
  208. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  209. * @param array of mixed $arguments Additional initialisation arguments
  210. */
  211. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  212. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  213. if (is_null($this->_cachePrefix)) {
  214. $baseUnique = $this->_getUniqueID();
  215. $this->_cachePrefix = substr(md5($baseUnique),0,8).'.';
  216. $this->_cacheTime = $cacheTime;
  217. parent::__construct($parent);
  218. }
  219. } // function __construct()
  220. /**
  221. * Destroy this cell collection
  222. */
  223. public function __destruct() {
  224. $cacheList = $this->getCellList();
  225. foreach($cacheList as $cellID) {
  226. wincache_ucache_delete($this->_cachePrefix.$cellID.'.cache');
  227. }
  228. } // function __destruct()
  229. /**
  230. * Identify whether the caching method is currently available
  231. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  232. *
  233. * @return boolean
  234. */
  235. public static function cacheMethodIsAvailable() {
  236. if (!function_exists('wincache_ucache_add')) {
  237. return false;
  238. }
  239. return true;
  240. }
  241. }