For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
9.3 KiB

  1. <?php
  2. /**
  3. * PHPExcel_CachedObjectStorage_APC
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_CachedObjectStorage_APC extends PHPExcel_CachedObjectStorage_CacheBase implements PHPExcel_CachedObjectStorage_ICache
  28. {
  29. /**
  30. * Prefix used to uniquely identify cache data for this worksheet
  31. *
  32. * @access private
  33. * @var string
  34. */
  35. private $cachePrefix = null;
  36. /**
  37. * Cache timeout
  38. *
  39. * @access private
  40. * @var integer
  41. */
  42. private $cacheTime = 600;
  43. /**
  44. * Store cell data in cache for the current cell object if it's "dirty",
  45. * and the 'nullify' the current cell object
  46. *
  47. * @access private
  48. * @return void
  49. * @throws PHPExcel_Exception
  50. */
  51. protected function storeData()
  52. {
  53. if ($this->currentCellIsDirty && !empty($this->currentObjectID)) {
  54. $this->currentObject->detach();
  55. if (!apc_store(
  56. $this->cachePrefix . $this->currentObjectID . '.cache',
  57. serialize($this->currentObject),
  58. $this->cacheTime
  59. )) {
  60. $this->__destruct();
  61. throw new PHPExcel_Exception('Failed to store cell ' . $this->currentObjectID . ' in APC');
  62. }
  63. $this->currentCellIsDirty = false;
  64. }
  65. $this->currentObjectID = $this->currentObject = null;
  66. }
  67. /**
  68. * Add or Update a cell in cache identified by coordinate address
  69. *
  70. * @access public
  71. * @param string $pCoord Coordinate address of the cell to update
  72. * @param PHPExcel_Cell $cell Cell to update
  73. * @return PHPExcel_Cell
  74. * @throws PHPExcel_Exception
  75. */
  76. public function addCacheData($pCoord, PHPExcel_Cell $cell)
  77. {
  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. }
  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. * @throws PHPExcel_Exception
  93. * @return boolean
  94. */
  95. public function isDataSet($pCoord)
  96. {
  97. // Check if the requested entry is the current object, or exists in the cache
  98. if (parent::isDataSet($pCoord)) {
  99. if ($this->currentObjectID == $pCoord) {
  100. return true;
  101. }
  102. // Check if the requested entry still exists in apc
  103. $success = apc_fetch($this->cachePrefix.$pCoord.'.cache');
  104. if ($success === false) {
  105. // Entry no longer exists in APC, so clear it from the cache array
  106. parent::deleteCacheData($pCoord);
  107. throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in APC cache');
  108. }
  109. return true;
  110. }
  111. return false;
  112. }
  113. /**
  114. * Get cell at a specific coordinate
  115. *
  116. * @access public
  117. * @param string $pCoord Coordinate of the cell
  118. * @throws PHPExcel_Exception
  119. * @return PHPExcel_Cell Cell that was found, or null if not found
  120. */
  121. public function getCacheData($pCoord)
  122. {
  123. if ($pCoord === $this->currentObjectID) {
  124. return $this->currentObject;
  125. }
  126. $this->storeData();
  127. // Check if the entry that has been requested actually exists
  128. if (parent::isDataSet($pCoord)) {
  129. $obj = apc_fetch($this->cachePrefix . $pCoord . '.cache');
  130. if ($obj === false) {
  131. // Entry no longer exists in APC, so clear it from the cache array
  132. parent::deleteCacheData($pCoord);
  133. throw new PHPExcel_Exception('Cell entry '.$pCoord.' no longer exists in APC cache');
  134. }
  135. } else {
  136. // Return null if requested entry doesn't exist in cache
  137. return null;
  138. }
  139. // Set current entry to the requested entry
  140. $this->currentObjectID = $pCoord;
  141. $this->currentObject = unserialize($obj);
  142. // Re-attach this as the cell's parent
  143. $this->currentObject->attach($this);
  144. // Return requested entry
  145. return $this->currentObject;
  146. }
  147. /**
  148. * Get a list of all cell addresses currently held in cache
  149. *
  150. * @return string[]
  151. */
  152. public function getCellList()
  153. {
  154. if ($this->currentObjectID !== null) {
  155. $this->storeData();
  156. }
  157. return parent::getCellList();
  158. }
  159. /**
  160. * Delete a cell in cache identified by coordinate address
  161. *
  162. * @access public
  163. * @param string $pCoord Coordinate address of the cell to delete
  164. * @throws PHPExcel_Exception
  165. */
  166. public function deleteCacheData($pCoord)
  167. {
  168. // Delete the entry from APC
  169. apc_delete($this->cachePrefix.$pCoord.'.cache');
  170. // Delete the entry from our cell address array
  171. parent::deleteCacheData($pCoord);
  172. }
  173. /**
  174. * Clone the cell collection
  175. *
  176. * @access public
  177. * @param PHPExcel_Worksheet $parent The new worksheet
  178. * @throws PHPExcel_Exception
  179. * @return void
  180. */
  181. public function copyCellCollection(PHPExcel_Worksheet $parent)
  182. {
  183. parent::copyCellCollection($parent);
  184. // Get a new id for the new file name
  185. $baseUnique = $this->getUniqueID();
  186. $newCachePrefix = substr(md5($baseUnique), 0, 8) . '.';
  187. $cacheList = $this->getCellList();
  188. foreach ($cacheList as $cellID) {
  189. if ($cellID != $this->currentObjectID) {
  190. $obj = apc_fetch($this->cachePrefix . $cellID . '.cache');
  191. if ($obj === false) {
  192. // Entry no longer exists in APC, so clear it from the cache array
  193. parent::deleteCacheData($cellID);
  194. throw new PHPExcel_Exception('Cell entry ' . $cellID . ' no longer exists in APC');
  195. }
  196. if (!apc_store($newCachePrefix . $cellID . '.cache', $obj, $this->cacheTime)) {
  197. $this->__destruct();
  198. throw new PHPExcel_Exception('Failed to store cell ' . $cellID . ' in APC');
  199. }
  200. }
  201. }
  202. $this->cachePrefix = $newCachePrefix;
  203. }
  204. /**
  205. * Clear the cell collection and disconnect from our parent
  206. *
  207. * @return void
  208. */
  209. public function unsetWorksheetCells()
  210. {
  211. if ($this->currentObject !== null) {
  212. $this->currentObject->detach();
  213. $this->currentObject = $this->currentObjectID = null;
  214. }
  215. // Flush the APC cache
  216. $this->__destruct();
  217. $this->cellCache = array();
  218. // detach ourself from the worksheet, so that it can then delete this object successfully
  219. $this->parent = null;
  220. }
  221. /**
  222. * Initialise this new cell collection
  223. *
  224. * @param PHPExcel_Worksheet $parent The worksheet for this cell collection
  225. * @param array of mixed $arguments Additional initialisation arguments
  226. */
  227. public function __construct(PHPExcel_Worksheet $parent, $arguments)
  228. {
  229. $cacheTime = (isset($arguments['cacheTime'])) ? $arguments['cacheTime'] : 600;
  230. if ($this->cachePrefix === null) {
  231. $baseUnique = $this->getUniqueID();
  232. $this->cachePrefix = substr(md5($baseUnique), 0, 8) . '.';
  233. $this->cacheTime = $cacheTime;
  234. parent::__construct($parent);
  235. }
  236. }
  237. /**
  238. * Destroy this cell collection
  239. */
  240. public function __destruct()
  241. {
  242. $cacheList = $this->getCellList();
  243. foreach ($cacheList as $cellID) {
  244. apc_delete($this->cachePrefix . $cellID . '.cache');
  245. }
  246. }
  247. /**
  248. * Identify whether the caching method is currently available
  249. * Some methods are dependent on the availability of certain extensions being enabled in the PHP build
  250. *
  251. * @return boolean
  252. */
  253. public static function cacheMethodIsAvailable()
  254. {
  255. if (!function_exists('apc_store')) {
  256. return false;
  257. }
  258. if (apc_sma_info() === false) {
  259. return false;
  260. }
  261. return true;
  262. }
  263. }