Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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