3 namespace Drupal\Core\TempStore;
5 use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface;
6 use Drupal\Core\Lock\LockBackendInterface;
7 use Drupal\Core\Session\AccountProxyInterface;
8 use Symfony\Component\HttpFoundation\RequestStack;
11 * Creates a PrivateTempStore object for a given collection.
13 class PrivateTempStoreFactory {
16 * The storage factory creating the backend to store the data.
18 * @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface
20 protected $storageFactory;
23 * The lock object used for this data.
25 * @var \Drupal\Core\Lock\LockBackendInterface
27 protected $lockBackend;
32 * @var \Drupal\Core\Session\AccountProxyInterface
34 protected $currentUser;
39 * @var \Symfony\Component\HttpFoundation\RequestStack
41 protected $requestStack;
44 * The time to live for items in seconds.
51 * Constructs a Drupal\Core\TempStore\PrivateTempStoreFactory object.
53 * @param \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $storage_factory
54 * The key/value store factory.
55 * @param \Drupal\Core\Lock\LockBackendInterface $lock_backend
56 * The lock object used for this data.
57 * @param \Drupal\Core\Session\AccountProxyInterface $current_user
58 * The current account.
59 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
62 * The time to live for items, in seconds.
64 public function __construct(KeyValueExpirableFactoryInterface $storage_factory, LockBackendInterface $lock_backend, AccountProxyInterface $current_user, RequestStack $request_stack, $expire = 604800) {
65 $this->storageFactory = $storage_factory;
66 $this->lockBackend = $lock_backend;
67 $this->currentUser = $current_user;
68 $this->requestStack = $request_stack;
69 $this->expire = $expire;
73 * Creates a PrivateTempStore.
75 * @param string $collection
76 * The collection name to use for this key/value store. This is typically
77 * a shared namespace or module name, e.g. 'views', 'entity', etc.
79 * @return \Drupal\Core\TempStore\PrivateTempStore
80 * An instance of the key/value store.
82 public function get($collection) {
83 // Store the data for this collection in the database.
84 $storage = $this->storageFactory->get("tempstore.private.$collection");
85 return new PrivateTempStore($storage, $this->lockBackend, $this->currentUser, $this->requestStack, $this->expire);