Settings.php 6.3 KB

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