ca68757bc8a5237aa3dfa138ad2433685ec1b198
[yaffs-website] / web / core / lib / Drupal / Component / FileCache / FileCacheFactory.php
1 <?php
2
3 namespace Drupal\Component\FileCache;
4
5 /**
6  * Creates a FileCache object.
7  */
8 class FileCacheFactory {
9
10   /**
11    * The configuration key to disable FileCache completely.
12    */
13   const DISABLE_CACHE = 'file_cache_disable';
14
15   /**
16    * The configuration used to create FileCache objects.
17    *
18    * @var array
19    */
20   protected static $configuration;
21
22   /**
23    * The cache prefix.
24    *
25    * @var string
26    */
27   protected static $prefix;
28
29   /**
30    * Instantiates a FileCache object for a given collection identifier.
31    *
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.
37    *
38    * @return \Drupal\Component\FileCache\FileCacheInterface
39    *   The initialized FileCache object.
40    */
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('', '');
45     }
46
47     $configuration = [];
48
49     // Check for a collection specific setting first.
50     if (isset(static::$configuration[$collection])) {
51       $configuration += static::$configuration[$collection];
52     }
53     // Then check if a default configuration has been provided.
54     if (!empty($default_configuration)) {
55       $configuration += $default_configuration;
56     }
57     // Last check if a default setting has been provided.
58     if (isset(static::$configuration['default'])) {
59       $configuration += static::$configuration['default'];
60     }
61
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' => [],
68     ];
69
70     $configuration = $configuration + $fallback_configuration;
71
72     $class = $configuration['class'];
73     return new $class(static::getPrefix(), $configuration['collection'], $configuration['cache_backend_class'], $configuration['cache_backend_configuration']);
74   }
75
76   /**
77    * Gets the configuration used for constructing future file cache objects.
78    *
79    * @return array
80    *   The configuration that is used.
81    */
82   public static function getConfiguration() {
83     return static::$configuration;
84   }
85
86   /**
87    * Sets the configuration to use for constructing future file cache objects.
88    *
89    * @param array $configuration
90    *   The configuration to use.
91    */
92   public static function setConfiguration($configuration) {
93     static::$configuration = $configuration;
94   }
95
96   /**
97    * Returns the cache prefix.
98    *
99    * @return string
100    *   The cache prefix.
101    */
102   public static function getPrefix() {
103     return static::$prefix;
104   }
105
106   /**
107    * Sets the cache prefix that should be used.
108    *
109    * Should be set to a secure, unique key to prevent cache pollution by a
110    * third party.
111    *
112    * @param string $prefix
113    *   The cache prefix.
114    */
115   public static function setPrefix($prefix) {
116     static::$prefix = $prefix;
117   }
118
119 }