PrivateTempStore.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Drupal\Core\TempStore;
  3. use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
  4. use Drupal\Core\Lock\LockBackendInterface;
  5. use Drupal\Core\Session\AccountProxyInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. /**
  8. * Stores and retrieves temporary data for a given owner.
  9. *
  10. * A PrivateTempStore can be used to make temporary, non-cache data available
  11. * across requests. The data for the PrivateTempStore is stored in one
  12. * key/value collection. PrivateTempStore data expires automatically after a
  13. * given timeframe.
  14. *
  15. * The PrivateTempStore is different from a cache, because the data in it is not
  16. * yet saved permanently and so it cannot be rebuilt. Typically, the
  17. * PrivateTempStore might be used to store work in progress that is later saved
  18. * permanently elsewhere, e.g. autosave data, multistep forms, or in-progress
  19. * changes to complex configuration that are not ready to be saved.
  20. *
  21. * The PrivateTempStore differs from the SharedTempStore in that all keys are
  22. * ensured to be unique for a particular user and users can never share data. If
  23. * you want to be able to share data between users or use it for locking, use
  24. * \Drupal\Core\TempStore\SharedTempStore.
  25. */
  26. class PrivateTempStore {
  27. /**
  28. * The key/value storage object used for this data.
  29. *
  30. * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
  31. */
  32. protected $storage;
  33. /**
  34. * The lock object used for this data.
  35. *
  36. * @var \Drupal\Core\Lock\LockBackendInterface
  37. */
  38. protected $lockBackend;
  39. /**
  40. * The current user.
  41. *
  42. * @var \Drupal\Core\Session\AccountProxyInterface
  43. */
  44. protected $currentUser;
  45. /**
  46. * The request stack.
  47. *
  48. * @var \Symfony\Component\HttpFoundation\RequestStack
  49. */
  50. protected $requestStack;
  51. /**
  52. * The time to live for items in seconds.
  53. *
  54. * By default, data is stored for one week (604800 seconds) before expiring.
  55. *
  56. * @var int
  57. */
  58. protected $expire;
  59. /**
  60. * Constructs a new object for accessing data from a key/value store.
  61. *
  62. * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $storage
  63. * The key/value storage object used for this data. Each storage object
  64. * represents a particular collection of data and will contain any number
  65. * of key/value pairs.
  66. * @param \Drupal\Core\Lock\LockBackendInterface $lock_backend
  67. * The lock object used for this data.
  68. * @param \Drupal\Core\Session\AccountProxyInterface $current_user
  69. * The current user account.
  70. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
  71. * The request stack.
  72. * @param int $expire
  73. * The time to live for items, in seconds.
  74. */
  75. public function __construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lock_backend, AccountProxyInterface $current_user, RequestStack $request_stack, $expire = 604800) {
  76. $this->storage = $storage;
  77. $this->lockBackend = $lock_backend;
  78. $this->currentUser = $current_user;
  79. $this->requestStack = $request_stack;
  80. $this->expire = $expire;
  81. }
  82. /**
  83. * Retrieves a value from this PrivateTempStore for a given key.
  84. *
  85. * @param string $key
  86. * The key of the data to retrieve.
  87. *
  88. * @return mixed
  89. * The data associated with the key, or NULL if the key does not exist.
  90. */
  91. public function get($key) {
  92. $key = $this->createkey($key);
  93. if (($object = $this->storage->get($key)) && ($object->owner == $this->getOwner())) {
  94. return $object->data;
  95. }
  96. }
  97. /**
  98. * Stores a particular key/value pair in this PrivateTempStore.
  99. *
  100. * @param string $key
  101. * The key of the data to store.
  102. * @param mixed $value
  103. * The data to store.
  104. *
  105. * @throws \Drupal\Core\TempStore\TempStoreException
  106. * Thrown when a lock for the backend storage could not be acquired.
  107. */
  108. public function set($key, $value) {
  109. $key = $this->createkey($key);
  110. if (!$this->lockBackend->acquire($key)) {
  111. $this->lockBackend->wait($key);
  112. if (!$this->lockBackend->acquire($key)) {
  113. throw new TempStoreException("Couldn't acquire lock to update item '$key' in '{$this->storage->getCollectionName()}' temporary storage.");
  114. }
  115. }
  116. $value = (object) [
  117. 'owner' => $this->getOwner(),
  118. 'data' => $value,
  119. 'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'),
  120. ];
  121. $this->storage->setWithExpire($key, $value, $this->expire);
  122. $this->lockBackend->release($key);
  123. }
  124. /**
  125. * Returns the metadata associated with a particular key/value pair.
  126. *
  127. * @param string $key
  128. * The key of the data to store.
  129. *
  130. * @return mixed
  131. * An object with the owner and updated time if the key has a value, or
  132. * NULL otherwise.
  133. */
  134. public function getMetadata($key) {
  135. $key = $this->createkey($key);
  136. // Fetch the key/value pair and its metadata.
  137. $object = $this->storage->get($key);
  138. if ($object) {
  139. // Don't keep the data itself in memory.
  140. unset($object->data);
  141. return $object;
  142. }
  143. }
  144. /**
  145. * Deletes data from the store for a given key and releases the lock on it.
  146. *
  147. * @param string $key
  148. * The key of the data to delete.
  149. *
  150. * @return bool
  151. * TRUE if the object was deleted or does not exist, FALSE if it exists but
  152. * is not owned by $this->owner.
  153. *
  154. * @throws \Drupal\Core\TempStore\TempStoreException
  155. * Thrown when a lock for the backend storage could not be acquired.
  156. */
  157. public function delete($key) {
  158. $key = $this->createkey($key);
  159. if (!$object = $this->storage->get($key)) {
  160. return TRUE;
  161. }
  162. elseif ($object->owner != $this->getOwner()) {
  163. return FALSE;
  164. }
  165. if (!$this->lockBackend->acquire($key)) {
  166. $this->lockBackend->wait($key);
  167. if (!$this->lockBackend->acquire($key)) {
  168. throw new TempStoreException("Couldn't acquire lock to delete item '$key' from '{$this->storage->getCollectionName()}' temporary storage.");
  169. }
  170. }
  171. $this->storage->delete($key);
  172. $this->lockBackend->release($key);
  173. return TRUE;
  174. }
  175. /**
  176. * Ensures that the key is unique for a user.
  177. *
  178. * @param string $key
  179. * The key.
  180. *
  181. * @return string
  182. * The unique key for the user.
  183. */
  184. protected function createkey($key) {
  185. return $this->getOwner() . ':' . $key;
  186. }
  187. /**
  188. * Gets the current owner based on the current user or the session ID.
  189. *
  190. * @return string
  191. * The owner.
  192. */
  193. protected function getOwner() {
  194. return $this->currentUser->id() ?: $this->requestStack->getCurrentRequest()->getSession()->getId();
  195. }
  196. }