Merge pull request #3019 from PHPOffice/Cache-SimpleCache3-Support
Support for SimpleCache Interface versions 1.0, 2.0 and 3.0
This commit is contained in:
commit
cc17d2fef9
|
|
@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
- Support for SimpleCache Interface versions 1.0, 2.0 and 3.0
|
||||||
- Add Chart Axis Option textRotation [Issue #2705](https://github.com/PHPOffice/PhpSpreadsheet/issues/2705) [PR #2940](https://github.com/PHPOffice/PhpSpreadsheet/pull/2940)
|
- Add Chart Axis Option textRotation [Issue #2705](https://github.com/PHPOffice/PhpSpreadsheet/issues/2705) [PR #2940](https://github.com/PHPOffice/PhpSpreadsheet/pull/2940)
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@
|
||||||
"markbaker/matrix": "^3.0",
|
"markbaker/matrix": "^3.0",
|
||||||
"psr/http-client": "^1.0",
|
"psr/http-client": "^1.0",
|
||||||
"psr/http-factory": "^1.0",
|
"psr/http-factory": "^1.0",
|
||||||
"psr/simple-cache": "^1.0 || ^2.0"
|
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"dealerdirect/phpcodesniffer-composer-installer": "dev-master",
|
"dealerdirect/phpcodesniffer-composer-installer": "dev-master",
|
||||||
|
|
|
||||||
|
|
@ -1100,11 +1100,6 @@ parameters:
|
||||||
count: 1
|
count: 1
|
||||||
path: src/PhpSpreadsheet/Cell/Coordinate.php
|
path: src/PhpSpreadsheet/Cell/Coordinate.php
|
||||||
|
|
||||||
-
|
|
||||||
message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Collection\\\\Memory\\:\\:\\$cache has no type specified\\.$#"
|
|
||||||
count: 1
|
|
||||||
path: src/PhpSpreadsheet/Collection/Memory.php
|
|
||||||
|
|
||||||
-
|
-
|
||||||
message: "#^Parameter \\#1 \\$namedRange of method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\:\\:addNamedRange\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\NamedRange, \\$this\\(PhpOffice\\\\PhpSpreadsheet\\\\DefinedName\\) given\\.$#"
|
message: "#^Parameter \\#1 \\$namedRange of method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\:\\:addNamedRange\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\NamedRange, \\$this\\(PhpOffice\\\\PhpSpreadsheet\\\\DefinedName\\) given\\.$#"
|
||||||
count: 1
|
count: 1
|
||||||
|
|
|
||||||
|
|
@ -268,7 +268,9 @@ class Cells
|
||||||
*/
|
*/
|
||||||
private function getUniqueID()
|
private function getUniqueID()
|
||||||
{
|
{
|
||||||
return Settings::getCache() instanceof Memory
|
$cacheType = Settings::getCache();
|
||||||
|
|
||||||
|
return ($cacheType instanceof Memory\SimpleCache1 || $cacheType instanceof Memory\SimpleCache3)
|
||||||
? random_bytes(7) . ':'
|
? random_bytes(7) . ':'
|
||||||
: uniqid('phpspreadsheet.', true) . '.';
|
: uniqid('phpspreadsheet.', true) . '.';
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace PhpOffice\PhpSpreadsheet\Collection;
|
namespace PhpOffice\PhpSpreadsheet\Collection\Memory;
|
||||||
|
|
||||||
use DateInterval;
|
use DateInterval;
|
||||||
use Psr\SimpleCache\CacheInterface;
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
|
@ -11,8 +11,11 @@ use Psr\SimpleCache\CacheInterface;
|
||||||
* Alternatives implementation should leverage off-memory, non-volatile storage
|
* Alternatives implementation should leverage off-memory, non-volatile storage
|
||||||
* to reduce overall memory usage.
|
* to reduce overall memory usage.
|
||||||
*/
|
*/
|
||||||
class Memory implements CacheInterface
|
class SimpleCache1 implements CacheInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var array Cell Cache
|
||||||
|
*/
|
||||||
private $cache = [];
|
private $cache = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -0,0 +1,109 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhpOffice\PhpSpreadsheet\Collection\Memory;
|
||||||
|
|
||||||
|
use DateInterval;
|
||||||
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the default implementation for in-memory cell collection.
|
||||||
|
*
|
||||||
|
* Alternatives implementation should leverage off-memory, non-volatile storage
|
||||||
|
* to reduce overall memory usage.
|
||||||
|
*/
|
||||||
|
class SimpleCache3 implements CacheInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var array Cell Cache
|
||||||
|
*/
|
||||||
|
private $cache = [];
|
||||||
|
|
||||||
|
public function clear(): bool
|
||||||
|
{
|
||||||
|
$this->cache = [];
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
*/
|
||||||
|
public function delete($key): bool
|
||||||
|
{
|
||||||
|
unset($this->cache[$key]);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param iterable $keys
|
||||||
|
*/
|
||||||
|
public function deleteMultiple($keys): bool
|
||||||
|
{
|
||||||
|
foreach ($keys as $key) {
|
||||||
|
$this->delete($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $default
|
||||||
|
*/
|
||||||
|
public function get($key, $default = null): mixed
|
||||||
|
{
|
||||||
|
if ($this->has($key)) {
|
||||||
|
return $this->cache[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param iterable $keys
|
||||||
|
* @param mixed $default
|
||||||
|
*/
|
||||||
|
public function getMultiple($keys, $default = null): iterable
|
||||||
|
{
|
||||||
|
$results = [];
|
||||||
|
foreach ($keys as $key) {
|
||||||
|
$results[$key] = $this->get($key, $default);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
*/
|
||||||
|
public function has($key): bool
|
||||||
|
{
|
||||||
|
return array_key_exists($key, $this->cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $value
|
||||||
|
* @param null|DateInterval|int $ttl
|
||||||
|
*/
|
||||||
|
public function set($key, $value, $ttl = null): bool
|
||||||
|
{
|
||||||
|
$this->cache[$key] = $value;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param iterable $values
|
||||||
|
* @param null|DateInterval|int $ttl
|
||||||
|
*/
|
||||||
|
public function setMultiple($values, $ttl = null): bool
|
||||||
|
{
|
||||||
|
foreach ($values as $key => $value) {
|
||||||
|
$this->set($key, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -52,7 +52,7 @@ class XmlScanner
|
||||||
|
|
||||||
public static function threadSafeLibxmlDisableEntityLoaderAvailability()
|
public static function threadSafeLibxmlDisableEntityLoaderAvailability()
|
||||||
{
|
{
|
||||||
if (PHP_MAJOR_VERSION == 7) {
|
if (PHP_MAJOR_VERSION === 7) {
|
||||||
switch (PHP_MINOR_VERSION) {
|
switch (PHP_MINOR_VERSION) {
|
||||||
case 2:
|
case 2:
|
||||||
return PHP_RELEASE_VERSION >= 1;
|
return PHP_RELEASE_VERSION >= 1;
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use PhpOffice\PhpSpreadsheet\Collection\Memory;
|
||||||
use Psr\Http\Client\ClientInterface;
|
use Psr\Http\Client\ClientInterface;
|
||||||
use Psr\Http\Message\RequestFactoryInterface;
|
use Psr\Http\Message\RequestFactoryInterface;
|
||||||
use Psr\SimpleCache\CacheInterface;
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
use ReflectionClass;
|
||||||
|
|
||||||
class Settings
|
class Settings
|
||||||
{
|
{
|
||||||
|
|
@ -161,12 +162,19 @@ class Settings
|
||||||
public static function getCache(): CacheInterface
|
public static function getCache(): CacheInterface
|
||||||
{
|
{
|
||||||
if (!self::$cache) {
|
if (!self::$cache) {
|
||||||
self::$cache = new Memory();
|
self::$cache = self::useSimpleCacheVersion3() ? new Memory\SimpleCache3() : new Memory\SimpleCache1();
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::$cache;
|
return self::$cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function useSimpleCacheVersion3(): bool
|
||||||
|
{
|
||||||
|
return
|
||||||
|
PHP_MAJOR_VERSION === 8 &&
|
||||||
|
(new ReflectionClass(CacheInterface::class))->getMethod('get')->getReturnType() !== null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the HTTP client implementation to be used for network request.
|
* Set the HTTP client implementation to be used for network request.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Collection;
|
||||||
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
||||||
use PhpOffice\PhpSpreadsheet\Collection\Cells;
|
use PhpOffice\PhpSpreadsheet\Collection\Cells;
|
||||||
use PhpOffice\PhpSpreadsheet\Collection\Memory;
|
use PhpOffice\PhpSpreadsheet\Collection\Memory;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Settings;
|
||||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
@ -107,7 +108,10 @@ class CellsTest extends TestCase
|
||||||
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
|
||||||
|
|
||||||
$collection = $this->getMockBuilder(Cells::class)
|
$collection = $this->getMockBuilder(Cells::class)
|
||||||
->setConstructorArgs([new Worksheet(), new Memory()])
|
->setConstructorArgs([
|
||||||
|
new Worksheet(),
|
||||||
|
Settings::useSimpleCacheVersion3() ? new Memory\SimpleCache3() : new Memory\SimpleCache1(),
|
||||||
|
])
|
||||||
->onlyMethods(['has'])
|
->onlyMethods(['has'])
|
||||||
->getMock();
|
->getMock();
|
||||||
|
|
||||||
|
|
@ -121,7 +125,9 @@ class CellsTest extends TestCase
|
||||||
{
|
{
|
||||||
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
|
||||||
|
|
||||||
$cache = $this->createMock(Memory::class);
|
$cache = $this->createMock(
|
||||||
|
Settings::useSimpleCacheVersion3() ? Memory\SimpleCache3::class : Memory\SimpleCache1::class
|
||||||
|
);
|
||||||
$cell = $this->createMock(Cell::class);
|
$cell = $this->createMock(Cell::class);
|
||||||
$cache->method('set')
|
$cache->method('set')
|
||||||
->willReturn(false);
|
->willReturn(false);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue