annotate core/modules/user/src/SharedTempStoreFactory.php @ 9:1fc0ff908d1f

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