| 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 |
* @deprecated 3.5.0 no longer needed |
| 103 |
*/ |
| 104 |
public static function setLibXmlLoaderOptions($options): int |
| 105 |
{ |
| 106 |
if ($options === null) { |
| 107 |
$options = defined('LIBXML_DTDLOAD') ? (LIBXML_DTDLOAD | LIBXML_DTDATTR) : 0; |
| 108 |
} |
| 109 |
self::$libXmlLoaderOptions = $options; |
| 110 |
|
| 111 |
return $options; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get default options for libxml loader. |
| 116 |
* Defaults to LIBXML_DTDLOAD | LIBXML_DTDATTR when not set explicitly. |
| 117 |
* |
| 118 |
* @return int Default options for libxml loader |
| 119 |
* |
| 120 |
* @deprecated 3.5.0 no longer needed |
| 121 |
*/ |
| 122 |
public static function getLibXmlLoaderOptions(): int |
| 123 |
{ |
| 124 |
return self::$libXmlLoaderOptions ?? (defined('LIBXML_DTDLOAD') ? (LIBXML_DTDLOAD | LIBXML_DTDATTR) : 0); |
| 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 |
* @codeCoverageIgnore |
| 135 |
*/ |
| 136 |
public static function setLibXmlDisableEntityLoader(/** @scrutinizer ignore-unused */ $state): void |
| 137 |
{ |
| 138 |
// noop |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Deprecated, has no effect. |
| 143 |
* |
| 144 |
* @return bool $state |
| 145 |
* |
| 146 |
* @deprecated will be removed without replacement as it is no longer necessary on PHP 7.3.0+ |
| 147 |
* |
| 148 |
* @codeCoverageIgnore |
| 149 |
*/ |
| 150 |
public static function getLibXmlDisableEntityLoader(): bool |
| 151 |
{ |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Sets the implementation of cache that should be used for cell collection. |
| 157 |
*/ |
| 158 |
public static function setCache(?CacheInterface $cache): void |
| 159 |
{ |
| 160 |
self::$cache = $cache; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Gets the implementation of cache that is being used for cell collection. |
| 165 |
*/ |
| 166 |
public static function getCache(): CacheInterface |
| 167 |
{ |
| 168 |
if (!self::$cache) { |
| 169 |
self::$cache = self::useSimpleCacheVersion3() ? new Memory\SimpleCache3() : new Memory\SimpleCache1(); |
| 170 |
} |
| 171 |
|
| 172 |
return self::$cache; |
| 173 |
} |
| 174 |
|
| 175 |
public static function useSimpleCacheVersion3(): bool |
| 176 |
{ |
| 177 |
return |
| 178 |
PHP_MAJOR_VERSION === 8 && |
| 179 |
(new ReflectionClass(CacheInterface::class))->getMethod('get')->getReturnType() !== null; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Set the HTTP client implementation to be used for network request. |
| 184 |
*/ |
| 185 |
public static function setHttpClient(ClientInterface $httpClient, RequestFactoryInterface $requestFactory): void |
| 186 |
{ |
| 187 |
self::$httpClient = $httpClient; |
| 188 |
self::$requestFactory = $requestFactory; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Unset the HTTP client configuration. |
| 193 |
*/ |
| 194 |
public static function unsetHttpClient(): void |
| 195 |
{ |
| 196 |
self::$httpClient = null; |
| 197 |
self::$requestFactory = null; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Get the HTTP client implementation to be used for network request. |
| 202 |
*/ |
| 203 |
public static function getHttpClient(): ClientInterface |
| 204 |
{ |
| 205 |
if (!self::$httpClient || !self::$requestFactory) { |
| 206 |
throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.'); |
| 207 |
} |
| 208 |
|
| 209 |
return self::$httpClient; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Get the HTTP request factory. |
| 214 |
*/ |
| 215 |
public static function getRequestFactory(): RequestFactoryInterface |
| 216 |
{ |
| 217 |
if (!self::$httpClient || !self::$requestFactory) { |
| 218 |
throw new Exception('HTTP client must be configured via Settings::setHttpClient() to be able to use WEBSERVICE function.'); |
| 219 |
} |
| 220 |
|
| 221 |
return self::$requestFactory; |
| 222 |
} |
| 223 |
} |
| 224 |
|