Chris@0: storage = $storage; Chris@0: $this->lockBackend = $lock_backend; Chris@0: $this->owner = $owner; Chris@0: $this->requestStack = $request_stack; Chris@0: $this->expire = $expire; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves a value from this SharedTempStore 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: if ($object = $this->storage->get($key)) { Chris@0: return $object->data; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves a value from this SharedTempStore for a given key. Chris@0: * Chris@0: * Only returns the value if the value is owned by $this->owner. 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 getIfOwner($key) { Chris@0: if (($object = $this->storage->get($key)) && ($object->owner == $this->owner)) { Chris@0: return $object->data; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Stores a particular key/value pair only if the key doesn't already exist. Chris@0: * Chris@0: * @param string $key Chris@0: * The key of the data to check and store. Chris@0: * @param mixed $value Chris@0: * The data to store. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the data was set, or FALSE if it already existed. Chris@0: */ Chris@0: public function setIfNotExists($key, $value) { Chris@0: $value = (object) [ Chris@0: 'owner' => $this->owner, Chris@0: 'data' => $value, Chris@0: 'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'), Chris@0: ]; Chris@0: return $this->storage->setWithExpireIfNotExists($key, $value, $this->expire); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Stores a particular key/value pair in this SharedTempStore. Chris@0: * Chris@0: * Only stores the given key/value pair if it does not exist yet or is owned Chris@0: * by $this->owner. 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: * @return bool Chris@0: * TRUE if the data was set, or FALSE if it already exists and is not owned Chris@0: * by $this->user. 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 setIfOwner($key, $value) { Chris@0: if ($this->setIfNotExists($key, $value)) { Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: if (($object = $this->storage->get($key)) && ($object->owner == $this->owner)) { Chris@0: $this->set($key, $value); Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Stores a particular key/value pair in this SharedTempStore. 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: 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->owner, 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: // 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: * @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: 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: } 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: * Only delete the given key if it is owned by $this->owner. 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 deleteIfOwner($key) { Chris@0: if (!$object = $this->storage->get($key)) { Chris@0: return TRUE; Chris@0: } Chris@0: elseif ($object->owner == $this->owner) { Chris@0: $this->delete($key); Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: }