PrivateTempStoreFactory.php 2.6 KB

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