CachedObjectStorageFactory.php 7.0 KB

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