Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 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\Http\Client\ClientInterface;
   9  use Psr\Http\Message\RequestFactoryInterface;
  10  use Psr\SimpleCache\CacheInterface;
  11  use ReflectionClass;
  12  
  13  class Settings
  14  {
  15      /**
  16       * Class name of the chart renderer used for rendering charts
  17       * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph.
  18       *
  19       * @var string
  20       */
  21      private static $chartRenderer;
  22  
  23      /**
  24       * Default options for libxml loader.
  25       *
  26       * @var int
  27       */
  28      private static $libXmlLoaderOptions;
  29  
  30      /**
  31       * The cache implementation to be used for cell collection.
  32       *
  33       * @var CacheInterface
  34       */
  35      private static $cache;
  36  
  37      /**
  38       * The HTTP client implementation to be used for network request.
  39       *
  40       * @var null|ClientInterface
  41       */
  42      private static $httpClient;
  43  
  44      /**
  45       * @var null|RequestFactoryInterface
  46       */
  47      private static $requestFactory;
  48  
  49      /**
  50       * Set the locale code to use for formula translations and any special formatting.
  51       *
  52       * @param string $locale The locale code to use (e.g. "fr" or "pt_br" or "en_uk")
  53       *
  54       * @return bool Success or failure
  55       */
  56      public static function setLocale(string $locale)
  57      {
  58          return Calculation::getInstance()->setLocale($locale);
  59      }
  60  
  61      public static function getLocale(): string
  62      {
  63          return Calculation::getInstance()->getLocale();
  64      }
  65  
  66      /**
  67       * Identify to PhpSpreadsheet the external library to use for rendering charts.
  68       *
  69       * @param string $rendererClassName Class name of the chart renderer
  70       *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
  71       */
  72      public static function setChartRenderer(string $rendererClassName): void
  73      {
  74          if (!is_a($rendererClassName, IRenderer::class, true)) {
  75              throw new Exception('Chart renderer must implement ' . IRenderer::class);
  76          }
  77  
  78          self::$chartRenderer = $rendererClassName;
  79      }
  80  
  81      /**
  82       * Return the Chart Rendering Library that PhpSpreadsheet is currently configured to use.
  83       *
  84       * @return null|string Class name of the chart renderer
  85       *    eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph
  86       */
  87      public static function getChartRenderer(): ?string
  88      {
  89          return self::$chartRenderer;
  90      }
  91  
  92      public static function htmlEntityFlags(): int
  93      {
  94          return \ENT_COMPAT;
  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(): int
 117      {
 118          if (self::$libXmlLoaderOptions === null && defined('LIBXML_DTDLOAD')) {
 119              self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR);
 120          } elseif (self::$libXmlLoaderOptions === null) {
 121              self::$libXmlLoaderOptions = 0;
 122          }
 123  
 124          return self::$libXmlLoaderOptions;
 125      }
 126  
 127      /**
 128       * Deprecated, has no effect.
 129       *
 130       * @param bool $state
 131       *
 132       * @deprecated will be removed without replacement as it is no longer necessary on PHP 7.3.0+
 133       */
 134      public static function setLibXmlDisableEntityLoader($state): void
 135      {
 136          // noop
 137      }
 138  
 139      /**
 140       * Deprecated, has no effect.
 141       *
 142       * @return bool $state
 143       *
 144       * @deprecated will be removed without replacement as it is no longer necessary on PHP 7.3.0+
 145       */
 146      public static function getLibXmlDisableEntityLoader(): bool
 147      {
 148          return true;
 149      }
 150  
 151      /**
 152       * Sets the implementation of cache that should be used for cell collection.
 153       */
 154      public static function setCache(CacheInterface $cache): void
 155      {
 156          self::$cache = $cache;
 157      }
 158  
 159      /**
 160       * Gets the implementation of cache that is being used for cell collection.
 161       */
 162      public static function getCache(): CacheInterface
 163      {
 164          if (!self::$cache) {
 165              self::$cache = self::useSimpleCacheVersion3() ? new Memory\SimpleCache3() : new Memory\SimpleCache1();
 166          }
 167  
 168          return self::$cache;
 169      }
 170  
 171      public static function useSimpleCacheVersion3(): bool
 172      {
 173          return
 174              PHP_MAJOR_VERSION === 8 &&
 175              (new ReflectionClass(CacheInterface::class))->getMethod('get')->getReturnType() !== null;
 176      }
 177  
 178      /**
 179       * Set the HTTP client implementation to be used for network request.
 180       */
 181      public static function setHttpClient(ClientInterface $httpClient, RequestFactoryInterface $requestFactory): void
 182      {
 183          self::$httpClient = $httpClient;
 184          self::$requestFactory = $requestFactory;
 185      }
 186  
 187      /**
 188       * Unset the HTTP client configuration.
 189       */
 190      public static function unsetHttpClient(): void
 191      {
 192          self::$httpClient = null;
 193          self::$requestFactory = null;
 194      }
 195  
 196      /**
 197       * Get the HTTP client implementation to be used for network request.
 198       */
 199      public static function getHttpClient(): ClientInterface
 200      {
 201          self::assertHttpClient();
 202  
 203          return self::$httpClient;
 204      }
 205  
 206      /**
 207       * Get the HTTP request factory.
 208       */
 209      public static function getRequestFactory(): RequestFactoryInterface
 210      {
 211          self::assertHttpClient();
 212  
 213          return self::$requestFactory;
 214      }
 215  
 216      private static function assertHttpClient(): void
 217      {
 218          if (!self::$httpClient || !self::$requestFactory) {
 219              throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.');
 220          }
 221      }
 222  }