class TempStoreFactory

Creates a key/value storage object for the current user or anonymous session.

Hierarchy

Expanded class hierarchy of TempStoreFactory

1 file declares its use of TempStoreFactory
TempStoreDatabaseTest.php in drupal/core/modules/user/lib/Drupal/user/Tests/TempStoreDatabaseTest.php
Definition of Drupal\user\Tests\TempStoreDatabaseTest.

File

drupal/core/modules/user/lib/Drupal/user/TempStoreFactory.php, line 17
Definition of Drupal\user\TempStoreFactory.

Namespace

Drupal\user
View source
class TempStoreFactory {

  /**
   * The connection object used for this data.
   *
   * @var Drupal\Core\Database\Connection $connection
   */
  protected $connection;

  /**
   * The lock object used for this data.
   *
   * @var Drupal\Core\Lock\LockBackendInterface $lockBackend
   */
  protected $lockBackend;

  /**
   * Constructs a Drupal\user\TempStoreFactory object.
   *
   * @param Drupal\Core\Database\Connection $connection
   *   The connection object used for this data.
   * @param Drupal\Core\Lock\LockBackendInterface $lockBackend
   *   The lock object used for this data.
   */
  function __construct(Connection $connection, LockBackendInterface $lockBackend) {
    $this->connection = $connection;
    $this->lockBackend = $lockBackend;
  }

  /**
   * Creates a TempStore for the current user or anonymous session.
   *
   * @param string $collection
   *   The collection name to use for this key/value store. This is typically
   *   a shared namespace or module name, e.g. 'views', 'entity', etc.
   * @param mixed $owner
   *   (optional) The owner of this TempStore. By default, the TempStore is
   *   owned by the currently authenticated user, or by the active anonymous
   *   session if no user is logged in.
   *
   * @return Drupal\user\TempStore
   *   An instance of the the key/value store.
   */
  function get($collection, $owner = NULL) {

    // Use the currently authenticated user ID or the active user ID unless
    // the owner is overridden.
    if (!isset($owner)) {
      $owner = $GLOBALS['user']->uid ?: session_id();
    }

    // Store the data for this collection in the database.
    $storage = new DatabaseStorageExpirable($collection, $this->connection);
    return new TempStore($storage, $this->lockBackend, $owner);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TempStoreFactory::$connection protected property The connection object used for this data.
TempStoreFactory::$lockBackend protected property The lock object used for this data.
TempStoreFactory::get function Creates a TempStore for the current user or anonymous session.
TempStoreFactory::__construct function Constructs a Drupal\user\TempStoreFactory object.