Upgrade to 3.2.0

This commit is contained in:
Bastian Allgeier
2019-06-25 09:56:08 +02:00
parent 9e18cf635d
commit 9c89153d35
296 changed files with 14408 additions and 2504 deletions

View File

@@ -2,7 +2,6 @@
namespace Kirby\Cache;
use Exception;
use Kirby\Toolkit\Dir;
use Kirby\Toolkit\F;
@@ -11,80 +10,107 @@ use Kirby\Toolkit\F;
*
* @package Kirby Cache
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license MIT
* @link https://getkirby.com
* @copyright Bastian Allgeier GmbH
* @license https://opensource.org/licenses/MIT
*/
class FileCache extends Cache
{
/**
* Set all parameters which are needed for the file cache
* see defaults for available parameters
*
* @param array $params
* Full root including prefix
* @var string
*/
public function __construct(array $params)
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
];
parent::__construct(array_merge($defaults, $params));
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'];
}
// try to create the directory
Dir::make($this->options['root'], true);
Dir::make($this->root, true);
}
/**
* Returns the full path to a file for a given key
*
* @param string $key
* @param string $key
* @return string
*/
protected function file(string $key): string
{
$extension = isset($this->options['extension']) ? '.' . $this->options['extension'] : '';
$file = $this->root . '/' . $key;
return $this->options['root'] . '/' . $this->key($key) . $extension;
if (isset($this->options['extension'])) {
return $file . '.' . $this->options['extension'];
} else {
return $file;
}
}
/**
* Write an item to the cache for a given number of 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);
* // 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
* @param string $key
* @param mixed $value
* @param int $minutes
* @return boolean
*/
public function set(string $key, $value, int $minutes = 0)
public function set(string $key, $value, int $minutes = 0): bool
{
return F::write($this->file($key), $this->value($value, $minutes)->toJson());
$file = $this->file($key);
return F::write($file, (new Value($value, $minutes))->toJson());
}
/**
* Retrieve an item from the cache.
* Internal method to retrieve the raw cache value;
* needs to return a Value object or null if not found
*
* @param string $key
* @return mixed
* @param string $key
* @return Kirby\Cache\Value|null
*/
public function retrieve(string $key)
{
return Value::fromJson(F::read($this->file($key)));
$file = $this->file($key);
return Value::fromJson(F::read($file));
}
/**
* Checks when the cache has been created
* 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
* @return mixed
*/
public function created(string $key): int
public function created(string $key)
{
// use the modification timestamp
// as indicator when the cache has been created/overwritten
@@ -92,37 +118,39 @@ class FileCache extends Cache
// get the file for this cache key
$file = $this->file($key);
return file_exists($file) ? filemtime($this->file($key)) : 0;
return file_exists($file) ? filemtime($this->file($key)) : false;
}
/**
* Remove an item from the cache
* Removes an item from the cache and returns
* whether the operation was successful
*
* @param string $key
* @param string $key
* @return boolean
*/
public function remove(string $key): bool
{
return F::remove($this->file($key));
$file = $this->file($key);
if (is_file($file) === true) {
return F::remove($file);
} else {
return false;
}
}
/**
* Flush the entire cache directory
* Flushes the entire cache and returns
* whether the operation was successful
*
* @return boolean
*/
public function flush(): bool
{
$root = $this->options['root'];
if (empty($this->options['prefix']) === false) {
$root = $root . '/' . $this->options['prefix'];
}
if (Dir::remove($root) === true && Dir::make($root) === true) {
if (Dir::remove($this->root) === true && Dir::make($this->root) === true) {
return true;
}
return false;
return false; // @codeCoverageIgnore
}
}