PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/includes/phpexcel/PHPExcel/CachedObjectStorage/Wincache.php

http://github.com/Dolibarr/dolibarr
PHP | 230 lines | 114 code | 36 blank | 80 comment | 20 complexity | 22d0187f8dbdaa74416d882b978b8dad MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0, LGPL-2.0, CC-BY-SA-4.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, MIT
  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. */
  27. /**
  28. * PHPExcel_CachedObjectStorage_Wincache
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorage_Wincache extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache {
  35. private $_cachePrefix = null;
  36. private $_cacheTime = 600;
  37. private function _storeData() {
  38. $this->_currentObject->detach();
  39. $obj = serialize($this->_currentObject);
  40. if (wincache_ucache_exists($this->_cachePrefix.$this->_currentObjectID.'.cache')) {
  41. if (!wincache_ucache_set($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime)) {
  42. $this->__destruct();
  43. throw new Exception('Failed to store cell '.$cellID.' in WinCache');
  44. }
  45. } else {
  46. if (!wincache_ucache_add($this->_cachePrefix.$this->_currentObjectID.'.cache', $obj, $this->_cacheTime)) {
  47. $this->__destruct();
  48. throw new Exception('Failed to store cell '.$cellID.' in WinCache');
  49. }
  50. }
  51. $this->_currentObjectID = $this->_currentObject = null;
  52. } // function _storeData()
  53. /**
  54. * Add or Update a cell in cache identified by coordinate address
  55. *
  56. * @param string $pCoord Coordinate address of the cell to update
  57. * @param PHPExcel_Cell $cell Cell to update
  58. * @return void
  59. * @throws Exception
  60. */
  61. public function addCacheData($pCoord, PHPExcel_Cell $cell) {
  62. if (($pCoord !== $this->_currentObjectID) && ($this->_currentObjectID !== null)) {
  63. $this->_storeData();
  64. }
  65. $this->_cellCache[$pCoord] = true;
  66. $this->_currentObjectID = $pCoord;
  67. $this->_currentObject = $cell;
  68. return $cell;
  69. } // function addCacheData()
  70. /**
  71. * Is a value set in the current PHPExcel_CachedObjectStorage_ICache for an indexed cell?
  72. *
  73. * @param string $pCoord Coordinate address of the cell to check
  74. * @return void
  75. * @return boolean
  76. */
  77. public function isDataSet($pCoord) {
  78. // Check if the requested entry is the current object, or exists in the cache
  79. if (parent::isDataSet($pCoord)) {
  80. if ($this->_currentObjectID == $pCoord) {
  81. return true;
  82. }
  83. // Check if the requested entry still exists in cache
  84. $success = wincache_ucache_exists($this->_cachePrefix.$pCoord.'.cache');
  85. if ($success === false) {
  86. // Entry no longer exists in Wincache, so clear it from the cache array
  87. parent::deleteCacheData($pCoord);
  88. throw new Exception('Cell entry '.$cellID.' no longer exists in WinCache');
  89. }
  90. return true;
  91. }
  92. return false;
  93. } // function isDataSet()
  94. /**
  95. * Get cell at a specific coordinate
  96. *
  97. * @param string $pCoord Coordinate of the cell
  98. * @throws Exception
  99. * @return PHPExcel_Cell Cell that was found, or null if not found
  100. */
  101. public function getCacheData($pCoord) {
  102. if ($pCoord === $this->_currentObjectID) {
  103. return $this->_currentObject;
  104. }
  105. $this->_storeData();
  106. // Check if the entry that has been requested actually exists
  107. $obj = null;
  108. if (parent::isDataSet($pCoord)) {
  109. $success = false;
  110. $obj = wincache_ucache_get($this->_cachePrefix.$pCoord.'.cache', $success);
  111. if ($success === false) {
  112. // Entry no longer exists in WinCache, so clear it from the cache array
  113. parent::deleteCacheData($pCoord);
  114. throw new Exception('Cell entry '.$cellID.' no longer exists in WinCache');
  115. }
  116. } else {
  117. // Return null if requested entry doesn't exist in cache
  118. return null;
  119. }
  120. // Set current entry to the requested entry
  121. $this->_currentObjectID = $pCoord;
  122. $this->_currentObject = unserialize($obj);
  123. // Re-attach the parent worksheet
  124. $this->_currentObject->attach($this->_parent);
  125. // Return requested entry
  126. return $this->_currentObject;
  127. } // function getCacheData()
  128. /**
  129. * Delete a cell in cache identified by coordinate address
  130. *
  131. * @param string $pCoord Coordinate address of the cell to delete
  132. * @throws Exception
  133. */
  134. public function deleteCacheData($pCoord) {
  135. // Delete the entry from Wincache
  136. wincache_ucache_delete($this->_cachePrefix.$pCoord.'.cache');
  137. // Delete the entry from our cell address array
  138. parent::deleteCacheData($pCoord);
  139. } // function deleteCacheData()
  140. /**
  141. * Clone the cell collection
  142. *
  143. * @return void
  144. */
  145. public function copyCellCollection(PHPExcel_Worksheet $parent) {
  146. parent::copyCellCollection($parent);
  147. // Get a new id for the new file name
  148. $baseUnique = $this->_getUniqueID();
  149. $newCachePrefix = substr(md5($baseUnique),0,8).'.';
  150. $cacheList = $this->getCellList();
  151. foreach($cacheList as $cellID) {
  152. if ($cellID != $this->_currentObjectID) {
  153. $success = false;
  154. $obj = wincache_ucache_get($this->_cachePrefix.$cellID.'.cache', $success);
  155. if ($success === false) {
  156. // Entry no longer exists in WinCache, so clear it from the cache array
  157. parent::deleteCacheData($cellID);
  158. throw new Exception('Cell entry '.$cellID.' no longer exists in Wincache');
  159. }
  160. if (!wincache_ucache_add($newCachePrefix.$cellID.'.cache', $obj, $this->_cacheTime)) {
  161. $this->__destruct();
  162. throw new Exception('Failed to store cell '.$cellID.' in Wincache');
  163. }
  164. }
  165. }
  166. $this->_cachePrefix = $newCachePrefix;
  167. } // function copyCellCollection()
  168. public function unsetWorksheetCells() {
  169. if(!is_null($this->_currentObject)) {
  170. $this->_currentObject->detach();
  171. $this->_currentObject = $this->_currentObjectID = null;
  172. }
  173. // Flush the WinCache cache
  174. $this->__destruct();
  175. $this->_cellCache = array();
  176. // detach ourself from the worksheet, so that it can then delete this object successfully
  177. $this->_parent = null;
  178. } // function unsetWorksheetCells()
  179. public function __construct(PHPExcel_Worksheet $parent, $arguments) {
  180. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  181. if (is_null($this->_cachePrefix)) {
  182. $baseUnique = $this->_getUniqueID();
  183. $this->_cachePrefix = substr(md5($baseUnique),0,8).'.';
  184. $this->_cacheTime = $cacheTime;
  185. parent::__construct($parent);
  186. }
  187. } // function __construct()
  188. public function __destruct() {
  189. $cacheList = $this->getCellList();
  190. foreach($cacheList as $cellID) {
  191. wincache_ucache_delete($this->_cachePrefix.$cellID.'.cache');
  192. }
  193. } // function __destruct()
  194. }