diff --git a/core/lib/Drupal/Core/AtomicCounter/AtomicCounterInterface.php b/core/lib/Drupal/Core/AtomicCounter/AtomicCounterInterface.php new file mode 100644 index 0000000..265082d --- /dev/null +++ b/core/lib/Drupal/Core/AtomicCounter/AtomicCounterInterface.php @@ -0,0 +1,34 @@ +condition('collection', $this->collection) ->execute(); } + + /** + * {@inheritdoc} + */ + public function fetchCounter($name) { + // ::get() isn't used here since that assumes serialized data. + $arguments = [':collection' => $this->collection, ':name' => $name]; + return $this->connection->query("SELECT value FROM {" . $this->connection->escapeTable($this->table) . "} WHERE collection = :collection AND name = :name", $arguments) + ->fetchField() ?: 0; + } + + /** + * {@inheritdoc} + */ + public function incrementCounter($name) { + try { + $this->connection + ->insert($this->table) + ->fields([ + 'collection' => $this->collection, + 'name' => $name, + 'value' => 1, + ]) + ->execute(); + } catch (\Exception $e) { + $this->connection->update($this->table) + ->expression('value', 'value + 1') + ->condition('name', $name) + ->execute(); + } + } + } diff --git a/core/lib/Drupal/Core/PhpStorage/CoordinatedMTimeProtectedFileStorage.php b/core/lib/Drupal/Core/PhpStorage/CoordinatedMTimeProtectedFileStorage.php index 1d920f9..6302f1f 100644 --- a/core/lib/Drupal/Core/PhpStorage/CoordinatedMTimeProtectedFileStorage.php +++ b/core/lib/Drupal/Core/PhpStorage/CoordinatedMTimeProtectedFileStorage.php @@ -7,7 +7,9 @@ namespace Drupal\Core\PhpStorage; use Drupal\Component\PhpStorage\MTimeProtectedFileStorage; +use Drupal\Component\Serialization\PhpSerialize; use Drupal\Core\Database\Database; +use Drupal\Core\KeyValueStore\DatabaseStorage; /** * Coordinated storage for php code. @@ -31,11 +33,11 @@ class CoordinatedMTimeProtectedFileStorage extends MTimeProtectedFileStorage { /** - * The coordinator object. + * The atomic counter. * - * @var \Drupal\Core\PhpStorage\Coordinator\CoordinatorInterface + * @var \Drupal\Core\AtomicCounter\AtomicCounterInterface */ - protected $coordinator; + protected $counter; /** * Constructs this CoordinatedMTimeProtectedFastFileStorage object. @@ -45,11 +47,12 @@ class CoordinatedMTimeProtectedFileStorage extends MTimeProtectedFileStorage { public function __construct(array $configuration) { parent::__construct($configuration); - if (!isset($configuration['coordinator_class'])) { - $configuration['coordinator_class'] = '\\Drupal\\Core\\PhpStorage\\Coordinator\\DatabaseCoordinator'; + if (!isset($configuration['atomic_counter_class']) || !is_callable($configuration['atomic_counter_class'])) { + $this->counter = new DatabaseStorage('coordinated_mtime_storage_counter', new PhpSerialize(), Database::getConnection()); + } + else { + $this->counter = new $configuration['atomic_counter_class']($configuration); } - - $this->coordinator = new $configuration['coordinator_class']($configuration); } /** @@ -63,8 +66,7 @@ public function getFullPath($name, &$directory = NULL, &$directory_mtime = NULL) $directory_mtime = file_exists($directory) ? filemtime($directory) : 0; } - $counter = $this->coordinator->fetchCounter(); - $counter = $counter ?: '0'; + $counter = $this->counter->fetchCounter('service_container_counter'); $path = $directory . '/' . hash_hmac('sha256', $name, $this->secret . $directory_mtime) . ".$counter.php"; return $path; } @@ -73,7 +75,7 @@ public function getFullPath($name, &$directory = NULL, &$directory_mtime = NULL) * {@inheritdoc} */ public function deleteAll() { - $this->coordinator->incrementCounter(); + $this->counter->incrementCounter('service_container_counter'); parent::deleteAll(); } diff --git a/core/lib/Drupal/Core/PhpStorage/Coordinator/CoordinatorInterface.php b/core/lib/Drupal/Core/PhpStorage/Coordinator/CoordinatorInterface.php deleted file mode 100644 index 2bc6f07..0000000 --- a/core/lib/Drupal/Core/PhpStorage/Coordinator/CoordinatorInterface.php +++ /dev/null @@ -1,25 +0,0 @@ -query($sql) - ->fetchColumn(); - } - - /** - * {@inheritdoc} - */ - public function incrementCounter() { - try { - Database::getConnection() - ->insert('key_value') - ->fields(array( - 'collection' => 'coordinated_mtime_storage_counter', - 'name' => 'service_container_counter', - 'value' => 1, - )) - ->execute(); - } catch (\Exception $e) { - Database::getConnection()->update('key_value') - ->expression('value', 'value + 1') - ->condition('name', 'service_container_counter') - ->execute(); - } - } - -}