PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

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

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