SharedTempStoreFactory.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Drupal\Core\TempStore;
  3. use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface;
  4. use Drupal\Core\Lock\LockBackendInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. /**
  7. * Creates a shared temporary storage for a collection.
  8. */
  9. class SharedTempStoreFactory {
  10. /**
  11. * The storage factory creating the backend to store the data.
  12. *
  13. * @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface
  14. */
  15. protected $storageFactory;
  16. /**
  17. * The lock object used for this data.
  18. *
  19. * @var \Drupal\Core\Lock\LockBackendInterface
  20. */
  21. protected $lockBackend;
  22. /**
  23. * The request stack.
  24. *
  25. * @var \Symfony\Component\HttpFoundation\RequestStack
  26. */
  27. protected $requestStack;
  28. /**
  29. * The time to live for items in seconds.
  30. *
  31. * @var int
  32. */
  33. protected $expire;
  34. /**
  35. * Constructs a Drupal\Core\TempStore\SharedTempStoreFactory object.
  36. *
  37. * @param \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $storage_factory
  38. * The key/value store factory.
  39. * @param \Drupal\Core\Lock\LockBackendInterface $lock_backend
  40. * The lock object used for this data.
  41. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
  42. * The request stack.
  43. * @param int $expire
  44. * The time to live for items, in seconds.
  45. */
  46. public function __construct(KeyValueExpirableFactoryInterface $storage_factory, LockBackendInterface $lock_backend, RequestStack $request_stack, $expire = 604800) {
  47. $this->storageFactory = $storage_factory;
  48. $this->lockBackend = $lock_backend;
  49. $this->requestStack = $request_stack;
  50. $this->expire = $expire;
  51. }
  52. /**
  53. * Creates a SharedTempStore for the current user or anonymous session.
  54. *
  55. * @param string $collection
  56. * The collection name to use for this key/value store. This is typically
  57. * a shared namespace or module name, e.g. 'views', 'entity', etc.
  58. * @param mixed $owner
  59. * (optional) The owner of this SharedTempStore. By default, the
  60. * SharedTempStore is owned by the currently authenticated user, or by the
  61. * active anonymous session if no user is logged in.
  62. *
  63. * @return \Drupal\Core\TempStore\SharedTempStore
  64. * An instance of the key/value store.
  65. */
  66. public function get($collection, $owner = NULL) {
  67. // Use the currently authenticated user ID or the active user ID unless
  68. // the owner is overridden.
  69. if (!isset($owner)) {
  70. $owner = \Drupal::currentUser()->id() ?: session_id();
  71. }
  72. // Store the data for this collection in the database.
  73. $storage = $this->storageFactory->get("tempstore.shared.$collection");
  74. return new SharedTempStore($storage, $this->lockBackend, $owner, $this->requestStack, $this->expire);
  75. }
  76. }