X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Flib%2FDrupal%2FCore%2FTempStore%2FPrivateTempStore.php;fp=web%2Fcore%2Flib%2FDrupal%2FCore%2FTempStore%2FPrivateTempStore.php;h=f9d10347050450e388e790deeb425aeaa01cfde3;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/web/core/lib/Drupal/Core/TempStore/PrivateTempStore.php b/web/core/lib/Drupal/Core/TempStore/PrivateTempStore.php new file mode 100644 index 000000000..f9d103470 --- /dev/null +++ b/web/core/lib/Drupal/Core/TempStore/PrivateTempStore.php @@ -0,0 +1,213 @@ +storage = $storage; + $this->lockBackend = $lock_backend; + $this->currentUser = $current_user; + $this->requestStack = $request_stack; + $this->expire = $expire; + } + + /** + * Retrieves a value from this PrivateTempStore for a given key. + * + * @param string $key + * The key of the data to retrieve. + * + * @return mixed + * The data associated with the key, or NULL if the key does not exist. + */ + public function get($key) { + $key = $this->createkey($key); + if (($object = $this->storage->get($key)) && ($object->owner == $this->getOwner())) { + return $object->data; + } + } + + /** + * Stores a particular key/value pair in this PrivateTempStore. + * + * @param string $key + * The key of the data to store. + * @param mixed $value + * The data to store. + * + * @throws \Drupal\Core\TempStore\TempStoreException + * Thrown when a lock for the backend storage could not be acquired. + */ + public function set($key, $value) { + $key = $this->createkey($key); + if (!$this->lockBackend->acquire($key)) { + $this->lockBackend->wait($key); + if (!$this->lockBackend->acquire($key)) { + throw new TempStoreException("Couldn't acquire lock to update item '$key' in '{$this->storage->getCollectionName()}' temporary storage."); + } + } + + $value = (object) [ + 'owner' => $this->getOwner(), + 'data' => $value, + 'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'), + ]; + $this->storage->setWithExpire($key, $value, $this->expire); + $this->lockBackend->release($key); + } + + /** + * Returns the metadata associated with a particular key/value pair. + * + * @param string $key + * The key of the data to store. + * + * @return mixed + * An object with the owner and updated time if the key has a value, or + * NULL otherwise. + */ + public function getMetadata($key) { + $key = $this->createkey($key); + // Fetch the key/value pair and its metadata. + $object = $this->storage->get($key); + if ($object) { + // Don't keep the data itself in memory. + unset($object->data); + return $object; + } + } + + /** + * Deletes data from the store for a given key and releases the lock on it. + * + * @param string $key + * The key of the data to delete. + * + * @return bool + * TRUE if the object was deleted or does not exist, FALSE if it exists but + * is not owned by $this->owner. + * + * @throws \Drupal\Core\TempStore\TempStoreException + * Thrown when a lock for the backend storage could not be acquired. + */ + public function delete($key) { + $key = $this->createkey($key); + if (!$object = $this->storage->get($key)) { + return TRUE; + } + elseif ($object->owner != $this->getOwner()) { + return FALSE; + } + if (!$this->lockBackend->acquire($key)) { + $this->lockBackend->wait($key); + if (!$this->lockBackend->acquire($key)) { + throw new TempStoreException("Couldn't acquire lock to delete item '$key' from '{$this->storage->getCollectionName()}' temporary storage."); + } + } + $this->storage->delete($key); + $this->lockBackend->release($key); + return TRUE; + } + + /** + * Ensures that the key is unique for a user. + * + * @param string $key + * The key. + * + * @return string + * The unique key for the user. + */ + protected function createkey($key) { + return $this->getOwner() . ':' . $key; + } + + /** + * Gets the current owner based on the current user or the session ID. + * + * @return string + * The owner. + */ + protected function getOwner() { + return $this->currentUser->id() ?: $this->requestStack->getCurrentRequest()->getSession()->getId(); + } + +}