diff --git a/core/lib/Drupal/Core/PhpStorage/CacheStorage.php b/core/lib/Drupal/Core/PhpStorage/CacheStorage.php index 3be4e78..725086a 100644 --- a/core/lib/Drupal/Core/PhpStorage/CacheStorage.php +++ b/core/lib/Drupal/Core/PhpStorage/CacheStorage.php @@ -38,9 +38,20 @@ class CacheStorage implements PhpStorageInterface { */ protected $bin; + /** + * @var callable + */ + protected $cacheBackendFactory; + public function __construct(array $configuration) { $this->secret = $configuration['secret']; $this->bin = $configuration['bin']; + if (isset($configuration['cache_backend_factory']) && is_callable($configuration['cache_backend_factory'])) { + $this->cacheBackendFactory = $configuration['cache_backend_factory']; + } + else { + $this->cacheBackendFactory = [$this, 'getDatabaseBackend']; + } } /** @@ -67,7 +78,7 @@ public function load($name) { // multiple times by the classloader as the container class is loaded. // So for better performance use the phar protocol. stream_wrapper_unregister('phar'); - stream_wrapper_register('phar', 'Drupal\Core\StreamWrapper\SpecialCacheStream'); + stream_wrapper_register('phar', 'Drupal\Core\StreamWrapper\SpecialInMemoryStream'); SpecialInMemoryStream::init($this, $cached->data); $return = (include "phar://$name") !== FALSE; #var_dump(opcache_get_status(TRUE)['scripts']["phar://$name"]); @@ -160,19 +171,20 @@ public function listAll() { */ protected function cacheBackend() { if (!isset($this->cacheBackend)) { - $bootstrap_php_storage = Settings::get('php_storage_cache_backend'); - if (is_callable($bootstrap_php_storage)) { - $this->cacheBackend = $bootstrap_php_storage(); - } - else { - $connection = Database::getConnection(); - $this->cacheBackend = new DatabaseBackend($connection, new DatabaseCacheTagsChecksum($connection), 'php_' . $this->bin); - } + $this->cacheBackend = call_user_func($this->cacheBackendFactory); } return $this->cacheBackend; } /** + * Construct a database cache backend. + */ + protected function getDatabaseBackend() { + $connection = Database::getConnection(); + return new DatabaseBackend($connection, new DatabaseCacheTagsChecksum($connection), 'php_' . $this->bin); + } + + /** * Return a secret key based on the filename. * * By using a secret key, a SQL injection does not lead immediately to diff --git a/core/tests/Drupal/Tests/Core/PhpStorage/CacheStorageTest.php b/core/tests/Drupal/Tests/Core/PhpStorage/CacheStorageTest.php index 299952b..c0e47ff 100644 --- a/core/tests/Drupal/Tests/Core/PhpStorage/CacheStorageTest.php +++ b/core/tests/Drupal/Tests/Core/PhpStorage/CacheStorageTest.php @@ -29,8 +29,11 @@ class CacheStorageTest extends PhpStorageTestBase { */ public function testCRUD() { $secret = $this->randomMachineName(); - $storage = new CacheStorage(['secret' => $secret, 'bin' => 'test']); - new Settings(['php_storage_cache_backend' => function () { return new MemoryBackend('');}]); + $storage = new CacheStorage([ + 'secret' => $secret, + 'cache_backend_factory' => function () { return new MemoryBackend('');}, + 'bin' => 'test' + ]); $this->assertCRUD($storage); }