Settings.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Chart\Renderer\IRenderer;
  5. use PhpOffice\PhpSpreadsheet\Collection\Memory;
  6. use Psr\SimpleCache\CacheInterface;
  7. class Settings
  8. {
  9. /**
  10. * Class name of the chart renderer used for rendering charts
  11. * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph.
  12. *
  13. * @var string
  14. */
  15. private static $chartRenderer;
  16. /**
  17. * Default options for libxml loader.
  18. *
  19. * @var int
  20. */
  21. private static $libXmlLoaderOptions = null;
  22. /**
  23. * Allow/disallow libxml_disable_entity_loader() call when not thread safe.
  24. * Default behaviour is to do the check, but if you're running PHP versions
  25. * 7.2 < 7.2.1
  26. * 7.1 < 7.1.13
  27. * 7.0 < 7.0.27
  28. * then you may need to disable this check to prevent unwanted behaviour in other threads
  29. * SECURITY WARNING: Changing this flag is not recommended.
  30. *
  31. * @var bool
  32. */
  33. private static $libXmlDisableEntityLoader = true;
  34. /**
  35. * The cache implementation to be used for cell collection.
  36. *
  37. * @var CacheInterface
  38. */
  39. private static $cache;
  40. /**
  41. * Set the locale code to use for formula translations and any special formatting.
  42. *
  43. * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
  44. *
  45. * @return bool Success or failure
  46. */
  47. public static function setLocale($locale)
  48. {
  49. return Calculation::getInstance()->setLocale($locale);
  50. }
  51. /**
  52. * Identify to PhpSpreadsheet the external library to use for rendering charts.
  53. *
  54. * @param string $rendererClass Class name of the chart renderer
  55. * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
  56. *
  57. * @throws Exception
  58. */
  59. public static function setChartRenderer($rendererClass)
  60. {
  61. if (!is_a($rendererClass, IRenderer::class, true)) {
  62. throw new Exception('Chart renderer must implement ' . IRenderer::class);
  63. }
  64. self::$chartRenderer = $rendererClass;
  65. }
  66. /**
  67. * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
  68. *
  69. * @return null|string Class name of the chart renderer
  70. * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
  71. */
  72. public static function getChartRenderer()
  73. {
  74. return self::$chartRenderer;
  75. }
  76. /**
  77. * Set default options for libxml loader.
  78. *
  79. * @param int $options Default options for libxml loader
  80. */
  81. public static function setLibXmlLoaderOptions($options)
  82. {
  83. if ($options === null && defined('LIBXML_DTDLOAD')) {
  84. $options = LIBXML_DTDLOAD | LIBXML_DTDATTR;
  85. }
  86. self::$libXmlLoaderOptions = $options;
  87. }
  88. /**
  89. * Get default options for libxml loader.
  90. * Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly.
  91. *
  92. * @return int Default options for libxml loader
  93. */
  94. public static function getLibXmlLoaderOptions()
  95. {
  96. if (self::$libXmlLoaderOptions === null && defined('LIBXML_DTDLOAD')) {
  97. self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR);
  98. } elseif (self::$libXmlLoaderOptions === null) {
  99. self::$libXmlLoaderOptions = true;
  100. }
  101. return self::$libXmlLoaderOptions;
  102. }
  103. /**
  104. * Enable/Disable the entity loader for libxml loader.
  105. * Allow/disallow libxml_disable_entity_loader() call when not thread safe.
  106. * Default behaviour is to do the check, but if you're running PHP versions
  107. * 7.2 < 7.2.1
  108. * 7.1 < 7.1.13
  109. * 7.0 < 7.0.27
  110. * then you may need to disable this check to prevent unwanted behaviour in other threads
  111. * SECURITY WARNING: Changing this flag to false is not recommended.
  112. *
  113. * @param bool $state
  114. */
  115. public static function setLibXmlDisableEntityLoader($state)
  116. {
  117. self::$libXmlDisableEntityLoader = (bool) $state;
  118. }
  119. /**
  120. * Return the state of the entity loader (disabled/enabled) for libxml loader.
  121. *
  122. * @return bool $state
  123. */
  124. public static function getLibXmlDisableEntityLoader()
  125. {
  126. return self::$libXmlDisableEntityLoader;
  127. }
  128. /**
  129. * Sets the implementation of cache that should be used for cell collection.
  130. *
  131. * @param CacheInterface $cache
  132. */
  133. public static function setCache(CacheInterface $cache)
  134. {
  135. self::$cache = $cache;
  136. }
  137. /**
  138. * Gets the implementation of cache that should be used for cell collection.
  139. *
  140. * @return CacheInterface
  141. */
  142. public static function getCache()
  143. {
  144. if (!self::$cache) {
  145. self::$cache = new Memory();
  146. }
  147. return self::$cache;
  148. }
  149. }