diff --git a/core/lib/Drupal/Component/FileCache/FileCache.php b/core/lib/Drupal/Component/FileCache/FileCache.php index 761d490..f0c39f2 100644 --- a/core/lib/Drupal/Component/FileCache/FileCache.php +++ b/core/lib/Drupal/Component/FileCache/FileCache.php @@ -68,6 +68,13 @@ public function __construct($prefix, $collection, $cache_backend_class = NULL, a } /** + * Destruct a FileCache object. + */ + public function __destruct() { + unset($this->cache); + } + + /** * {@inheritdoc} */ public function get($filepath) { diff --git a/core/lib/Drupal/Component/FileCache/FileCacheFactory.php b/core/lib/Drupal/Component/FileCache/FileCacheFactory.php index 2397d1b..5a412e8 100644 --- a/core/lib/Drupal/Component/FileCache/FileCacheFactory.php +++ b/core/lib/Drupal/Component/FileCache/FileCacheFactory.php @@ -27,6 +27,13 @@ class FileCacheFactory { protected static $prefix; /** + * The list of file cache collections. + * + * @var array + */ + protected static $collections = []; + + /** * Instantiates a FileCache object for a given collection identifier. * * @param string $collection @@ -58,7 +65,11 @@ public static function get($collection, $default_configuration = []) { $configuration = $configuration + $default_configuration; $class = $configuration['class']; - return new $class(static::getPrefix(), $configuration['collection'], $configuration['cache_backend_class'], $configuration['cache_backend_configuration']); + + // Keep a list of references to FileCache objects. + static::$collections[$collection] = new $class(static::getPrefix(), $configuration['collection'], $configuration['cache_backend_class'], $configuration['cache_backend_configuration']); + + return static::$collections[$collection]; } /** @@ -104,4 +115,25 @@ public static function setPrefix($prefix) { static::$prefix = $prefix; } + /** + * Destroys a file cache object. + * + * @param string $collection + * The collection identifier for this FileCache. + */ + public static function destroyCollection($collection) { + if (!empty(static::$collections[$collection])) { + static::$collections[$collection]->__destruct(); + } + } + + /** + * Destroys all file cache collections. + */ + public static function destroy() { + array_walk(static::$collections, function ($collection) { + static::destroyCollection($collection); + }); + } + } diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index 0db2274..f2914b8 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -654,6 +654,9 @@ protected function tearDown() { } } + // Clean FileCache cache. + FileCacheFactory::destroy(); + // Clean up statics, container, and settings. if (function_exists('drupal_static_reset')) { drupal_static_reset();