Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

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