3 namespace Drupal\Component\FileCache;
6 * Creates a FileCache object.
8 class FileCacheFactory {
11 * The configuration key to disable FileCache completely.
13 const DISABLE_CACHE = 'file_cache_disable';
16 * The configuration used to create FileCache objects.
20 protected static $configuration;
27 protected static $prefix;
30 * Instantiates a FileCache object for a given collection identifier.
32 * @param string $collection
33 * The collection identifier for this FileCache.
34 * @param array $default_configuration
35 * (optional) The default configuration for this FileCache collection. This
36 * can be used to e.g. specify default usage of a FileCache class.
38 * @return \Drupal\Component\FileCache\FileCacheInterface
39 * The initialized FileCache object.
41 public static function get($collection, $default_configuration = []) {
42 // If there is a special key in the configuration, disable FileCache completely.
43 if (!empty(static::$configuration[static::DISABLE_CACHE])) {
44 return new NullFileCache('', '');
49 // Check for a collection specific setting first.
50 if (isset(static::$configuration[$collection])) {
51 $configuration += static::$configuration[$collection];
53 // Then check if a default configuration has been provided.
54 if (!empty($default_configuration)) {
55 $configuration += $default_configuration;
57 // Last check if a default setting has been provided.
58 if (isset(static::$configuration['default'])) {
59 $configuration += static::$configuration['default'];
62 // Ensure that all properties are set.
63 $fallback_configuration = [
64 'class' => '\Drupal\Component\FileCache\FileCache',
65 'collection' => $collection,
66 'cache_backend_class' => NULL,
67 'cache_backend_configuration' => [],
70 $configuration = $configuration + $fallback_configuration;
72 $class = $configuration['class'];
73 return new $class(static::getPrefix(), $configuration['collection'], $configuration['cache_backend_class'], $configuration['cache_backend_configuration']);
77 * Gets the configuration used for constructing future file cache objects.
80 * The configuration that is used.
82 public static function getConfiguration() {
83 return static::$configuration;
87 * Sets the configuration to use for constructing future file cache objects.
89 * @param array $configuration
90 * The configuration to use.
92 public static function setConfiguration($configuration) {
93 static::$configuration = $configuration;
97 * Returns the cache prefix.
102 public static function getPrefix() {
103 return static::$prefix;
107 * Sets the cache prefix that should be used.
109 * Should be set to a secure, unique key to prevent cache pollution by a
112 * @param string $prefix
115 public static function setPrefix($prefix) {
116 static::$prefix = $prefix;