Update Kirby and dependencies

This commit is contained in:
Paul Nicoué 2022-08-31 15:02:43 +02:00
parent 503b339974
commit 399fa20902
439 changed files with 66915 additions and 64442 deletions

View file

@ -15,72 +15,72 @@ use APCUIterator;
*/
class ApcuCache extends Cache
{
/**
* Determines if an item exists in the cache
*
* @param string $key
* @return bool
*/
public function exists(string $key): bool
{
return apcu_exists($this->key($key));
}
/**
* Determines if an item exists in the cache
*
* @param string $key
* @return bool
*/
public function exists(string $key): bool
{
return apcu_exists($this->key($key));
}
/**
* 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();
}
}
/**
* 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();
}
}
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
return apcu_delete($this->key($key));
}
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
return apcu_delete($this->key($key));
}
/**
* 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)
{
return Value::fromJson(apcu_fetch($this->key($key)));
}
/**
* 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)
{
return Value::fromJson(apcu_fetch($this->key($key)));
}
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*
* <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
*/
public function set(string $key, $value, int $minutes = 0): bool
{
return apcu_store($this->key($key), (new Value($value, $minutes))->toJson(), $this->expiration($minutes));
}
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*
* <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
*/
public function set(string $key, $value, int $minutes = 0): bool
{
return apcu_store($this->key($key), (new Value($value, $minutes))->toJson(), $this->expiration($minutes));
}
}

View file

@ -16,227 +16,227 @@ namespace Kirby\Cache;
*/
abstract class Cache
{
/**
* Stores all options for the driver
* @var array
*/
protected $options = [];
/**
* Stores all options for the driver
* @var array
*/
protected $options = [];
/**
* Sets all parameters which are needed to connect to the cache storage
*
* @param array $options
*/
public function __construct(array $options = [])
{
$this->options = $options;
}
/**
* Sets all parameters which are needed to connect to the cache storage
*
* @param array $options
*/
public function __construct(array $options = [])
{
$this->options = $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
*
* <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
*/
abstract public function set(string $key, $value, int $minutes = 0): bool;
/**
* 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
*/
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
{
if (empty($this->options['prefix']) === false) {
$key = $this->options['prefix'] . '/' . $key;
}
/**
* Adds the prefix to the key if given
*
* @param string $key
* @return string
*/
protected function key(string $key): string
{
if (empty($this->options['prefix']) === false) {
$key = $this->options['prefix'] . '/' . $key;
}
return $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);
/**
* 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
$value = $this->retrieve($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
$value = $this->retrieve($key);
// check for a valid cache value
if (!is_a($value, 'Kirby\Cache\Value')) {
return $default;
}
// check for a valid cache value
if (!is_a($value, 'Kirby\Cache\Value')) {
return $default;
}
// remove the item if it is expired
if ($value->expires() > 0 && time() >= $value->expires()) {
$this->remove($key);
return $default;
}
// 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 pure value
return $value->value();
}
/**
* Calculates the expiration timestamp
*
* @param int $minutes
* @return int
*/
protected function expiration(int $minutes = 0): int
{
// 0 = keep forever
if ($minutes === 0) {
return 0;
}
/**
* Calculates the expiration timestamp
*
* @param int $minutes
* @return int
*/
protected function expiration(int $minutes = 0): int
{
// 0 = keep forever
if ($minutes === 0) {
return 0;
}
// calculate the time
return time() + ($minutes * 60);
}
// calculate the time
return time() + ($minutes * 60);
}
/**
* 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)
{
// get the Value object
$value = $this->retrieve($key);
/**
* 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)
{
// get the Value object
$value = $this->retrieve($key);
// check for a valid Value object
if (!is_a($value, 'Kirby\Cache\Value')) {
return false;
}
// check for a valid Value object
if (!is_a($value, 'Kirby\Cache\Value')) {
return false;
}
// return the expires timestamp
return $value->expires();
}
// return the expires timestamp
return $value->expires();
}
/**
* Checks if an item in the cache is expired
*
* @param string $key
* @return bool
*/
public function expired(string $key): bool
{
$expires = $this->expires($key);
/**
* Checks if an item in the cache is expired
*
* @param string $key
* @return bool
*/
public function expired(string $key): bool
{
$expires = $this->expires($key);
if ($expires === null) {
return false;
} elseif (!is_int($expires)) {
return true;
} else {
return time() >= $expires;
}
}
if ($expires === null) {
return false;
} elseif (!is_int($expires)) {
return true;
} else {
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
*/
public function created(string $key)
{
// get the Value object
$value = $this->retrieve($key);
/**
* 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
*/
public function created(string $key)
{
// get the Value object
$value = $this->retrieve($key);
// check for a valid Value object
if (!is_a($value, 'Kirby\Cache\Value')) {
return false;
}
// check for a valid Value object
if (!is_a($value, 'Kirby\Cache\Value')) {
return false;
}
// return the expires timestamp
return $value->created();
}
// return the expires timestamp
return $value->created();
}
/**
* Alternate version for Cache::created($key)
*
* @param string $key
* @return int|false
*/
public function modified(string $key)
{
return static::created($key);
}
/**
* Alternate version for Cache::created($key)
*
* @param string $key
* @return int|false
*/
public function modified(string $key)
{
return static::created($key);
}
/**
* Determines if an item exists in the cache
*
* @param string $key
* @return bool
*/
public function exists(string $key): bool
{
return $this->expired($key) === false;
}
/**
* Determines if an item exists in the cache
*
* @param string $key
* @return bool
*/
public function exists(string $key): bool
{
return $this->expired($key) === false;
}
/**
* 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;
/**
* 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;
* this needs to be defined by the driver
*
* @return bool
*/
abstract public function flush(): bool;
/**
* Flushes the entire cache and returns
* whether the operation was successful;
* this needs to be defined by the driver
*
* @return bool
*/
abstract public function flush(): bool;
/**
* Returns all passed cache options
*
* @return array
*/
public function options(): array
{
return $this->options;
}
/**
* Returns all passed cache options
*
* @return array
*/
public function options(): array
{
return $this->options;
}
}

View file

@ -18,217 +18,217 @@ use Kirby\Toolkit\Str;
*/
class FileCache extends Cache
{
/**
* Full root including prefix
*
* @var string
*/
protected $root;
/**
* Full root including prefix
*
* @var string
*/
protected $root;
/**
* Sets all parameters which are needed for the file cache
*
* @param array $options 'root' (required)
* 'prefix' (default: none)
* 'extension' (file extension for cache files, default: none)
*/
public function __construct(array $options)
{
$defaults = [
'root' => null,
'prefix' => null,
'extension' => null
];
/**
* Sets all parameters which are needed for the file cache
*
* @param array $options 'root' (required)
* 'prefix' (default: none)
* 'extension' (file extension for cache files, default: none)
*/
public function __construct(array $options)
{
$defaults = [
'root' => null,
'prefix' => null,
'extension' => null
];
parent::__construct(array_merge($defaults, $options));
parent::__construct(array_merge($defaults, $options));
// build the full root including prefix
$this->root = $this->options['root'];
if (empty($this->options['prefix']) === false) {
$this->root .= '/' . $this->options['prefix'];
}
// build the full root including prefix
$this->root = $this->options['root'];
if (empty($this->options['prefix']) === false) {
$this->root .= '/' . $this->options['prefix'];
}
// try to create the directory
Dir::make($this->root, true);
}
// try to create the directory
Dir::make($this->root, true);
}
/**
* Returns the full root including prefix
*
* @return string
*/
public function root(): string
{
return $this->root;
}
/**
* Returns the full root including prefix
*
* @return string
*/
public function root(): string
{
return $this->root;
}
/**
* Returns the full path to a file for a given key
*
* @param string $key
* @return string
*/
protected function file(string $key): string
{
// strip out invalid characters in each path segment
// split by slash or backslash
$keyParts = [];
foreach (preg_split('#([\/\\\\])#', $key, 0, PREG_SPLIT_DELIM_CAPTURE) as $part) {
switch ($part) {
// forward slashes don't need special treatment
case '/':
break;
/**
* Returns the full path to a file for a given key
*
* @param string $key
* @return string
*/
protected function file(string $key): string
{
// strip out invalid characters in each path segment
// split by slash or backslash
$keyParts = [];
foreach (preg_split('#([\/\\\\])#', $key, 0, PREG_SPLIT_DELIM_CAPTURE) as $part) {
switch ($part) {
case '/':
// forward slashes don't need special treatment
break;
// backslashes get their own marker in the path
// to differentiate the cache key from one with forward slashes
case '\\':
$keyParts[] = '_backslash';
break;
case '\\':
// backslashes get their own marker in the path
// to differentiate the cache key from one with forward slashes
$keyParts[] = '_backslash';
break;
// empty part means two slashes in a row;
// special marker like for backslashes
case '':
$keyParts[] = '_empty';
break;
case '':
// empty part means two slashes in a row;
// special marker like for backslashes
$keyParts[] = '_empty';
break;
// an actual path segment
default:
// check if the segment only contains safe characters;
// underscores are *not* safe to guarantee uniqueness
// as they are used in the special cases
if (preg_match('/^[a-zA-Z0-9-]+$/', $part) === 1) {
$keyParts[] = $part;
} else {
$keyParts[] = Str::slug($part) . '_' . sha1($part);
}
}
}
default:
// an actual path segment:
// check if the segment only contains safe characters;
// underscores are *not* safe to guarantee uniqueness
// as they are used in the special cases
if (preg_match('/^[a-zA-Z0-9-]+$/', $part) === 1) {
$keyParts[] = $part;
} else {
$keyParts[] = Str::slug($part) . '_' . sha1($part);
}
}
}
$file = $this->root . '/' . implode('/', $keyParts);
$file = $this->root . '/' . implode('/', $keyParts);
if (isset($this->options['extension'])) {
return $file . '.' . $this->options['extension'];
} else {
return $file;
}
}
if (isset($this->options['extension'])) {
return $file . '.' . $this->options['extension'];
} else {
return $file;
}
}
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*
* <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
*/
public function set(string $key, $value, int $minutes = 0): bool
{
$file = $this->file($key);
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*
* <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
*/
public function set(string $key, $value, int $minutes = 0): bool
{
$file = $this->file($key);
return F::write($file, (new Value($value, $minutes))->toJson());
}
return F::write($file, (new Value($value, $minutes))->toJson());
}
/**
* 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)
{
$file = $this->file($key);
$value = F::read($file);
/**
* 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)
{
$file = $this->file($key);
$value = F::read($file);
return $value ? Value::fromJson($value) : null;
}
return $value ? Value::fromJson($value) : null;
}
/**
* 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)
{
// use the modification timestamp
// as indicator when the cache has been created/overwritten
clearstatcache();
/**
* 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)
{
// use the modification timestamp
// as indicator when the cache has been created/overwritten
clearstatcache();
// get the file for this cache key
$file = $this->file($key);
return file_exists($file) ? filemtime($this->file($key)) : false;
}
// get the file for this cache key
$file = $this->file($key);
return file_exists($file) ? filemtime($this->file($key)) : 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
{
$file = $this->file($key);
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
$file = $this->file($key);
if (is_file($file) === true && F::remove($file) === true) {
$this->removeEmptyDirectories(dirname($file));
return true;
}
if (is_file($file) === true && F::remove($file) === true) {
$this->removeEmptyDirectories(dirname($file));
return true;
}
return false;
}
return false;
}
/**
* Removes empty directories safely by checking each directory
* up to the root directory
*
* @param string $dir
* @return void
*/
protected function removeEmptyDirectories(string $dir): void
{
try {
// ensure the path doesn't end with a slash for the next comparison
$dir = rtrim($dir, '/\/');
/**
* Removes empty directories safely by checking each directory
* up to the root directory
*
* @param string $dir
* @return void
*/
protected function removeEmptyDirectories(string $dir): void
{
try {
// ensure the path doesn't end with a slash for the next comparison
$dir = rtrim($dir, '/\/');
// checks all directory segments until reaching the root directory
while (Str::startsWith($dir, $this->root()) === true && $dir !== $this->root()) {
$files = array_diff(scandir($dir) ?? [], ['.', '..']);
// checks all directory segments until reaching the root directory
while (Str::startsWith($dir, $this->root()) === true && $dir !== $this->root()) {
$files = array_diff(scandir($dir) ?? [], ['.', '..']);
if (empty($files) === true && Dir::remove($dir) === true) {
// continue with the next level up
$dir = dirname($dir);
} else {
// no need to continue with the next level up as `$dir` was not deleted
break;
}
}
} catch (Exception $e) { // @codeCoverageIgnore
// silently stops the process
}
}
if (empty($files) === true && Dir::remove($dir) === true) {
// continue with the next level up
$dir = dirname($dir);
} else {
// no need to continue with the next level up as `$dir` was not deleted
break;
}
}
} catch (Exception $e) { // @codeCoverageIgnore
// silently stops the process
}
}
/**
* 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) {
return true;
}
/**
* 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) {
return true;
}
return false; // @codeCoverageIgnore
}
return false; // @codeCoverageIgnore
}
}

View file

@ -15,85 +15,85 @@ use Memcached as MemcachedExt;
*/
class MemCached extends Cache
{
/**
* store for the memcache connection
* @var \Memcached
*/
protected $connection;
/**
* store for the memcache connection
* @var \Memcached
*/
protected $connection;
/**
* Sets all parameters which are needed to connect to Memcached
*
* @param array $options 'host' (default: localhost)
* 'port' (default: 11211)
* 'prefix' (default: null)
*/
public function __construct(array $options = [])
{
$defaults = [
'host' => 'localhost',
'port' => 11211,
'prefix' => null,
];
/**
* Sets all parameters which are needed to connect to Memcached
*
* @param array $options 'host' (default: localhost)
* 'port' (default: 11211)
* 'prefix' (default: null)
*/
public function __construct(array $options = [])
{
$defaults = [
'host' => 'localhost',
'port' => 11211,
'prefix' => null,
];
parent::__construct(array_merge($defaults, $options));
parent::__construct(array_merge($defaults, $options));
$this->connection = new MemcachedExt();
$this->connection->addServer($this->options['host'], $this->options['port']);
}
$this->connection = new MemcachedExt();
$this->connection->addServer($this->options['host'], $this->options['port']);
}
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*
* <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
*/
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));
}
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*
* <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
*/
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));
}
/**
* 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)
{
return Value::fromJson($this->connection->get($this->key($key)));
}
/**
* 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)
{
return Value::fromJson($this->connection->get($this->key($key)));
}
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
return $this->connection->delete($this->key($key));
}
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
return $this->connection->delete($this->key($key));
}
/**
* 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
{
return $this->connection->flush();
}
/**
* 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
{
return $this->connection->flush();
}
}

View file

@ -13,70 +13,70 @@ namespace Kirby\Cache;
*/
class MemoryCache extends Cache
{
/**
* Cache data
* @var array
*/
protected $store = [];
/**
* Cache data
* @var array
*/
protected $store = [];
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*
* <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
*/
public function set(string $key, $value, int $minutes = 0): bool
{
$this->store[$key] = new Value($value, $minutes);
return true;
}
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*
* <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
*/
public function set(string $key, $value, int $minutes = 0): bool
{
$this->store[$key] = new Value($value, $minutes);
return true;
}
/**
* 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)
{
return $this->store[$key] ?? null;
}
/**
* 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)
{
return $this->store[$key] ?? null;
}
/**
* 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;
}
}
/**
* 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;
}
}
/**
* Flushes the entire cache and returns
* whether the operation was successful
*
* @return bool
*/
public function flush(): bool
{
$this->store = [];
return true;
}
/**
* Flushes the entire cache and returns
* whether the operation was successful
*
* @return bool
*/
public function flush(): bool
{
$this->store = [];
return true;
}
}

View file

@ -13,57 +13,57 @@ namespace Kirby\Cache;
*/
class NullCache extends Cache
{
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*
* <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
*/
public function set(string $key, $value, int $minutes = 0): bool
{
return true;
}
/**
* Writes an item to the cache for a given number of minutes and
* returns whether the operation was successful
*
* <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
*/
public function set(string $key, $value, int $minutes = 0): bool
{
return true;
}
/**
* 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)
{
return null;
}
/**
* 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)
{
return null;
}
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
return true;
}
/**
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @return bool
*/
public function remove(string $key): bool
{
return true;
}
/**
* Flushes the entire cache and returns
* whether the operation was successful
*
* @return bool
*/
public function flush(): bool
{
return true;
}
/**
* Flushes the entire cache and returns
* whether the operation was successful
*
* @return bool
*/
public function flush(): bool
{
return true;
}
}

View file

@ -17,136 +17,136 @@ use Throwable;
*/
class Value
{
/**
* Cached value
* @var mixed
*/
protected $value;
/**
* Cached value
* @var mixed
*/
protected $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;
/**
* 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;
/**
* Creation timestamp
* @var int
*/
protected $created;
/**
* Creation timestamp
* @var int
*/
protected $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)
{
$this->value = $value;
$this->minutes = $minutes ?? 0;
$this->created = $created ?? time();
}
/**
* 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)
{
$this->value = $value;
$this->minutes = $minutes ?? 0;
$this->created = $created ?? time();
}
/**
* Returns the creation date as UNIX timestamp
*
* @return int
*/
public function created(): int
{
return $this->created;
}
/**
* Returns the creation date as UNIX timestamp
*
* @return int
*/
public function created(): int
{
return $this->created;
}
/**
* Returns the expiration date as UNIX timestamp or
* null if the value never expires
*
* @return int|null
*/
public function expires(): ?int
{
// 0 = keep forever
if ($this->minutes === 0) {
return null;
}
/**
* Returns the expiration date as UNIX timestamp or
* null if the value never expires
*
* @return int|null
*/
public function expires(): ?int
{
// 0 = keep forever
if ($this->minutes === 0) {
return null;
}
if ($this->minutes > 1000000000) {
// absolute timestamp
return $this->minutes;
}
if ($this->minutes > 1000000000) {
// absolute timestamp
return $this->minutes;
}
return $this->created + ($this->minutes * 60);
}
return $this->created + ($this->minutes * 60);
}
/**
* Creates a value object from an array
*
* @param array $array
* @return static
*/
public static function fromArray(array $array)
{
return new static($array['value'] ?? null, $array['minutes'] ?? 0, $array['created'] ?? null);
}
/**
* Creates a value object from an array
*
* @param array $array
* @return static
*/
public static function fromArray(array $array)
{
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)
{
try {
$array = json_decode($json, true);
/**
* Creates a value object from a JSON string;
* returns null on error
*
* @param string $json
* @return static|null
*/
public static function fromJson(string $json)
{
try {
$array = json_decode($json, true);
if (is_array($array)) {
return static::fromArray($array);
} else {
return null;
}
} catch (Throwable $e) {
return null;
}
}
if (is_array($array)) {
return static::fromArray($array);
} else {
return null;
}
} catch (Throwable $e) {
return null;
}
}
/**
* Converts the object to a JSON string
*
* @return string
*/
public function toJson(): string
{
return json_encode($this->toArray());
}
/**
* Converts the object to a JSON string
*
* @return string
*/
public function toJson(): string
{
return json_encode($this->toArray());
}
/**
* Converts the object to an array
*
* @return array
*/
public function toArray(): array
{
return [
'created' => $this->created,
'minutes' => $this->minutes,
'value' => $this->value,
];
}
/**
* Converts the object to an array
*
* @return array
*/
public function toArray(): array
{
return [
'created' => $this->created,
'minutes' => $this->minutes,
'value' => $this->value,
];
}
/**
* Returns the pure value
*
* @return mixed
*/
public function value()
{
return $this->value;
}
/**
* Returns the pure value
*
* @return mixed
*/
public function value()
{
return $this->value;
}
}