Update Composer packages

This commit is contained in:
Paul Nicoué 2022-12-19 14:56:05 +01:00
parent 0320235f6c
commit a8b68fb61b
378 changed files with 28466 additions and 28852 deletions

View file

@ -15,11 +15,17 @@ use APCUIterator;
*/
class ApcuCache extends Cache
{
/**
* Returns whether the cache is ready to
* store values
*/
public function enabled(): bool
{
return apcu_enabled();
}
/**
* Determines if an item exists in the cache
*
* @param string $key
* @return bool
*/
public function exists(string $key): bool
{
@ -29,24 +35,19 @@ class ApcuCache extends Cache
/**
* Flushes the entire cache and returns
* whether the operation was successful
*
* @return bool
*/
public function flush(): bool
{
if (empty($this->options['prefix']) === false) {
return apcu_delete(new APCUIterator('!^' . preg_quote($this->options['prefix']) . '!'));
} else {
return apcu_clear_cache();
}
return apcu_clear_cache();
}
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
@ -56,13 +57,11 @@ class ApcuCache extends Cache
/**
* Internal method to retrieve the raw cache value;
* needs to return a Value object or null if not found
*
* @param string $key
* @return \Kirby\Cache\Value|null
*/
public function retrieve(string $key)
public function retrieve(string $key): Value|null
{
return Value::fromJson(apcu_fetch($this->key($key)));
$value = apcu_fetch($this->key($key));
return Value::fromJson($value);
}
/**
@ -73,14 +72,12 @@ class ApcuCache extends Cache
* // put an item in the cache for 15 minutes
* $cache->set('value', 'my value', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return bool
*/
public function set(string $key, $value, int $minutes = 0): bool
{
return apcu_store($this->key($key), (new Value($value, $minutes))->toJson(), $this->expiration($minutes));
$key = $this->key($key);
$value = (new Value($value, $minutes))->toJson();
$expires = $this->expiration($minutes);
return apcu_store($key, $value, $expires);
}
}

View file

@ -2,6 +2,8 @@
namespace Kirby\Cache;
use Closure;
/**
* Cache foundation
* This abstract class is used as
@ -18,14 +20,11 @@ abstract class Cache
{
/**
* Stores all options for the driver
* @var array
*/
protected $options = [];
protected array $options = [];
/**
* Sets all parameters which are needed to connect to the cache storage
*
* @param array $options
*/
public function __construct(array $options = [])
{
@ -33,87 +32,47 @@ abstract class Cache
}
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful;
* this needs to be defined by the driver
*
* <code>
* // put an item in the cache for 15 minutes
* $cache->set('value', 'my value', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return bool
* Checks when the cache has been created;
* returns the creation timestamp on success
* and false if the item does not exist
*/
abstract public function set(string $key, $value, int $minutes = 0): bool;
/**
* Adds the prefix to the key if given
*
* @param string $key
* @return string
*/
protected function key(string $key): string
public function created(string $key): int|false
{
if (empty($this->options['prefix']) === false) {
$key = $this->options['prefix'] . '/' . $key;
}
return $key;
}
/**
* Internal method to retrieve the raw cache value;
* needs to return a Value object or null if not found;
* this needs to be defined by the driver
*
* @param string $key
* @return \Kirby\Cache\Value|null
*/
abstract public function retrieve(string $key);
/**
* Gets an item from the cache
*
* <code>
* // get an item from the cache driver
* $value = $cache->get('value');
*
* // return a default value if the requested item isn't cached
* $value = $cache->get('value', 'default value');
* </code>
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key, $default = null)
{
// get the Value
// get the Value object
$value = $this->retrieve($key);
// check for a valid cache value
if (!is_a($value, 'Kirby\Cache\Value')) {
return $default;
// check for a valid Value object
if ($value instanceof Value === false) {
return false;
}
// remove the item if it is expired
if ($value->expires() > 0 && time() >= $value->expires()) {
$this->remove($key);
return $default;
}
// return the pure value
return $value->value();
// return the expires timestamp
return $value->created();
}
/**
* Returns whether the cache is ready to
* store values
*/
public function enabled(): bool
{
// TODO: Make this method abstract in a future
// release to ensure that cache drivers override it;
// until then, we assume that the cache is enabled
return true;
}
/**
* Determines if an item exists in the cache
*/
public function exists(string $key): bool
{
return $this->expired($key) === false;
}
/**
* Calculates the expiration timestamp
*
* @param int $minutes
* @return int
*/
protected function expiration(int $minutes = 0): int
{
@ -130,17 +89,14 @@ abstract class Cache
* Checks when an item in the cache expires;
* returns the expiry timestamp on success, null if the
* item never expires and false if the item does not exist
*
* @param string $key
* @return int|null|false
*/
public function expires(string $key)
public function expires(string $key): int|false|null
{
// get the Value object
$value = $this->retrieve($key);
// check for a valid Value object
if (!is_a($value, 'Kirby\Cache\Value')) {
if ($value instanceof Value === false) {
return false;
}
@ -150,9 +106,6 @@ abstract class Cache
/**
* Checks if an item in the cache is expired
*
* @param string $key
* @return bool
*/
public function expired(string $key): bool
{
@ -160,83 +113,126 @@ abstract class Cache
if ($expires === null) {
return false;
} elseif (!is_int($expires)) {
return true;
} else {
return time() >= $expires;
}
if (is_int($expires) === false) {
return true;
}
return time() >= $expires;
}
/**
* Checks when the cache has been created;
* returns the creation timestamp on success
* and false if the item does not exist
*
* @param string $key
* @return int|false
* Flushes the entire cache and returns
* whether the operation was successful;
* this needs to be defined by the driver
*/
public function created(string $key)
abstract public function flush(): bool;
/**
* Gets an item from the cache
*
* <code>
* // get an item from the cache driver
* $value = $cache->get('value');
*
* // return a default value if the requested item isn't cached
* $value = $cache->get('value', 'default value');
* </code>
*/
public function get(string $key, $default = null)
{
// get the Value object
// get the Value
$value = $this->retrieve($key);
// check for a valid Value object
if (!is_a($value, 'Kirby\Cache\Value')) {
return false;
// check for a valid cache value
if ($value instanceof Value === false) {
return $default;
}
// return the expires timestamp
return $value->created();
// remove the item if it is expired
if ($value->expires() > 0 && time() >= $value->expires()) {
$this->remove($key);
return $default;
}
// return the pure value
return $value->value();
}
/**
* Returns a value by either getting it from the cache
* or via the callback function which then is stored in
* the cache for future retrieval. This method cannot be
* used for `null` as value to be cached.
* @since 3.8.0
*/
public function getOrSet(
string $key,
Closure $result,
int $minutes = 0
) {
$value = $this->get($key);
$result = $value ?? $result();
if ($value === null) {
$this->set($key, $result, $minutes);
}
return $result;
}
/**
* Adds the prefix to the key if given
*/
protected function key(string $key): string
{
if (empty($this->options['prefix']) === false) {
$key = $this->options['prefix'] . '/' . $key;
}
return $key;
}
/**
* Alternate version for Cache::created($key)
*
* @param string $key
* @return int|false
*/
public function modified(string $key)
public function modified(string $key): int|false
{
return static::created($key);
}
/**
* Determines if an item exists in the cache
*
* @param string $key
* @return bool
* Returns all passed cache options
*/
public function exists(string $key): bool
public function options(): array
{
return $this->expired($key) === false;
return $this->options;
}
/**
* Removes an item from the cache and returns
* whether the operation was successful;
* this needs to be defined by the driver
*
* @param string $key
* @return bool
*/
abstract public function remove(string $key): bool;
/**
* Flushes the entire cache and returns
* whether the operation was successful;
* Internal method to retrieve the raw cache value;
* needs to return a Value object or null if not found;
* this needs to be defined by the driver
*
* @return bool
*/
abstract public function flush(): bool;
abstract public function retrieve(string $key): Value|null;
/**
* Returns all passed cache options
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful;
* this needs to be defined by the driver
*
* @return array
* <code>
* // put an item in the cache for 15 minutes
* $cache->set('value', 'my value', 15);
* </code>
*/
public function options(): array
{
return $this->options;
}
abstract public function set(string $key, $value, int $minutes = 0): bool;
}

View file

@ -20,10 +20,8 @@ class FileCache extends Cache
{
/**
* Full root including prefix
*
* @var string
*/
protected $root;
protected string $root;
/**
* Sets all parameters which are needed for the file cache
@ -44,6 +42,7 @@ class FileCache extends Cache
// build the full root including prefix
$this->root = $this->options['root'];
if (empty($this->options['prefix']) === false) {
$this->root .= '/' . $this->options['prefix'];
}
@ -52,10 +51,17 @@ class FileCache extends Cache
Dir::make($this->root, true);
}
/**
* Returns whether the cache is ready to
* store values
*/
public function enabled(): bool
{
return is_writable($this->root) === true;
}
/**
* Returns the full root including prefix
*
* @return string
*/
public function root(): string
{
@ -64,9 +70,6 @@ class FileCache extends Cache
/**
* Returns the full path to a file for a given key
*
* @param string $key
* @return string
*/
protected function file(string $key): string
{
@ -108,9 +111,9 @@ class FileCache extends Cache
if (isset($this->options['extension'])) {
return $file . '.' . $this->options['extension'];
} else {
return $file;
}
return $file;
}
/**
@ -121,11 +124,6 @@ class FileCache extends Cache
* // put an item in the cache for 15 minutes
* $cache->set('value', 'my value', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return bool
*/
public function set(string $key, $value, int $minutes = 0): bool
{
@ -137,11 +135,8 @@ class FileCache extends Cache
/**
* Internal method to retrieve the raw cache value;
* needs to return a Value object or null if not found
*
* @param string $key
* @return \Kirby\Cache\Value|null
*/
public function retrieve(string $key)
public function retrieve(string $key): Value|null
{
$file = $this->file($key);
$value = F::read($file);
@ -153,11 +148,8 @@ class FileCache extends Cache
* Checks when the cache has been created;
* returns the creation timestamp on success
* and false if the item does not exist
*
* @param string $key
* @return mixed
*/
public function created(string $key)
public function created(string $key): int|false
{
// use the modification timestamp
// as indicator when the cache has been created/overwritten
@ -165,15 +157,12 @@ class FileCache extends Cache
// get the file for this cache key
$file = $this->file($key);
return file_exists($file) ? filemtime($this->file($key)) : false;
return file_exists($file) ? filemtime($file) : false;
}
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
@ -190,9 +179,6 @@ class FileCache extends Cache
/**
* Removes empty directories safely by checking each directory
* up to the root directory
*
* @param string $dir
* @return void
*/
protected function removeEmptyDirectories(string $dir): void
{
@ -202,7 +188,13 @@ class FileCache extends Cache
// checks all directory segments until reaching the root directory
while (Str::startsWith($dir, $this->root()) === true && $dir !== $this->root()) {
$files = array_diff(scandir($dir) ?? [], ['.', '..']);
$files = scandir($dir);
if ($files === false) {
$files = []; // @codeCoverageIgnore
}
$files = array_diff($files, ['.', '..']);
if (empty($files) === true && Dir::remove($dir) === true) {
// continue with the next level up
@ -212,7 +204,7 @@ class FileCache extends Cache
break;
}
}
} catch (Exception $e) { // @codeCoverageIgnore
} catch (Exception) { // @codeCoverageIgnore
// silently stops the process
}
}
@ -220,12 +212,13 @@ class FileCache extends Cache
/**
* Flushes the entire cache and returns
* whether the operation was successful
*
* @return bool
*/
public function flush(): bool
{
if (Dir::remove($this->root) === true && Dir::make($this->root) === true) {
if (
Dir::remove($this->root) === true &&
Dir::make($this->root) === true
) {
return true;
}

View file

@ -16,10 +16,14 @@ use Memcached as MemcachedExt;
class MemCached extends Cache
{
/**
* store for the memcache connection
* @var \Memcached
* Store for the memcache connection
*/
protected $connection;
protected MemcachedExt $connection;
/**
* Stores whether the connection was successful
*/
protected bool $enabled;
/**
* Sets all parameters which are needed to connect to Memcached
@ -39,7 +43,19 @@ class MemCached extends Cache
parent::__construct(array_merge($defaults, $options));
$this->connection = new MemcachedExt();
$this->connection->addServer($this->options['host'], $this->options['port']);
$this->enabled = $this->connection->addServer(
$this->options['host'],
$this->options['port']
);
}
/**
* Returns whether the cache is ready to
* store values
*/
public function enabled(): bool
{
return $this->enabled;
}
/**
@ -50,35 +66,28 @@ class MemCached extends Cache
* // put an item in the cache for 15 minutes
* $cache->set('value', 'my value', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return bool
*/
public function set(string $key, $value, int $minutes = 0): bool
{
return $this->connection->set($this->key($key), (new Value($value, $minutes))->toJson(), $this->expiration($minutes));
$key = $this->key($key);
$value = (new Value($value, $minutes))->toJson();
$expires = $this->expiration($minutes);
return $this->connection->set($key, $value, $expires);
}
/**
* Internal method to retrieve the raw cache value;
* needs to return a Value object or null if not found
*
* @param string $key
* @return \Kirby\Cache\Value|null
*/
public function retrieve(string $key)
public function retrieve(string $key): Value|null
{
return Value::fromJson($this->connection->get($this->key($key)));
$value = $this->connection->get($this->key($key));
return Value::fromJson($value);
}
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
@ -89,8 +98,6 @@ class MemCached extends Cache
* Flushes the entire cache and returns
* whether the operation was successful;
* WARNING: Memcached only supports flushing the whole cache at once!
*
* @return bool
*/
public function flush(): bool
{

View file

@ -15,9 +15,17 @@ class MemoryCache extends Cache
{
/**
* Cache data
* @var array
*/
protected $store = [];
protected array $store = [];
/**
* Returns whether the cache is ready to
* store values
*/
public function enabled(): bool
{
return true;
}
/**
* Writes an item to the cache for a given number of minutes and
@ -27,11 +35,6 @@ class MemoryCache extends Cache
* // put an item in the cache for 15 minutes
* $cache->set('value', 'my value', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return bool
*/
public function set(string $key, $value, int $minutes = 0): bool
{
@ -42,11 +45,8 @@ class MemoryCache extends Cache
/**
* Internal method to retrieve the raw cache value;
* needs to return a Value object or null if not found
*
* @param string $key
* @return \Kirby\Cache\Value|null
*/
public function retrieve(string $key)
public function retrieve(string $key): Value|null
{
return $this->store[$key] ?? null;
}
@ -54,25 +54,20 @@ class MemoryCache extends Cache
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
if (isset($this->store[$key])) {
unset($this->store[$key]);
return true;
} else {
return false;
}
return false;
}
/**
* Flushes the entire cache and returns
* whether the operation was successful
*
* @return bool
*/
public function flush(): bool
{

View file

@ -13,6 +13,15 @@ namespace Kirby\Cache;
*/
class NullCache extends Cache
{
/**
* Returns whether the cache is ready to
* store values
*/
public function enabled(): bool
{
return false;
}
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
@ -21,11 +30,6 @@ class NullCache extends Cache
* // put an item in the cache for 15 minutes
* $cache->set('value', 'my value', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return bool
*/
public function set(string $key, $value, int $minutes = 0): bool
{
@ -35,11 +39,8 @@ class NullCache extends Cache
/**
* Internal method to retrieve the raw cache value;
* needs to return a Value object or null if not found
*
* @param string $key
* @return \Kirby\Cache\Value|null
*/
public function retrieve(string $key)
public function retrieve(string $key): Value|null
{
return null;
}
@ -47,9 +48,6 @@ class NullCache extends Cache
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
@ -59,8 +57,6 @@ class NullCache extends Cache
/**
* Flushes the entire cache and returns
* whether the operation was successful
*
* @return bool
*/
public function flush(): bool
{

View file

@ -19,7 +19,6 @@ class Value
{
/**
* Cached value
* @var mixed
*/
protected $value;
@ -27,35 +26,30 @@ class Value
* the number of minutes until the value expires
* @todo Rename this property to $expiry to reflect
* both minutes and absolute timestamps
* @var int
*/
protected $minutes;
protected int $minutes;
/**
* Creation timestamp
* @var int
*/
protected $created;
protected int $created;
/**
* Constructor
*
* @param mixed $value
* @param int $minutes the number of minutes until the value expires
* or an absolute UNIX timestamp
* @param int $created the UNIX timestamp when the value has been created
*/
public function __construct($value, int $minutes = 0, int $created = null)
public function __construct($value, int $minutes = 0, int|null $created = null)
{
$this->value = $value;
$this->minutes = $minutes ?? 0;
$this->minutes = $minutes;
$this->created = $created ?? time();
}
/**
* Returns the creation date as UNIX timestamp
*
* @return int
*/
public function created(): int
{
@ -65,10 +59,8 @@ class Value
/**
* Returns the expiration date as UNIX timestamp or
* null if the value never expires
*
* @return int|null
*/
public function expires(): ?int
public function expires(): int|null
{
// 0 = keep forever
if ($this->minutes === 0) {
@ -85,41 +77,37 @@ class Value
/**
* Creates a value object from an array
*
* @param array $array
* @return static
*/
public static function fromArray(array $array)
public static function fromArray(array $array): static
{
return new static($array['value'] ?? null, $array['minutes'] ?? 0, $array['created'] ?? null);
return new static(
$array['value'] ?? null,
$array['minutes'] ?? 0,
$array['created'] ?? null
);
}
/**
* Creates a value object from a JSON string;
* returns null on error
*
* @param string $json
* @return static|null
*/
public static function fromJson(string $json)
public static function fromJson(string $json): static|null
{
try {
$array = json_decode($json, true);
if (is_array($array)) {
if (is_array($array) === true) {
return static::fromArray($array);
} else {
return null;
}
} catch (Throwable $e) {
return null;
} catch (Throwable) {
return null;
}
}
/**
* Converts the object to a JSON string
*
* @return string
*/
public function toJson(): string
{
@ -128,8 +116,6 @@ class Value
/**
* Converts the object to an array
*
* @return array
*/
public function toArray(): array
{
@ -142,8 +128,6 @@ class Value
/**
* Returns the pure value
*
* @return mixed
*/
public function value()
{