3 namespace Drupal\Core\TempStore;
5 use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface;
6 use Drupal\Core\Lock\LockBackendInterface;
7 use Symfony\Component\HttpFoundation\RequestStack;
10 * Creates a shared temporary storage for a collection.
12 class SharedTempStoreFactory {
15 * The storage factory creating the backend to store the data.
17 * @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface
19 protected $storageFactory;
22 * The lock object used for this data.
24 * @var \Drupal\Core\Lock\LockBackendInterface
26 protected $lockBackend;
31 * @var \Symfony\Component\HttpFoundation\RequestStack
33 protected $requestStack;
36 * The time to live for items in seconds.
43 * Constructs a Drupal\Core\TempStore\SharedTempStoreFactory object.
45 * @param \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface $storage_factory
46 * The key/value store factory.
47 * @param \Drupal\Core\Lock\LockBackendInterface $lock_backend
48 * The lock object used for this data.
49 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
52 * The time to live for items, in seconds.
54 public function __construct(KeyValueExpirableFactoryInterface $storage_factory, LockBackendInterface $lock_backend, RequestStack $request_stack, $expire = 604800) {
55 $this->storageFactory = $storage_factory;
56 $this->lockBackend = $lock_backend;
57 $this->requestStack = $request_stack;
58 $this->expire = $expire;
62 * Creates a SharedTempStore for the current user or anonymous session.
64 * @param string $collection
65 * The collection name to use for this key/value store. This is typically
66 * a shared namespace or module name, e.g. 'views', 'entity', etc.
68 * (optional) The owner of this SharedTempStore. By default, the
69 * SharedTempStore is owned by the currently authenticated user, or by the
70 * active anonymous session if no user is logged in.
72 * @return \Drupal\Core\TempStore\SharedTempStore
73 * An instance of the key/value store.
75 public function get($collection, $owner = NULL) {
76 // Use the currently authenticated user ID or the active user ID unless
77 // the owner is overridden.
79 $owner = \Drupal::currentUser()->id() ?: session_id();
82 // Store the data for this collection in the database.
83 $storage = $this->storageFactory->get("tempstore.shared.$collection");
84 return new SharedTempStore($storage, $this->lockBackend, $owner, $this->requestStack, $this->expire);