From 2afe33ff7efef138a5f644a2e516352e02f7d5eb Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@googlemail.com>
Date: Thu, 3 Jul 2014 05:47:49 +0200
Subject: [PATCH] this is a better solution to the previous committed fix. This
 solution works in all cases (even when not running tests) and makes it
 useable for other CacheFile classes as well. In addition it keeps the
 dependencies in the PersistenceCache class

---
 core/Cache/PersistentCache.php |  5 ++++-
 core/CacheFile.php             | 16 ++++++++++++++++
 tests/PHPUnit/Fixture.php      |  1 -
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/core/Cache/PersistentCache.php b/core/Cache/PersistentCache.php
index cd695bde2d..2596b950a5 100644
--- a/core/Cache/PersistentCache.php
+++ b/core/Cache/PersistentCache.php
@@ -96,7 +96,7 @@ class PersistentCache
 
     public static function _reset()
     {
-        self::$content = null;
+        self::$content = array();
     }
 
     /**
@@ -106,6 +106,9 @@ class PersistentCache
     {
         if (is_null(self::$storage)) {
             self::$storage = new CacheFile('tracker', 43200);
+            self::$storage->addOnDeleteCallback(function () {
+                PersistentCache::_reset();
+            });
         }
 
         return self::$storage;
diff --git a/core/CacheFile.php b/core/CacheFile.php
index 94ad253020..7a1c824ff8 100644
--- a/core/CacheFile.php
+++ b/core/CacheFile.php
@@ -36,6 +36,11 @@ class CacheFile
      */
     const MINIMUM_TTL = 60;
 
+    /**
+     * @var \Callable[]
+     */
+    private static $onDeleteCallback = array();
+
     /**
      * @param string $directory directory to use
      * @param int $timeToLiveInSeconds TTL
@@ -182,6 +187,11 @@ class CacheFile
         return false;
     }
 
+    public function addOnDeleteCallback($onDeleteCallback)
+    {
+        self::$onDeleteCallback[] = $onDeleteCallback;
+    }
+
     /**
      * A function to delete all cache entries in the directory
      */
@@ -193,6 +203,12 @@ class CacheFile
         };
 
         Filesystem::unlinkRecursive($this->cachePath, $deleteRootToo = false, $beforeUnlink);
+
+        if (!empty(self::$onDeleteCallback)) {
+            foreach (self::$onDeleteCallback as $callback) {
+                $callback();
+            }
+        }
     }
 
     public function opCacheInvalidate($filepath)
diff --git a/tests/PHPUnit/Fixture.php b/tests/PHPUnit/Fixture.php
index cb5fac1035..22b09767a3 100644
--- a/tests/PHPUnit/Fixture.php
+++ b/tests/PHPUnit/Fixture.php
@@ -179,7 +179,6 @@ class Fixture extends PHPUnit_Framework_Assert
         Piwik::setUserHasSuperUserAccess();
 
         Cache::deleteTrackerCache();
-        \Piwik\Cache\PersistentCache::_reset();
 
         static::loadAllPlugins($this->getTestEnvironment(), $this->testCaseClass);
 
-- 
GitLab