PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/web/core/lib/Drupal/Core/TempStore/SharedTempStore.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 318 lines | 149 code | 19 blank | 150 comment | 14 complexity | c1d78e019f73be4efcc7d2c222b8c824 MD5 | raw file
  1. <?php
  2. namespace Drupal\Core\TempStore;
  3. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  4. use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
  5. use Drupal\Core\Lock\LockBackendInterface;
  6. use Drupal\Core\Session\AccountProxyInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. /**
  9. * Stores and retrieves temporary data for a given owner.
  10. *
  11. * A SharedTempStore can be used to make temporary, non-cache data available
  12. * across requests. The data for the SharedTempStore is stored in one key/value
  13. * collection. SharedTempStore data expires automatically after a given
  14. * timeframe.
  15. *
  16. * The SharedTempStore is different from a cache, because the data in it is not
  17. * yet saved permanently and so it cannot be rebuilt. Typically, the
  18. * SharedTempStore might be used to store work in progress that is later saved
  19. * permanently elsewhere, e.g. autosave data, multistep forms, or in-progress
  20. * changes to complex configuration that are not ready to be saved.
  21. *
  22. * Each SharedTempStore belongs to a particular owner (e.g. a user, session, or
  23. * process). Multiple owners may use the same key/value collection, and the
  24. * owner is stored along with the key/value pair.
  25. *
  26. * Every key is unique within the collection, so the SharedTempStore can check
  27. * whether a particular key is already set by a different owner. This is
  28. * useful for informing one owner that the data is already in use by another;
  29. * for example, to let one user know that another user is in the process of
  30. * editing certain data, or even to restrict other users from editing it at
  31. * the same time. It is the responsibility of the implementation to decide
  32. * when and whether one owner can use or update another owner's data.
  33. *
  34. * If you want to be able to ensure that the data belongs to the current user,
  35. * use \Drupal\Core\TempStore\PrivateTempStore.
  36. */
  37. class SharedTempStore {
  38. use DependencySerializationTrait;
  39. /**
  40. * The key/value storage object used for this data.
  41. *
  42. * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
  43. */
  44. protected $storage;
  45. /**
  46. * The lock object used for this data.
  47. *
  48. * @var \Drupal\Core\Lock\LockBackendInterface
  49. */
  50. protected $lockBackend;
  51. /**
  52. * The request stack.
  53. *
  54. * @var \Symfony\Component\HttpFoundation\RequestStack
  55. */
  56. protected $requestStack;
  57. /**
  58. * The owner key to store along with the data (e.g. a user or session ID).
  59. *
  60. * @var mixed
  61. */
  62. protected $owner;
  63. /**
  64. * The current user.
  65. *
  66. * @var \Drupal\Core\Session\AccountProxyInterface
  67. */
  68. protected $currentUser;
  69. /**
  70. * The time to live for items in seconds.
  71. *
  72. * By default, data is stored for one week (604800 seconds) before expiring.
  73. *
  74. * @var int
  75. */
  76. protected $expire;
  77. /**
  78. * Constructs a new object for accessing data from a key/value store.
  79. *
  80. * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $storage
  81. * The key/value storage object used for this data. Each storage object
  82. * represents a particular collection of data and will contain any number
  83. * of key/value pairs.
  84. * @param \Drupal\Core\Lock\LockBackendInterface $lock_backend
  85. * The lock object used for this data.
  86. * @param mixed $owner
  87. * The owner key to store along with the data (e.g. a user or session ID).
  88. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
  89. * The request stack.
  90. * @param \Drupal\Core\Session\AccountProxyInterface $current_user
  91. * The current user.
  92. * @param int $expire
  93. * The time to live for items, in seconds.
  94. */
  95. public function __construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lock_backend, $owner, RequestStack $request_stack, $current_user = NULL, $expire = 604800) {
  96. $this->storage = $storage;
  97. $this->lockBackend = $lock_backend;
  98. $this->owner = $owner;
  99. $this->requestStack = $request_stack;
  100. if (!$current_user instanceof AccountProxyInterface) {
  101. @trigger_error('Calling ' . __METHOD__ . '() without the $current_user argument is deprecated in drupal:9.2.0 and will be required in drupal:10.0.0. See https://www.drupal.org/node/3006268', E_USER_DEPRECATED);
  102. if (is_int($current_user)) {
  103. // If the $current_user argument is numeric then this object has been
  104. // instantiated with the old constructor signature.
  105. $expire = $current_user;
  106. }
  107. $current_user = \Drupal::currentUser();
  108. }
  109. $this->currentUser = $current_user;
  110. $this->expire = $expire;
  111. }
  112. /**
  113. * Retrieves a value from this SharedTempStore for a given key.
  114. *
  115. * @param string $key
  116. * The key of the data to retrieve.
  117. *
  118. * @return mixed
  119. * The data associated with the key, or NULL if the key does not exist.
  120. */
  121. public function get($key) {
  122. if ($object = $this->storage->get($key)) {
  123. return $object->data;
  124. }
  125. }
  126. /**
  127. * Retrieves a value from this SharedTempStore for a given key.
  128. *
  129. * Only returns the value if the value is owned by $this->owner.
  130. *
  131. * @param string $key
  132. * The key of the data to retrieve.
  133. *
  134. * @return mixed
  135. * The data associated with the key, or NULL if the key does not exist.
  136. */
  137. public function getIfOwner($key) {
  138. if (($object = $this->storage->get($key)) && ($object->owner == $this->owner)) {
  139. return $object->data;
  140. }
  141. }
  142. /**
  143. * Stores a particular key/value pair only if the key doesn't already exist.
  144. *
  145. * @param string $key
  146. * The key of the data to check and store.
  147. * @param mixed $value
  148. * The data to store.
  149. *
  150. * @return bool
  151. * TRUE if the data was set, or FALSE if it already existed.
  152. */
  153. public function setIfNotExists($key, $value) {
  154. $value = (object) [
  155. 'owner' => $this->owner,
  156. 'data' => $value,
  157. 'updated' => (int) $this->requestStack->getMainRequest()->server->get('REQUEST_TIME'),
  158. ];
  159. $this->ensureAnonymousSession();
  160. $set = $this->storage->setWithExpireIfNotExists($key, $value, $this->expire);
  161. return $set;
  162. }
  163. /**
  164. * Stores a particular key/value pair in this SharedTempStore.
  165. *
  166. * Only stores the given key/value pair if it does not exist yet or is owned
  167. * by $this->owner.
  168. *
  169. * @param string $key
  170. * The key of the data to store.
  171. * @param mixed $value
  172. * The data to store.
  173. *
  174. * @return bool
  175. * TRUE if the data was set, or FALSE if it already exists and is not owned
  176. * by $this->user.
  177. *
  178. * @throws \Drupal\Core\TempStore\TempStoreException
  179. * Thrown when a lock for the backend storage could not be acquired.
  180. */
  181. public function setIfOwner($key, $value) {
  182. if ($this->setIfNotExists($key, $value)) {
  183. return TRUE;
  184. }
  185. if (($object = $this->storage->get($key)) && ($object->owner == $this->owner)) {
  186. $this->set($key, $value);
  187. return TRUE;
  188. }
  189. return FALSE;
  190. }
  191. /**
  192. * Stores a particular key/value pair in this SharedTempStore.
  193. *
  194. * @param string $key
  195. * The key of the data to store.
  196. * @param mixed $value
  197. * The data to store.
  198. *
  199. * @throws \Drupal\Core\TempStore\TempStoreException
  200. * Thrown when a lock for the backend storage could not be acquired.
  201. */
  202. public function set($key, $value) {
  203. if (!$this->lockBackend->acquire($key)) {
  204. $this->lockBackend->wait($key);
  205. if (!$this->lockBackend->acquire($key)) {
  206. throw new TempStoreException("Couldn't acquire lock to update item '$key' in '{$this->storage->getCollectionName()}' temporary storage.");
  207. }
  208. }
  209. $value = (object) [
  210. 'owner' => $this->owner,
  211. 'data' => $value,
  212. 'updated' => (int) $this->requestStack->getMainRequest()->server->get('REQUEST_TIME'),
  213. ];
  214. $this->ensureAnonymousSession();
  215. $this->storage->setWithExpire($key, $value, $this->expire);
  216. $this->lockBackend->release($key);
  217. }
  218. /**
  219. * Returns the metadata associated with a particular key/value pair.
  220. *
  221. * @param string $key
  222. * The key of the data to store.
  223. *
  224. * @return \Drupal\Core\TempStore\Lock|null
  225. * An object with the owner and updated time if the key has a value, or
  226. * NULL otherwise.
  227. */
  228. public function getMetadata($key) {
  229. // Fetch the key/value pair and its metadata.
  230. $object = $this->storage->get($key);
  231. if ($object) {
  232. // Don't keep the data itself in memory.
  233. unset($object->data);
  234. return new Lock($object->owner, $object->updated);
  235. }
  236. }
  237. /**
  238. * Deletes data from the store for a given key and releases the lock on it.
  239. *
  240. * @param string $key
  241. * The key of the data to delete.
  242. *
  243. * @throws \Drupal\Core\TempStore\TempStoreException
  244. * Thrown when a lock for the backend storage could not be acquired.
  245. */
  246. public function delete($key) {
  247. if (!$this->lockBackend->acquire($key)) {
  248. $this->lockBackend->wait($key);
  249. if (!$this->lockBackend->acquire($key)) {
  250. throw new TempStoreException("Couldn't acquire lock to delete item '$key' from {$this->storage->getCollectionName()} temporary storage.");
  251. }
  252. }
  253. $this->storage->delete($key);
  254. $this->lockBackend->release($key);
  255. }
  256. /**
  257. * Deletes data from the store for a given key and releases the lock on it.
  258. *
  259. * Only delete the given key if it is owned by $this->owner.
  260. *
  261. * @param string $key
  262. * The key of the data to delete.
  263. *
  264. * @return bool
  265. * TRUE if the object was deleted or does not exist, FALSE if it exists but
  266. * is not owned by $this->owner.
  267. *
  268. * @throws \Drupal\Core\TempStore\TempStoreException
  269. * Thrown when a lock for the backend storage could not be acquired.
  270. */
  271. public function deleteIfOwner($key) {
  272. if (!$object = $this->storage->get($key)) {
  273. return TRUE;
  274. }
  275. elseif ($object->owner == $this->owner) {
  276. $this->delete($key);
  277. return TRUE;
  278. }
  279. return FALSE;
  280. }
  281. /**
  282. * Stores the owner in the session if the user is anonymous.
  283. *
  284. * This method should be called when a value is set.
  285. */
  286. protected function ensureAnonymousSession() {
  287. // If this is being run from the CLI then the request will not have a
  288. // session.
  289. if ($this->currentUser->isAnonymous() && $this->requestStack->getCurrentRequest()->hasSession()) {
  290. $this->requestStack->getCurrentRequest()->getSession()->set('core.tempstore.shared.owner', $this->owner);
  291. }
  292. }
  293. }