From 627a1510dcdbef486e5b02a259631cd525be89d3 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Tue, 7 Dec 2021 12:52:42 +0200 Subject: [PATCH] Simplify exception handling for Framework Cache classses --- .../Grav/Framework/Cache/Adapter/ChainCache.php | 8 ++++++-- .../Framework/Cache/Adapter/DoctrineCache.php | 8 ++++++-- .../Grav/Framework/Cache/Adapter/FileCache.php | 16 +++++++++++++--- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/system/src/Grav/Framework/Cache/Adapter/ChainCache.php b/system/src/Grav/Framework/Cache/Adapter/ChainCache.php index 76f518541..a5f2f582e 100644 --- a/system/src/Grav/Framework/Cache/Adapter/ChainCache.php +++ b/system/src/Grav/Framework/Cache/Adapter/ChainCache.php @@ -33,11 +33,15 @@ class ChainCache extends AbstractCache * Chain Cache constructor. * @param array $caches * @param null|int|DateInterval $defaultLifetime - * @throws \Psr\SimpleCache\InvalidArgumentException|InvalidArgumentException + * @throws InvalidArgumentException */ public function __construct(array $caches, $defaultLifetime = null) { - parent::__construct('', $defaultLifetime); + try { + parent::__construct('', $defaultLifetime); + } catch (\Psr\SimpleCache\InvalidArgumentException $e) { + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); + } if (!$caches) { throw new InvalidArgumentException('At least one cache must be specified'); diff --git a/system/src/Grav/Framework/Cache/Adapter/DoctrineCache.php b/system/src/Grav/Framework/Cache/Adapter/DoctrineCache.php index 29e9e3b52..26819d89b 100644 --- a/system/src/Grav/Framework/Cache/Adapter/DoctrineCache.php +++ b/system/src/Grav/Framework/Cache/Adapter/DoctrineCache.php @@ -29,12 +29,16 @@ class DoctrineCache extends AbstractCache * @param CacheProvider $doctrineCache * @param string $namespace * @param null|int|DateInterval $defaultLifetime - * @throws \Psr\SimpleCache\InvalidArgumentException|InvalidArgumentException + * @throws InvalidArgumentException */ public function __construct(CacheProvider $doctrineCache, $namespace = '', $defaultLifetime = null) { // Do not use $namespace or $defaultLifetime directly, store them with constructor and fetch with methods. - parent::__construct($namespace, $defaultLifetime); + try { + parent::__construct($namespace, $defaultLifetime); + } catch (\Psr\SimpleCache\InvalidArgumentException $e) { + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); + } // Set namespace to Doctrine Cache provider if it was given. $namespace = $this->getNamespace(); diff --git a/system/src/Grav/Framework/Cache/Adapter/FileCache.php b/system/src/Grav/Framework/Cache/Adapter/FileCache.php index 940a7ae60..69a112772 100644 --- a/system/src/Grav/Framework/Cache/Adapter/FileCache.php +++ b/system/src/Grav/Framework/Cache/Adapter/FileCache.php @@ -42,9 +42,13 @@ class FileCache extends AbstractCache */ public function __construct($namespace = '', $defaultLifetime = null, $folder = null) { - parent::__construct($namespace, $defaultLifetime ?: 31557600); // = 1 year + try { + parent::__construct($namespace, $defaultLifetime ?: 31557600); // = 1 year - $this->initFileCache($namespace, $folder ?? ''); + $this->initFileCache($namespace, $folder ?? ''); + } catch (\Psr\SimpleCache\InvalidArgumentException $e) { + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); + } } /** @@ -103,7 +107,13 @@ class FileCache extends AbstractCache { $file = $this->getFile($key); - return (!file_exists($file) || @unlink($file) || !file_exists($file)); + $result = false; + if (file_exists($file)) { + $result = @unlink($file); + $result &= !file_exists($file); + } + + return $result; } /**