Chris@0: storage = $storage; Chris@0: $this->lockBackend = $lock_backend; Chris@0: $this->currentUser = $current_user; Chris@0: $this->requestStack = $request_stack; Chris@0: $this->expire = $expire; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves a value from this PrivateTempStore for a given key. Chris@0: * Chris@0: * @param string $key Chris@0: * The key of the data to retrieve. Chris@0: * Chris@0: * @return mixed Chris@0: * The data associated with the key, or NULL if the key does not exist. Chris@0: */ Chris@0: public function get($key) { Chris@0: $key = $this->createkey($key); Chris@0: if (($object = $this->storage->get($key)) && ($object->owner == $this->getOwner())) { Chris@0: return $object->data; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Stores a particular key/value pair in this PrivateTempStore. Chris@0: * Chris@0: * @param string $key Chris@0: * The key of the data to store. Chris@0: * @param mixed $value Chris@0: * The data to store. Chris@0: * Chris@0: * @throws \Drupal\user\TempStoreException Chris@0: * Thrown when a lock for the backend storage could not be acquired. Chris@0: */ Chris@0: public function set($key, $value) { Chris@0: $key = $this->createkey($key); Chris@0: if (!$this->lockBackend->acquire($key)) { Chris@0: $this->lockBackend->wait($key); Chris@0: if (!$this->lockBackend->acquire($key)) { Chris@0: throw new TempStoreException("Couldn't acquire lock to update item '$key' in '{$this->storage->getCollectionName()}' temporary storage."); Chris@0: } Chris@0: } Chris@0: Chris@0: $value = (object) [ Chris@0: 'owner' => $this->getOwner(), Chris@0: 'data' => $value, Chris@0: 'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'), Chris@0: ]; Chris@0: $this->storage->setWithExpire($key, $value, $this->expire); Chris@0: $this->lockBackend->release($key); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the metadata associated with a particular key/value pair. Chris@0: * Chris@0: * @param string $key Chris@0: * The key of the data to store. Chris@0: * Chris@0: * @return mixed Chris@0: * An object with the owner and updated time if the key has a value, or Chris@0: * NULL otherwise. Chris@0: */ Chris@0: public function getMetadata($key) { Chris@0: $key = $this->createkey($key); Chris@0: // Fetch the key/value pair and its metadata. Chris@0: $object = $this->storage->get($key); Chris@0: if ($object) { Chris@0: // Don't keep the data itself in memory. Chris@0: unset($object->data); Chris@0: return $object; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deletes data from the store for a given key and releases the lock on it. Chris@0: * Chris@0: * @param string $key Chris@0: * The key of the data to delete. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the object was deleted or does not exist, FALSE if it exists but Chris@0: * is not owned by $this->owner. Chris@0: * Chris@0: * @throws \Drupal\user\TempStoreException Chris@0: * Thrown when a lock for the backend storage could not be acquired. Chris@0: */ Chris@0: public function delete($key) { Chris@0: $key = $this->createkey($key); Chris@0: if (!$object = $this->storage->get($key)) { Chris@0: return TRUE; Chris@0: } Chris@0: elseif ($object->owner != $this->getOwner()) { Chris@0: return FALSE; Chris@0: } Chris@0: if (!$this->lockBackend->acquire($key)) { Chris@0: $this->lockBackend->wait($key); Chris@0: if (!$this->lockBackend->acquire($key)) { Chris@0: throw new TempStoreException("Couldn't acquire lock to delete item '$key' from '{$this->storage->getCollectionName()}' temporary storage."); Chris@0: } Chris@0: } Chris@0: $this->storage->delete($key); Chris@0: $this->lockBackend->release($key); Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures that the key is unique for a user. Chris@0: * Chris@0: * @param string $key Chris@0: * The key. Chris@0: * Chris@0: * @return string Chris@0: * The unique key for the user. Chris@0: */ Chris@0: protected function createkey($key) { Chris@0: return $this->getOwner() . ':' . $key; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current owner based on the current user or the session ID. Chris@0: * Chris@0: * @return string Chris@0: * The owner. Chris@0: */ Chris@0: protected function getOwner() { Chris@0: return $this->currentUser->id() ?: $this->requestStack->getCurrentRequest()->getSession()->getId(); Chris@0: } Chris@0: Chris@0: }