diff --git a/core/lib/Drupal/Component/FileCache/ApcuFileCacheBackend.php b/core/lib/Drupal/Component/FileCache/ApcuFileCacheBackend.php index 88f864a..25b6097 100644 --- a/core/lib/Drupal/Component/FileCache/ApcuFileCacheBackend.php +++ b/core/lib/Drupal/Component/FileCache/ApcuFileCacheBackend.php @@ -8,7 +8,7 @@ namespace Drupal\Component\FileCache; /** - * Allows to cache data based on file modification dates in APCu. + * APCu backend for the file cache. */ class ApcuFileCacheBackend implements FileCacheBackendInterface { diff --git a/core/lib/Drupal/Component/FileCache/FileCache.php b/core/lib/Drupal/Component/FileCache/FileCache.php index 72e05c6..99b6a1a 100644 --- a/core/lib/Drupal/Component/FileCache/FileCache.php +++ b/core/lib/Drupal/Component/FileCache/FileCache.php @@ -10,7 +10,7 @@ /** * Allows to cache data based on file modification dates. */ -class FileCache { +class FileCache implements FileCacheInterface { /** * Prefix that is used for cache entries. @@ -31,11 +31,11 @@ class FileCache { protected static $cached = []; /** - * The identifier of this cache. + * The collection identifier of this cache. * * @var string */ - protected $identifier; + protected $collection; /** * The cache backend backing this FileCache object. @@ -47,39 +47,24 @@ class FileCache { /** * Constructs a FileCache object. * - * @param string $identifier - * An identifier to ensure that the same files could be cached for different - * purposes without clashing. - * @param NULL|string|array $cache_backend_class - * (optional) The class that should be used as cache backend. This can be - * an array('class' => $configuration) data structure or a simple string. + * @param string $collection + * A collection identifier to ensure that the same files could be cached for + * different purposes without clashing. + * @param string|null $cache_backend_class + * (optional) The class that should be used as cache backend. + * @param array $cache_backend_configuration + * (optional) The configuration for the backend class. */ - public function __construct($identifier, $cache_backend_class = NULL) { - $this->identifier = $identifier; + public function __construct($collection, $cache_backend_class = NULL, array $cache_backend_configuration = []) { + $this->collection = $collection; if (isset($cache_backend_class)) { - $class = $cache_backend_class; - $configuration = []; - - // Support providing of configuration as an array. - if (is_array($cache_backend_class)) { - $class = array_keys($cache_backend_class)[0]; - $configuration = array_shift($cache_backend_class); - } - - $this->cache = new $class($configuration); + $this->cache = new $cache_backend_class($cache_backend_configuration); } } /** - * Gets data based on a filename. - * - * @param string $filepath - * Name of the cache that the cached data is based on. - * - * @return mixed|null - * The data that was persisted with set() or NULL if there is no data - * or the file has been modified. + * {@inheritdoc} */ public function get($filepath) { $filepaths = [$filepath]; @@ -88,13 +73,7 @@ public function get($filepath) { } /** - * Gets data based on filenames. - * - * @param string[] $filepaths - * List of file names used as cache identifiers. - * - * @return array - * List of cached data keyed by the passed in file paths. + * {@inheritdoc} */ public function getMultiple(array $filepaths) { $file_data = []; @@ -113,7 +92,7 @@ public function getMultiple(array $filepaths) { continue; } - $cid = static::CACHE_PREFIX . ':' . $this->identifier . ':' . $realpath; + $cid = static::CACHE_PREFIX . ':' . $this->collection . ':' . $realpath; if (isset(static::$cached[$cid]) && static::$cached[$cid]['mtime'] == filemtime($realpath)) { $file_data[$filepath] = static::$cached[$cid]['data']; } @@ -140,12 +119,7 @@ public function getMultiple(array $filepaths) { } /** - * Store data based on a filename. - * - * @param string $filepath - * Name of the cache that the cached data is based on. - * @param mixed $data - * The data that should be cached. + * {@inheritdoc} */ public function set($filepath, $data) { $realpath = realpath($filepath); @@ -155,7 +129,7 @@ public function set($filepath, $data) { 'data' => $data, ]; - $cid = static::CACHE_PREFIX . ':' . $this->identifier . ':' . $realpath; + $cid = static::CACHE_PREFIX . ':' . $this->collection . ':' . $realpath; static::$cached[$cid] = $cached; if ($this->cache) { $this->cache->store($cid, $cached); @@ -163,14 +137,11 @@ public function set($filepath, $data) { } /** - * Delete data from the cache. - * - * @param string $filepath - * Name of the cache that the cached data is based on. + * {@inheritdoc} */ public function delete($filepath) { $realpath = realpath($filepath); - $cid = static::CACHE_PREFIX . ':' . $this->identifier . ':' . $realpath; + $cid = static::CACHE_PREFIX . ':' . $this->collection . ':' . $realpath; unset(static::$cached[$cid]); if ($this->cache) { diff --git a/core/lib/Drupal/Component/FileCache/FileCacheFactory.php b/core/lib/Drupal/Component/FileCache/FileCacheFactory.php index b54b18a..ba01861 100644 --- a/core/lib/Drupal/Component/FileCache/FileCacheFactory.php +++ b/core/lib/Drupal/Component/FileCache/FileCacheFactory.php @@ -6,6 +6,7 @@ */ namespace Drupal\Component\FileCache; +use Drupal\Core\Site\Settings; /** * Creates a FileCache object. @@ -20,37 +21,47 @@ class FileCacheFactory { protected static $configuration; /** - * Instantiates a FileCache object for a given identifier. + * Instantiates a FileCache object for a given collection identifier. * - * @param string $identifier - * The identifier for this FileCache. - * @param string $default_configuration + * @param string $collection + * The collection identifier for this FileCache. + * @param array $default_configuration * (optional) The default configuration for this FileCache identifier. This * can be used to e.g. specify default usage of a FileCacheCollector class. * * @return \Drupal\Component\FileCache\FileCache * The initialized FileCache object. */ - public static function get($identifier, $default_configuration = []) { + public static function get($collection, $default_configuration = []) { $default_configuration += [ 'class' => '\Drupal\Component\FileCache\FileCache', - 'identifier' => $identifier, + 'collection' => $collection, 'cache_backend_class' => NULL, + 'cache_backend_configuration' => [], ]; + // @todo Use extension_loaded('apcu') for non-testbot + // https://www.drupal.org/node/2447753. + if (function_exists('apc_fetch')) { + $default_configuration['cache_backend_class'] = '\Drupal\Component\FileCache\ApcuFileCacheBackend'; + } + $overrides = Settings::get('file_cache', []); $configuration = []; - if (isset(static::$configuration[$identifier])) { - $configuration = static::$configuration[$identifier]; + if (isset(static::$configuration[$collection])) { + $configuration = static::$configuration[$collection]; } elseif (isset(static::$configuration['default'])) { $configuration = static::$configuration['default']; } + elseif (isset($overrides[$collection])) { + $configuration = $overrides[$collection]; + } // Add defaults to the configuration. $configuration = $configuration + $default_configuration; $class = $configuration['class']; - return new $class($configuration['identifier'], $configuration['cache_backend_class']); + return new $class($configuration['collection'], $configuration['cache_backend_class'], $configuration['cache_backend_configuration']); } /** diff --git a/core/lib/Drupal/Component/FileCache/FileCacheInterface.php b/core/lib/Drupal/Component/FileCache/FileCacheInterface.php new file mode 100644 index 0000000..27fcd20 --- /dev/null +++ b/core/lib/Drupal/Component/FileCache/FileCacheInterface.php @@ -0,0 +1,56 @@ +fileCache->get($file); if (!$content) { $content = $this->loadFile($file); diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index 247a863..e26f72a 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -246,23 +246,6 @@ public static function createFromRequest(Request $request, $class_loader, $envir $response->prepare($request)->send(); } - // Initialize the FileCacheFactory component. - $configuration = Settings::get('file_cache'); - - // Provide a default configuration, if not set. - if (!isset($configuration['default'])) { - $configuration['default'] = [ - 'class' => '\Drupal\Component\FileCache\FileCache', - 'cache_backend_class' => NULL, - ]; - // @todo Use extension_loaded('apcu') for non-testbot - // https://www.drupal.org/node/2447753. - if (function_exists('apc_fetch')) { - $configuration['default']['cache_backend_class'] = '\Drupal\Component\FileCache\ApcuFileCacheBackend'; - } - } - FileCacheFactory::setConfiguration($configuration); - return $kernel; } @@ -414,7 +397,6 @@ public function boot() { if (!$this->sitePath) { throw new \Exception('Kernel does not have site path set before calling boot()'); } - // Initialize the container. $this->initializeContainer(); diff --git a/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php b/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php index 7cf7447..3e8103f 100644 --- a/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php +++ b/core/tests/Drupal/Tests/Component/FileCache/FileCacheFactoryTest.php @@ -23,11 +23,10 @@ protected function setUp() { parent::setUp(); $settings = [ - 'identifier' => 'test-23', - 'cache_backend_class' => [ - '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend' => [ - 'bin' => 'dog', - ], + 'collection' => 'test-23', + 'cache_backend_class' => '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend', + 'cache_backend_configuration' => [ + 'bin' => 'dog', ], ]; $configuration = FileCacheFactory::getConfiguration(); diff --git a/core/tests/Drupal/Tests/Component/FileCache/FileCacheTest.php b/core/tests/Drupal/Tests/Component/FileCache/FileCacheTest.php index c94d884..fda714d 100644 --- a/core/tests/Drupal/Tests/Component/FileCache/FileCacheTest.php +++ b/core/tests/Drupal/Tests/Component/FileCache/FileCacheTest.php @@ -36,12 +36,7 @@ class FileCacheTest extends UnitTestCase { protected function setUp() { parent::setUp(); - $settings = [ - '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend' => [ - 'bin' => 'llama', - ], - ]; - $this->fileCache = new FileCache('test', $settings); + $this->fileCache = new FileCache('test', '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend', ['bin' => 'llama']); $this->staticFileCache = new StaticFileCacheBackend(['bin' => 'llama']); } diff --git a/core/tests/Drupal/Tests/Component/FileCache/StaticFileCacheBackend.php b/core/tests/Drupal/Tests/Component/FileCache/StaticFileCacheBackend.php index d1511c6..ed82b81 100644 --- a/core/tests/Drupal/Tests/Component/FileCache/StaticFileCacheBackend.php +++ b/core/tests/Drupal/Tests/Component/FileCache/StaticFileCacheBackend.php @@ -72,4 +72,5 @@ public function delete($cid) { public static function reset() { static::$cache = []; } + }