CachedObjectStorageFactory.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_CachedObjectStorageFactory
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_CachedObjectStorage
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_CachedObjectStorageFactory
  35. {
  36. const cache_in_memory = 'Memory';
  37. const cache_in_memory_gzip = 'MemoryGZip';
  38. const cache_in_memory_serialized = 'MemorySerialized';
  39. const cache_igbinary = 'Igbinary';
  40. const cache_to_discISAM = 'DiscISAM';
  41. const cache_to_apc = 'APC';
  42. const cache_to_memcache = 'Memcache';
  43. const cache_to_phpTemp = 'PHPTemp';
  44. const cache_to_wincache = 'Wincache';
  45. const cache_to_sqlite = 'SQLite';
  46. const cache_to_sqlite3 = 'SQLite3';
  47. /**
  48. * Name of the method used for cell cacheing
  49. *
  50. * @var string
  51. */
  52. private static $_cacheStorageMethod = NULL;
  53. /**
  54. * Name of the class used for cell cacheing
  55. *
  56. * @var string
  57. */
  58. private static $_cacheStorageClass = NULL;
  59. /**
  60. * List of all possible cache storage methods
  61. *
  62. * @var string[]
  63. */
  64. private static $_storageMethods = array(
  65. self::cache_in_memory,
  66. self::cache_in_memory_gzip,
  67. self::cache_in_memory_serialized,
  68. self::cache_igbinary,
  69. self::cache_to_phpTemp,
  70. self::cache_to_discISAM,
  71. self::cache_to_apc,
  72. self::cache_to_memcache,
  73. self::cache_to_wincache,
  74. self::cache_to_sqlite,
  75. self::cache_to_sqlite3,
  76. );
  77. /**
  78. * Default arguments for each cache storage method
  79. *
  80. * @var array of mixed array
  81. */
  82. private static $_storageMethodDefaultParameters = array(
  83. self::cache_in_memory => array(
  84. ),
  85. self::cache_in_memory_gzip => array(
  86. ),
  87. self::cache_in_memory_serialized => array(
  88. ),
  89. self::cache_igbinary => array(
  90. ),
  91. self::cache_to_phpTemp => array( 'memoryCacheSize' => '1MB'
  92. ),
  93. self::cache_to_discISAM => array( 'dir' => NULL
  94. ),
  95. self::cache_to_apc => array( 'cacheTime' => 600
  96. ),
  97. self::cache_to_memcache => array( 'memcacheServer' => 'localhost',
  98. 'memcachePort' => 11211,
  99. 'cacheTime' => 600
  100. ),
  101. self::cache_to_wincache => array( 'cacheTime' => 600
  102. ),
  103. self::cache_to_sqlite => array(
  104. ),
  105. self::cache_to_sqlite3 => array(
  106. ),
  107. );
  108. /**
  109. * Arguments for the active cache storage method
  110. *
  111. * @var array of mixed array
  112. */
  113. private static $_storageMethodParameters = array();
  114. /**
  115. * Return the current cache storage method
  116. *
  117. * @return string|NULL
  118. **/
  119. public static function getCacheStorageMethod()
  120. {
  121. return self::$_cacheStorageMethod;
  122. } // function getCacheStorageMethod()
  123. /**
  124. * Return the current cache storage class
  125. *
  126. * @return PHPExcel_CachedObjectStorage_ICache|NULL
  127. **/
  128. public static function getCacheStorageClass()
  129. {
  130. return self::$_cacheStorageClass;
  131. } // function getCacheStorageClass()
  132. /**
  133. * Return the list of all possible cache storage methods
  134. *
  135. * @return string[]
  136. **/
  137. public static function getAllCacheStorageMethods()
  138. {
  139. return self::$_storageMethods;
  140. } // function getCacheStorageMethods()
  141. /**
  142. * Return the list of all available cache storage methods
  143. *
  144. * @return string[]
  145. **/
  146. public static function getCacheStorageMethods()
  147. {
  148. $activeMethods = array();
  149. foreach(self::$_storageMethods as $storageMethod) {
  150. $cacheStorageClass = 'PHPExcel_CachedObjectStorage_' . $storageMethod;
  151. if (call_user_func(array($cacheStorageClass, 'cacheMethodIsAvailable'))) {
  152. $activeMethods[] = $storageMethod;
  153. }
  154. }
  155. return $activeMethods;
  156. } // function getCacheStorageMethods()
  157. /**
  158. * Identify the cache storage method to use
  159. *
  160. * @param string $method Name of the method to use for cell cacheing
  161. * @param array of mixed $arguments Additional arguments to pass to the cell caching class
  162. * when instantiating
  163. * @return boolean
  164. **/
  165. public static function initialize($method = self::cache_in_memory, $arguments = array())
  166. {
  167. if (!in_array($method,self::$_storageMethods)) {
  168. return FALSE;
  169. }
  170. $cacheStorageClass = 'PHPExcel_CachedObjectStorage_'.$method;
  171. if (!call_user_func(array( $cacheStorageClass,
  172. 'cacheMethodIsAvailable'))) {
  173. return FALSE;
  174. }
  175. self::$_storageMethodParameters[$method] = self::$_storageMethodDefaultParameters[$method];
  176. foreach($arguments as $k => $v) {
  177. if (array_key_exists($k, self::$_storageMethodParameters[$method])) {
  178. self::$_storageMethodParameters[$method][$k] = $v;
  179. }
  180. }
  181. if (self::$_cacheStorageMethod === NULL) {
  182. self::$_cacheStorageClass = 'PHPExcel_CachedObjectStorage_' . $method;
  183. self::$_cacheStorageMethod = $method;
  184. }
  185. return TRUE;
  186. } // function initialize()
  187. /**
  188. * Initialise the cache storage
  189. *
  190. * @param PHPExcel_Worksheet $parent Enable cell caching for this worksheet
  191. * @return PHPExcel_CachedObjectStorage_ICache
  192. **/
  193. public static function getInstance(PHPExcel_Worksheet $parent)
  194. {
  195. $cacheMethodIsAvailable = TRUE;
  196. if (self::$_cacheStorageMethod === NULL) {
  197. $cacheMethodIsAvailable = self::initialize();
  198. }
  199. if ($cacheMethodIsAvailable) {
  200. $instance = new self::$_cacheStorageClass( $parent,
  201. self::$_storageMethodParameters[self::$_cacheStorageMethod]
  202. );
  203. if ($instance !== NULL) {
  204. return $instance;
  205. }
  206. }
  207. return FALSE;
  208. } // function getInstance()
  209. /**
  210. * Clear the cache storage
  211. *
  212. **/
  213. public static function finalize()
  214. {
  215. self::$_cacheStorageMethod = NULL;
  216. self::$_cacheStorageClass = NULL;
  217. self::$_storageMethodParameters = array();
  218. }
  219. }