storage = $storage; $this->lockBackend = $lock_backend; $this->owner = $owner; $this->requestStack = $request_stack; $this->currentUser = $current_user; $this->expire = $expire; } /** * Retrieves a value from this SharedTempStore 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) { if ($object = $this->storage->get($key)) { return $object->data; } } /** * Retrieves a value from this SharedTempStore for a given key. * * Only returns the value if the value is owned by $this->owner. * * @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 getIfOwner($key) { if (($object = $this->storage->get($key)) && ($object->owner == $this->owner)) { return $object->data; } } /** * Stores a particular key/value pair only if the key doesn't already exist. * * @param string $key * The key of the data to check and store. * @param mixed $value * The data to store. * * @return bool * TRUE if the data was set, or FALSE if it already existed. */ public function setIfNotExists($key, $value) { $value = (object) [ 'owner' => $this->owner, 'data' => $value, 'updated' => (int) $this->requestStack->getMainRequest()->server->get('REQUEST_TIME'), ]; $this->ensureAnonymousSession(); $set = $this->storage->setWithExpireIfNotExists($key, $value, $this->expire); return $set; } /** * Stores a particular key/value pair in this SharedTempStore. * * Only stores the given key/value pair if it does not exist yet or is owned * by $this->owner. * * @param string $key * The key of the data to store. * @param mixed $value * The data to store. * * @return bool * TRUE if the data was set, or FALSE if it already exists and is not owned * by $this->user. * * @throws \Drupal\Core\TempStore\TempStoreException * Thrown when a lock for the backend storage could not be acquired. */ public function setIfOwner($key, $value) { if ($this->setIfNotExists($key, $value)) { return TRUE; } if (($object = $this->storage->get($key)) && ($object->owner == $this->owner)) { $this->set($key, $value); return TRUE; } return FALSE; } /** * Stores a particular key/value pair in this SharedTempStore. * * @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) { 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->owner, 'data' => $value, 'updated' => (int) $this->requestStack->getMainRequest()->server->get('REQUEST_TIME'), ]; $this->ensureAnonymousSession(); $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 \Drupal\Core\TempStore\Lock|null * An object with the owner and updated time if the key has a value, or * NULL otherwise. */ public function getMetadata($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 new Lock($object->owner, $object->updated); } } /** * 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. * * @throws \Drupal\Core\TempStore\TempStoreException * Thrown when a lock for the backend storage could not be acquired. */ public function delete($key) { 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); } /** * Deletes data from the store for a given key and releases the lock on it. * * Only delete the given key if it is owned by $this->owner. * * @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 deleteIfOwner($key) { if (!$object = $this->storage->get($key)) { return TRUE; } elseif ($object->owner == $this->owner) { $this->delete($key); return TRUE; } return FALSE; } /** * Stores the owner in the session if the user is anonymous. * * This method should be called when a value is set. */ protected function ensureAnonymousSession() { if ($this->currentUser->isAnonymous()) { $this->requestStack->getSession()->set('core.tempstore.shared.owner', $this->owner); } } }