Skip to content

Commit

Permalink
Merge branch '1.4' of https://github.com/getgrav/grav into 1.4
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
rhukster committed Jan 19, 2018
2 parents 758ea8f + 8d39fdf commit dcbb5ef
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 34 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
* Made `modular` blueprint more flexible
* Objects: Add protected function `getElement()` to get serialized value for a single property
* `ObjectPropertyTrait`: Added protected functions `isPropertyLoaded()`, `offsetLoad()`, `offsetPrepare()` and `offsetSerialize()`
* `Grav\Framework\Cache`: Allow unlimited TTL
* Slight modification of Whoops error colors
* Updated vendor libs to latest
1. [](#bugfix)
* Date ordering should always be numeric [#1810](https://github.com/getgrav/grav/issues/1810)



# v1.4.0-beta.3
## 12/29/2017

Expand Down
2 changes: 2 additions & 0 deletions system/src/Grav/Common/Markdown/ParsedownExtra.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ class ParsedownExtra extends \ParsedownExtra
*
* @param $page
* @param $defaults
* @throws \Exception
*/
public function __construct($page, $defaults)
{
parent::__construct();

$this->init($page, $defaults);
}
}
32 changes: 28 additions & 4 deletions system/src/Grav/Framework/Cache/Adapter/ChainCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Grav\Framework\Cache\AbstractCache;
use Grav\Framework\Cache\CacheInterface;
use Grav\Framework\Cache\Exception\InvalidArgumentException;

/**
* Cache class for PSR-16 compatible "Simple Cache" implementation using chained cache adapters.
Expand All @@ -32,19 +33,19 @@ class ChainCache extends AbstractCache
* Chain Cache constructor.
* @param array $caches
* @param null|int|\DateInterval $defaultLifetime
* @throws \InvalidArgumentException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function __construct(array $caches, $defaultLifetime = null)
{
parent::__construct('', $defaultLifetime);

if (!$caches) {
throw new \InvalidArgumentException('At least one cache must be specified');
throw new InvalidArgumentException('At least one cache must be specified');
}

foreach ($caches as $cache) {
if (!$cache instanceof CacheInterface) {
throw new \InvalidArgumentException(
throw new InvalidArgumentException(
sprintf(
"The class '%s' does not implement the '%s' interface",
get_class($cache),
Expand All @@ -58,6 +59,9 @@ public function __construct(array $caches, $defaultLifetime = null)
$this->count = count($caches);
}

/**
* @inheritdoc
*/
public function doGet($key, $miss)
{
foreach ($this->caches as $i => $cache) {
Expand All @@ -75,6 +79,9 @@ public function doGet($key, $miss)
return $miss;
}

/**
* @inheritdoc
*/
public function doSet($key, $value, $ttl)
{
$success = true;
Expand All @@ -87,6 +94,9 @@ public function doSet($key, $value, $ttl)
return $success;
}

/**
* @inheritdoc
*/
public function doDelete($key)
{
$success = true;
Expand All @@ -99,6 +109,9 @@ public function doDelete($key)
return $success;
}

/**
* @inheritdoc
*/
public function doClear()
{
$success = true;
Expand All @@ -110,7 +123,9 @@ public function doClear()
return $success;
}


/**
* @inheritdoc
*/
public function doGetMultiple($keys, $miss)
{
$list = [];
Expand All @@ -136,6 +151,9 @@ public function doGetMultiple($keys, $miss)
return $values;
}

/**
* @inheritdoc
*/
public function doSetMultiple($values, $ttl)
{
$success = true;
Expand All @@ -148,6 +166,9 @@ public function doSetMultiple($values, $ttl)
return $success;
}

/**
* @inheritdoc
*/
public function doDeleteMultiple($keys)
{
$success = true;
Expand All @@ -160,6 +181,9 @@ public function doDeleteMultiple($keys)
return $success;
}

/**
* @inheritdoc
*/
public function doHas($key)
{
foreach ($this->caches as $cache) {
Expand Down
27 changes: 27 additions & 0 deletions system/src/Grav/Framework/Cache/Adapter/DoctrineCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Doctrine\Common\Cache\CacheProvider;
use Grav\Framework\Cache\AbstractCache;
use Grav\Framework\Cache\Exception\InvalidArgumentException;

/**
* Cache class for PSR-16 compatible "Simple Cache" implementation using Doctrine Cache backend.
Expand All @@ -28,6 +29,7 @@ class DoctrineCache extends AbstractCache
* @param CacheProvider $doctrineCache
* @param string $namespace
* @param null|int|\DateInterval $defaultLifetime
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function __construct(CacheProvider $doctrineCache, $namespace = '', $defaultLifetime = null)
{
Expand All @@ -41,6 +43,9 @@ public function __construct(CacheProvider $doctrineCache, $namespace = '', $defa
$this->driver = $doctrineCache;
}

/**
* @inheritdoc
*/
public function doGet($key, $miss)
{
$value = $this->driver->fetch($key);
Expand All @@ -49,31 +54,50 @@ public function doGet($key, $miss)
return $value !== false || $this->driver->contains($key) ? $value : $miss;
}

/**
* @inheritdoc
*/
public function doSet($key, $value, $ttl)
{
return $this->driver->save($key, $value, (int) $ttl);
}

/**
* @inheritdoc
*/
public function doDelete($key)
{
return $this->driver->delete($key);
}

/**
* @inheritdoc
*/
public function doClear()
{
return $this->driver->deleteAll();
}

/**
* @inheritdoc
*/
public function doGetMultiple($keys, $miss)
{
return $this->driver->fetchMultiple($keys);
}

/**
* @inheritdoc
*/
public function doSetMultiple($values, $ttl)
{
return $this->driver->saveMultiple($values, (int) $ttl);
}

/**
* @inheritdoc
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function doDeleteMultiple($keys)
{
// TODO: Remove when Doctrine Cache has been updated to support the feature.
Expand All @@ -89,6 +113,9 @@ public function doDeleteMultiple($keys)
return $this->driver->deleteMultiple($keys);
}

/**
* @inheritdoc
*/
public function doHas($key)
{
return $this->driver->contains($key);
Expand Down
45 changes: 44 additions & 1 deletion system/src/Grav/Framework/Cache/Adapter/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,26 @@
/**
* Cache class for PSR-16 compatible "Simple Cache" implementation using file backend.
*
* Defaults to 1 year TTL. Does not support unlimited TTL.
*
* @package Grav\Framework\Cache
*/
class FileCache extends AbstractCache
{
private $directory;
private $tmp;

/**
* @inheritdoc
*/
public function __construct($namespace = '', $defaultLifetime = null)
{
parent::__construct($namespace, $defaultLifetime ?: 31557600); // = 1 year
}

/**
* @inheritdoc
*/
public function doGet($key, $miss)
{
$now = time();
Expand All @@ -47,9 +60,13 @@ public function doGet($key, $miss)
return $miss;
}

/**
* @inheritdoc
* @throws \Psr\SimpleCache\CacheException
*/
public function doSet($key, $value, $ttl)
{
$expiresAt = time() + ($ttl ?: 31557600); // = 1 year
$expiresAt = time() + (int)$ttl;

$result = $this->write(
$this->getFile($key, true),
Expand All @@ -64,13 +81,19 @@ public function doSet($key, $value, $ttl)
return $result;
}

/**
* @inheritdoc
*/
public function doDelete($key)
{
$file = $this->getFile($key);

return (!file_exists($file) || @unlink($file) || !file_exists($file));
}

/**
* @inheritdoc
*/
public function doClear()
{
$result = true;
Expand All @@ -83,13 +106,21 @@ public function doClear()
return $result;
}

/**
* @inheritdoc
*/
public function doHas($key)
{
$file = $this->getFile($key);

return file_exists($file) && (@filemtime($file) > time() || $this->doGet($key, null));
}

/**
* @param string $key
* @param bool $mkdir
* @return string
*/
protected function getFile($key, $mkdir = false)
{
$hash = str_replace('/', '-', base64_encode(hash('sha256', static::class . $key, true)));
Expand All @@ -102,6 +133,11 @@ protected function getFile($key, $mkdir = false)
return $dir . substr($hash, 2, 20);
}

/**
* @param string $namespace
* @param string $directory
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
private function init($namespace, $directory)
{
if (!isset($directory[0])) {
Expand Down Expand Up @@ -129,6 +165,12 @@ private function init($namespace, $directory)
$this->directory = $directory;
}

/**
* @param string $file
* @param string $data
* @param int|null $expiresAt
* @return bool
*/
private function write($file, $data, $expiresAt = null)
{
set_error_handler(__CLASS__.'::throwError');
Expand All @@ -152,6 +194,7 @@ private function write($file, $data, $expiresAt = null)

/**
* @internal
* @throws \ErrorException
*/
public static function throwError($type, $message, $file, $line)
{
Expand Down
9 changes: 1 addition & 8 deletions system/src/Grav/Framework/Cache/Adapter/MemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* Cache class for PSR-16 compatible "Simple Cache" implementation using in memory backend.
*
* Memory backend does not use namespace or default ttl as the cache is unique to each cache object and request.
*
* @package Grav\Framework\Cache
Expand All @@ -23,14 +24,6 @@ class MemoryCache extends AbstractCache
*/
protected $cache = [];

/**
* Memory Cache constructor.
*/
public function __construct()
{
parent::__construct('', 300);
}

public function doGet($key, $miss)
{
if (!array_key_exists($key, $this->cache)) {
Expand Down
12 changes: 9 additions & 3 deletions system/src/Grav/Framework/Cache/Adapter/SessionCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public function doGet($key, $miss)

public function doSet($key, $value, $ttl)
{
$_SESSION[$this->getNamespace()][$key] = [self::VALUE => $value, self::LIFETIME => time() + $ttl];
$stored = [self::VALUE => $value];
if (null !== $ttl) {
$stored[self::LIFETIME] = time() + $ttl;

}

$_SESSION[$this->getNamespace()][$key] = $stored;

return true;
}
Expand All @@ -43,7 +49,7 @@ public function doDelete($key)

public function doClear()
{
$_SESSION[$this->getNamespace()] = [];
unset($_SESSION[$this->getNamespace()]);

return true;
}
Expand All @@ -62,7 +68,7 @@ protected function doGetStored($key)
{
$stored = isset($_SESSION[$this->getNamespace()][$key]) ? $_SESSION[$this->getNamespace()][$key] : null;

if ($stored && $stored[self::LIFETIME] < time()) {
if (isset($stored[self::LIFETIME]) && $stored[self::LIFETIME] < time()) {
unset($_SESSION[$this->getNamespace()][$key]);
$stored = null;
}
Expand Down
Loading

0 comments on commit dcbb5ef

Please sign in to comment.