0
0

CachedObjectStorageFactory.php 7.7 KB

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