Settings.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * The cache implementation to be used for cell collection.
  24. *
  25. * @var CacheInterface
  26. */
  27. private static $cache;
  28. /**
  29. * Set the locale code to use for formula translations and any special formatting.
  30. *
  31. * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
  32. *
  33. * @return bool Success or failure
  34. */
  35. public static function setLocale($locale)
  36. {
  37. return Calculation::getInstance()->setLocale($locale);
  38. }
  39. /**
  40. * Identify to PhpSpreadsheet the external library to use for rendering charts.
  41. *
  42. * @param string $rendererClass Class name of the chart renderer
  43. * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
  44. *
  45. * @throws Exception
  46. */
  47. public static function setChartRenderer($rendererClass)
  48. {
  49. if (!is_a($rendererClass, IRenderer::class, true)) {
  50. throw new Exception('Chart renderer must implement ' . IRenderer::class);
  51. }
  52. self::$chartRenderer = $rendererClass;
  53. }
  54. /**
  55. * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
  56. *
  57. * @return null|string Class name of the chart renderer
  58. * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
  59. */
  60. public static function getChartRenderer()
  61. {
  62. return self::$chartRenderer;
  63. }
  64. /**
  65. * Set default options for libxml loader.
  66. *
  67. * @param int $options Default options for libxml loader
  68. */
  69. public static function setLibXmlLoaderOptions($options)
  70. {
  71. if ($options === null && defined('LIBXML_DTDLOAD')) {
  72. $options = LIBXML_DTDLOAD | LIBXML_DTDATTR;
  73. }
  74. self::$libXmlLoaderOptions = $options;
  75. }
  76. /**
  77. * Get default options for libxml loader.
  78. * Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly.
  79. *
  80. * @return int Default options for libxml loader
  81. */
  82. public static function getLibXmlLoaderOptions()
  83. {
  84. if (self::$libXmlLoaderOptions === null && defined('LIBXML_DTDLOAD')) {
  85. self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR);
  86. } elseif (self::$libXmlLoaderOptions === null) {
  87. self::$libXmlLoaderOptions = true;
  88. }
  89. return self::$libXmlLoaderOptions;
  90. }
  91. /**
  92. * Sets the implementation of cache that should be used for cell collection.
  93. *
  94. * @param CacheInterface $cache
  95. */
  96. public static function setCache(CacheInterface $cache)
  97. {
  98. self::$cache = $cache;
  99. }
  100. /**
  101. * Gets the implementation of cache that should be used for cell collection.
  102. *
  103. * @return CacheInterface
  104. */
  105. public static function getCache()
  106. {
  107. if (!self::$cache) {
  108. self::$cache = new Memory();
  109. }
  110. return self::$cache;
  111. }
  112. }