FileCacheFactory.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Drupal\Component\FileCache;
  3. /**
  4. * Creates a FileCache object.
  5. */
  6. class FileCacheFactory {
  7. /**
  8. * The configuration key to disable FileCache completely.
  9. */
  10. const DISABLE_CACHE = 'file_cache_disable';
  11. /**
  12. * The configuration used to create FileCache objects.
  13. *
  14. * @var array
  15. */
  16. protected static $configuration;
  17. /**
  18. * The cache prefix.
  19. *
  20. * @var string
  21. */
  22. protected static $prefix;
  23. /**
  24. * Instantiates a FileCache object for a given collection identifier.
  25. *
  26. * @param string $collection
  27. * The collection identifier for this FileCache.
  28. * @param array $default_configuration
  29. * (optional) The default configuration for this FileCache collection. This
  30. * can be used to e.g. specify default usage of a FileCache class.
  31. *
  32. * @return \Drupal\Component\FileCache\FileCacheInterface
  33. * The initialized FileCache object.
  34. */
  35. public static function get($collection, $default_configuration = []) {
  36. // If there is a special key in the configuration, disable FileCache completely.
  37. if (!empty(static::$configuration[static::DISABLE_CACHE])) {
  38. return new NullFileCache('', '');
  39. }
  40. $configuration = [];
  41. // Check for a collection specific setting first.
  42. if (isset(static::$configuration[$collection])) {
  43. $configuration += static::$configuration[$collection];
  44. }
  45. // Then check if a default configuration has been provided.
  46. if (!empty($default_configuration)) {
  47. $configuration += $default_configuration;
  48. }
  49. // Last check if a default setting has been provided.
  50. if (isset(static::$configuration['default'])) {
  51. $configuration += static::$configuration['default'];
  52. }
  53. // Ensure that all properties are set.
  54. $fallback_configuration = [
  55. 'class' => '\Drupal\Component\FileCache\FileCache',
  56. 'collection' => $collection,
  57. 'cache_backend_class' => NULL,
  58. 'cache_backend_configuration' => [],
  59. ];
  60. $configuration = $configuration + $fallback_configuration;
  61. $class = $configuration['class'];
  62. return new $class(static::getPrefix(), $configuration['collection'], $configuration['cache_backend_class'], $configuration['cache_backend_configuration']);
  63. }
  64. /**
  65. * Gets the configuration used for constructing future file cache objects.
  66. *
  67. * @return array
  68. * The configuration that is used.
  69. */
  70. public static function getConfiguration() {
  71. return static::$configuration;
  72. }
  73. /**
  74. * Sets the configuration to use for constructing future file cache objects.
  75. *
  76. * @param array $configuration
  77. * The configuration to use.
  78. */
  79. public static function setConfiguration($configuration) {
  80. static::$configuration = $configuration;
  81. }
  82. /**
  83. * Returns the cache prefix.
  84. *
  85. * @return string
  86. * The cache prefix.
  87. */
  88. public static function getPrefix() {
  89. return static::$prefix;
  90. }
  91. /**
  92. * Sets the cache prefix that should be used.
  93. *
  94. * Should be set to a secure, unique key to prevent cache pollution by a
  95. * third party.
  96. *
  97. * @param string $prefix
  98. * The cache prefix.
  99. */
  100. public static function setPrefix($prefix) {
  101. static::$prefix = $prefix;
  102. }
  103. }