Support for SimpleCache Interface versions 1.0, 2.0 and 3.0; to stop people moaning; even though it requires a second implementation of the Memory cache for Cells

This commit is contained in:
MarkBaker 2022-08-20 21:27:05 +02:00
parent 4e82b55f37
commit e67de6f300
9 changed files with 137 additions and 13 deletions

View File

@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
### 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)
### Changed

View File

@ -75,7 +75,7 @@
"markbaker/matrix": "^3.0",
"psr/http-client": "^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": {
"dealerdirect/phpcodesniffer-composer-installer": "dev-master",

View File

@ -1100,11 +1100,6 @@ parameters:
count: 1
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\\.$#"
count: 1

View File

@ -268,7 +268,9 @@ class Cells
*/
private function getUniqueID()
{
return Settings::getCache() instanceof Memory
$cacheType = Settings::getCache();
return ($cacheType instanceof Memory\SimpleCache1 || $cacheType instanceof Memory\SimpleCache3)
? random_bytes(7) . ':'
: uniqid('phpspreadsheet.', true) . '.';
}

View File

@ -1,6 +1,6 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Collection;
namespace PhpOffice\PhpSpreadsheet\Collection\Memory;
use DateInterval;
use Psr\SimpleCache\CacheInterface;
@ -11,8 +11,11 @@ use Psr\SimpleCache\CacheInterface;
* Alternatives implementation should leverage off-memory, non-volatile storage
* to reduce overall memory usage.
*/
class Memory implements CacheInterface
class SimpleCache1 implements CacheInterface
{
/**
* @var array Cell Cache
*/
private $cache = [];
/**

View File

@ -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;
}
}

View File

@ -52,7 +52,7 @@ class XmlScanner
public static function threadSafeLibxmlDisableEntityLoaderAvailability()
{
if (PHP_MAJOR_VERSION == 7) {
if (PHP_MAJOR_VERSION === 7) {
switch (PHP_MINOR_VERSION) {
case 2:
return PHP_RELEASE_VERSION >= 1;

View File

@ -8,6 +8,7 @@ use PhpOffice\PhpSpreadsheet\Collection\Memory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\SimpleCache\CacheInterface;
use ReflectionClass;
class Settings
{
@ -161,12 +162,19 @@ class Settings
public static function getCache(): CacheInterface
{
if (!self::$cache) {
self::$cache = new Memory();
self::$cache = self::useSimpleCacheVersion3() ? new Memory\SimpleCache3() : new Memory\SimpleCache1();
}
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.
*/

View File

@ -5,6 +5,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Collection;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Collection\Cells;
use PhpOffice\PhpSpreadsheet\Collection\Memory;
use PhpOffice\PhpSpreadsheet\Settings;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PHPUnit\Framework\TestCase;
@ -107,7 +108,10 @@ class CellsTest extends TestCase
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$collection = $this->getMockBuilder(Cells::class)
->setConstructorArgs([new Worksheet(), new Memory()])
->setConstructorArgs([
new Worksheet(),
Settings::useSimpleCacheVersion3() ? new Memory\SimpleCache3() : new Memory\SimpleCache1(),
])
->onlyMethods(['has'])
->getMock();
@ -121,7 +125,9 @@ class CellsTest extends TestCase
{
$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);
$cache->method('set')
->willReturn(false);