Settings.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\Http\Client\ClientInterface;
  7. use Psr\Http\Message\RequestFactoryInterface;
  8. use Psr\SimpleCache\CacheInterface;
  9. class Settings
  10. {
  11. /**
  12. * Class name of the chart renderer used for rendering charts
  13. * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph.
  14. *
  15. * @var string
  16. */
  17. private static $chartRenderer;
  18. /**
  19. * Default options for libxml loader.
  20. *
  21. * @var int
  22. */
  23. private static $libXmlLoaderOptions = null;
  24. /**
  25. * Allow/disallow libxml_disable_entity_loader() call when not thread safe.
  26. * Default behaviour is to do the check, but if you're running PHP versions
  27. * 7.2 < 7.2.1
  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. * The HTTP client implementation to be used for network request.
  42. *
  43. * @var null|ClientInterface
  44. */
  45. private static $httpClient;
  46. /**
  47. * @var null|RequestFactoryInterface
  48. */
  49. private static $requestFactory;
  50. /**
  51. * Set the locale code to use for formula translations and any special formatting.
  52. *
  53. * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
  54. *
  55. * @return bool Success or failure
  56. */
  57. public static function setLocale($locale)
  58. {
  59. return Calculation::getInstance()->setLocale($locale);
  60. }
  61. /**
  62. * Identify to PhpSpreadsheet the external library to use for rendering charts.
  63. *
  64. * @param string $rendererClass Class name of the chart renderer
  65. * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
  66. */
  67. public static function setChartRenderer($rendererClass): void
  68. {
  69. if (!is_a($rendererClass, IRenderer::class, true)) {
  70. throw new Exception('Chart renderer must implement ' . IRenderer::class);
  71. }
  72. self::$chartRenderer = $rendererClass;
  73. }
  74. /**
  75. * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
  76. *
  77. * @return null|string Class name of the chart renderer
  78. * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
  79. */
  80. public static function getChartRenderer()
  81. {
  82. return self::$chartRenderer;
  83. }
  84. /**
  85. * Set default options for libxml loader.
  86. *
  87. * @param int $options Default options for libxml loader
  88. */
  89. public static function setLibXmlLoaderOptions($options): void
  90. {
  91. if ($options === null && defined('LIBXML_DTDLOAD')) {
  92. $options = LIBXML_DTDLOAD | LIBXML_DTDATTR;
  93. }
  94. self::$libXmlLoaderOptions = $options;
  95. }
  96. /**
  97. * Get default options for libxml loader.
  98. * Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly.
  99. *
  100. * @return int Default options for libxml loader
  101. */
  102. public static function getLibXmlLoaderOptions()
  103. {
  104. if (self::$libXmlLoaderOptions === null && defined('LIBXML_DTDLOAD')) {
  105. self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR);
  106. } elseif (self::$libXmlLoaderOptions === null) {
  107. self::$libXmlLoaderOptions = 0;
  108. }
  109. return self::$libXmlLoaderOptions;
  110. }
  111. /**
  112. * Enable/Disable the entity loader for libxml loader.
  113. * Allow/disallow libxml_disable_entity_loader() call when not thread safe.
  114. * Default behaviour is to do the check, but if you're running PHP versions
  115. * 7.2 < 7.2.1
  116. * then you may need to disable this check to prevent unwanted behaviour in other threads
  117. * SECURITY WARNING: Changing this flag to false is not recommended.
  118. *
  119. * @param bool $state
  120. */
  121. public static function setLibXmlDisableEntityLoader($state): void
  122. {
  123. self::$libXmlDisableEntityLoader = (bool) $state;
  124. }
  125. /**
  126. * Return the state of the entity loader (disabled/enabled) for libxml loader.
  127. *
  128. * @return bool $state
  129. */
  130. public static function getLibXmlDisableEntityLoader()
  131. {
  132. return self::$libXmlDisableEntityLoader;
  133. }
  134. /**
  135. * Sets the implementation of cache that should be used for cell collection.
  136. */
  137. public static function setCache(CacheInterface $cache): void
  138. {
  139. self::$cache = $cache;
  140. }
  141. /**
  142. * Gets the implementation of cache that should be used for cell collection.
  143. *
  144. * @return CacheInterface
  145. */
  146. public static function getCache()
  147. {
  148. if (!self::$cache) {
  149. self::$cache = new Memory();
  150. }
  151. return self::$cache;
  152. }
  153. /**
  154. * Set the HTTP client implementation to be used for network request.
  155. */
  156. public static function setHttpClient(ClientInterface $httpClient, RequestFactoryInterface $requestFactory): void
  157. {
  158. self::$httpClient = $httpClient;
  159. self::$requestFactory = $requestFactory;
  160. }
  161. /**
  162. * Unset the HTTP client configuration.
  163. */
  164. public static function unsetHttpClient(): void
  165. {
  166. self::$httpClient = null;
  167. self::$requestFactory = null;
  168. }
  169. /**
  170. * Get the HTTP client implementation to be used for network request.
  171. */
  172. public static function getHttpClient(): ClientInterface
  173. {
  174. self::assertHttpClient();
  175. return self::$httpClient;
  176. }
  177. /**
  178. * Get the HTTP request factory.
  179. */
  180. public static function getRequestFactory(): RequestFactoryInterface
  181. {
  182. self::assertHttpClient();
  183. return self::$requestFactory;
  184. }
  185. private static function assertHttpClient(): void
  186. {
  187. if (!self::$httpClient || !self::$requestFactory) {
  188. throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
  189. }
  190. }
  191. }