Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / TempStore / SharedTempStoreFactory.php
1 <?php
2
3 namespace Drupal\Core\TempStore;
4
5 use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface;
6 use Drupal\Core\Lock\LockBackendInterface;
7 use Symfony\Component\HttpFoundation\RequestStack;
8
9 /**
10  * Creates a shared temporary storage for a collection.
11  */
12 class SharedTempStoreFactory {
13
14   /**
15    * The storage factory creating the backend to store the data.
16    *
17    * @var \Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface
18    */
19   protected $storageFactory;
20
21   /**
22    * The lock object used for this data.
23    *
24    * @var \Drupal\Core\Lock\LockBackendInterface
25    */
26   protected $lockBackend;
27
28   /**
29    * The request stack.
30    *
31    * @var \Symfony\Component\HttpFoundation\RequestStack
32    */
33   protected $requestStack;
34
35   /**
36    * The time to live for items in seconds.
37    *
38    * @var int
39    */
40   protected $expire;
41
42   /**
43    * Constructs a Drupal\Core\TempStore\SharedTempStoreFactory object.
44    *
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
50    *   The request stack.
51    * @param int $expire
52    *   The time to live for items, in seconds.
53    */
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;
59   }
60
61   /**
62    * Creates a SharedTempStore for the current user or anonymous session.
63    *
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.
67    * @param mixed $owner
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.
71    *
72    * @return \Drupal\Core\TempStore\SharedTempStore
73    *   An instance of the key/value store.
74    */
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.
78     if (!isset($owner)) {
79       $owner = \Drupal::currentUser()->id() ?: session_id();
80     }
81
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);
85   }
86
87 }