CachedObjectStorageFactory.Class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/8/10
  6. * Time: 6:11 PM
  7. */
  8. namespace Util\PHPExcel;
  9. use Util\PHPExcel\Worksheet;
  10. class CachedObjectStorageFactory
  11. {
  12. const cache_in_memory = 'Memory';
  13. const cache_in_memory_gzip = 'MemoryGZip';
  14. const cache_in_memory_serialized = 'MemorySerialized';
  15. const cache_igbinary = 'Igbinary';
  16. const cache_to_discISAM = 'DiscISAM';
  17. const cache_to_apc = 'APC';
  18. const cache_to_memcache = 'Memcache';
  19. const cache_to_phpTemp = 'PHPTemp';
  20. const cache_to_wincache = 'Wincache';
  21. const cache_to_sqlite = 'SQLite';
  22. const cache_to_sqlite3 = 'SQLite3';
  23. /**
  24. * List of all possible cache storage methods
  25. *
  26. * @var string[]
  27. */
  28. private static $storageMethods = array(
  29. self::cache_in_memory,
  30. self::cache_in_memory_gzip,
  31. self::cache_in_memory_serialized,
  32. self::cache_igbinary,
  33. self::cache_to_phpTemp,
  34. self::cache_to_discISAM,
  35. self::cache_to_apc,
  36. self::cache_to_memcache,
  37. self::cache_to_wincache,
  38. self::cache_to_sqlite,
  39. self::cache_to_sqlite3,
  40. );
  41. /**
  42. * Default arguments for each cache storage method
  43. *
  44. * @var array of mixed array
  45. */
  46. private static $storageMethodDefaultParameters = array(
  47. self::cache_in_memory => array(
  48. ),
  49. self::cache_in_memory_gzip => array(
  50. ),
  51. self::cache_in_memory_serialized => array(
  52. ),
  53. self::cache_igbinary => array(
  54. ),
  55. self::cache_to_phpTemp => array( 'memoryCacheSize' => '1MB'
  56. ),
  57. self::cache_to_discISAM => array( 'dir' => null
  58. ),
  59. self::cache_to_apc => array( 'cacheTime' => 600
  60. ),
  61. self::cache_to_memcache => array( 'memcacheServer' => 'localhost',
  62. 'memcachePort' => 11211,
  63. 'cacheTime' => 600
  64. ),
  65. self::cache_to_wincache => array( 'cacheTime' => 600
  66. ),
  67. self::cache_to_sqlite => array(
  68. ),
  69. self::cache_to_sqlite3 => array(
  70. ),
  71. );
  72. /**
  73. * Arguments for the active cache storage method
  74. *
  75. * @var array of mixed array
  76. */
  77. private static $storageMethodParameters = array();
  78. /**
  79. * Name of the method used for cell cacheing
  80. *
  81. * @var string
  82. */
  83. private static $cacheStorageMethod = null;
  84. /**
  85. * Name of the class used for cell cacheing
  86. *
  87. * @var string
  88. */
  89. private static $cacheStorageClass = null;
  90. /**
  91. * Initialise the cache storage
  92. *
  93. * @param PHPExcel_Worksheet $parent Enable cell caching for this worksheet
  94. * @return PHPExcel_CachedObjectStorage_ICache
  95. **/
  96. public static function getInstance(Worksheet $parent)
  97. {
  98. $cacheMethodIsAvailable = true;
  99. if (self::$cacheStorageMethod === null) {
  100. $cacheMethodIsAvailable = self::initialize();
  101. }
  102. if ($cacheMethodIsAvailable) {
  103. $instance = new self::$cacheStorageClass(
  104. $parent,
  105. self::$storageMethodParameters[self::$cacheStorageMethod]
  106. );
  107. if ($instance !== null) {
  108. return $instance;
  109. }
  110. }
  111. return false;
  112. }
  113. /**
  114. * Identify the cache storage method to use
  115. *
  116. * @param string $method Name of the method to use for cell cacheing
  117. * @param array of mixed $arguments Additional arguments to pass to the cell caching class
  118. * when instantiating
  119. * @return boolean
  120. **/
  121. public static function initialize($method = self::cache_in_memory_serialized, $arguments = array())
  122. {
  123. if (!in_array($method, self::$storageMethods)) {
  124. return false;
  125. }
  126. $cacheStorageClass = "\Util\PHPExcel\CachedObjectStorage\\".$method;
  127. if (!call_user_func(array( $cacheStorageClass,
  128. 'cacheMethodIsAvailable'))) {
  129. return false;
  130. }
  131. self::$storageMethodParameters[$method] = self::$storageMethodDefaultParameters[$method];
  132. foreach ($arguments as $k => $v) {
  133. if (array_key_exists($k, self::$storageMethodParameters[$method])) {
  134. self::$storageMethodParameters[$method][$k] = $v;
  135. }
  136. }
  137. if (self::$cacheStorageMethod === null) {
  138. self::$cacheStorageClass = "\Util\PHPExcel\CachedObjectStorage\\".$method;
  139. self::$cacheStorageMethod = $method;
  140. }
  141. return true;
  142. }
  143. }