class MetadataBag

Metadata container.

Adds metadata to the session.

@author Drak <drak@zikula.org>

Hierarchy

Expanded class hierarchy of MetadataBag

5 files declare their use of MetadataBag
MetadataBagTest.php in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Tests/Session/Storage/MetadataBagTest.php
MockArraySessionStorage.php in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php
NativeSessionStorage.php in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php
SessionInterface.php in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/SessionInterface.php
SessionStorageInterface.php in drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php

File

drupal/core/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Session/Storage/MetadataBag.php, line 23

Namespace

Symfony\Component\HttpFoundation\Session\Storage
View source
class MetadataBag implements SessionBagInterface {
  const CREATED = 'c';
  const UPDATED = 'u';
  const LIFETIME = 'l';

  /**
   * @var string
   */
  private $name = '__metadata';

  /**
   * @var string
   */
  private $storageKey;

  /**
   * @var array
   */
  protected $meta = array();

  /**
   * Unix timestamp.
   *
   * @var integer
   */
  private $lastUsed;

  /**
   * Constructor.
   *
   * @param string $storageKey The key used to store bag in the session.
   */
  public function __construct($storageKey = '_sf2_meta') {
    $this->storageKey = $storageKey;
    $this->meta = array(
      self::CREATED => 0,
      self::UPDATED => 0,
      self::LIFETIME => 0,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function initialize(array &$array) {
    $this->meta =& $array;
    if (isset($array[self::CREATED])) {
      $this->lastUsed = $this->meta[self::UPDATED];
      $this->meta[self::UPDATED] = time();
    }
    else {
      $this
        ->stampCreated();
    }
  }

  /**
   * Gets the lifetime that the session cookie was set with.
   *
   * @return integer
   */
  public function getLifetime() {
    return $this->meta[self::LIFETIME];
  }

  /**
   * Stamps a new session's metadata.
   *
   * @param integer $lifetime Sets the cookie lifetime for the session cookie. A null value
   *                          will leave the system settings unchanged, 0 sets the cookie
   *                          to expire with browser session. Time is in seconds, and is
   *                          not a Unix timestamp.
   */
  public function stampNew($lifetime = null) {
    $this
      ->stampCreated($lifetime);
  }

  /**
   * {@inheritdoc}
   */
  public function getStorageKey() {
    return $this->storageKey;
  }

  /**
   * Gets the created timestamp metadata.
   *
   * @return integer Unix timestamp
   */
  public function getCreated() {
    return $this->meta[self::CREATED];
  }

  /**
   * Gets the last used metadata.
   *
   * @return integer Unix timestamp
   */
  public function getLastUsed() {
    return $this->lastUsed;
  }

  /**
   * {@inheritdoc}
   */
  public function clear() {

    // nothing to do
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this->name;
  }

  /**
   * Sets name.
   *
   * @param string $name
   */
  public function setName($name) {
    $this->name = $name;
  }
  private function stampCreated($lifetime = null) {
    $timeStamp = time();
    $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp;
    $this->meta[self::LIFETIME] = null === $lifetime ? ini_get('session.cookie_lifetime') : $lifetime;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MetadataBag::$lastUsed private property Unix timestamp.
MetadataBag::$meta protected property
MetadataBag::$name private property
MetadataBag::$storageKey private property
MetadataBag::clear public function Clears out data from bag. Overrides SessionBagInterface::clear
MetadataBag::CREATED constant
MetadataBag::getCreated public function Gets the created timestamp metadata.
MetadataBag::getLastUsed public function Gets the last used metadata.
MetadataBag::getLifetime public function Gets the lifetime that the session cookie was set with.
MetadataBag::getName public function Gets this bag's name Overrides SessionBagInterface::getName
MetadataBag::getStorageKey public function Gets the storage key for this bag. Overrides SessionBagInterface::getStorageKey
MetadataBag::initialize public function Initializes the Bag Overrides SessionBagInterface::initialize
MetadataBag::LIFETIME constant
MetadataBag::setName public function Sets name.
MetadataBag::stampCreated private function
MetadataBag::stampNew public function Stamps a new session's metadata.
MetadataBag::UPDATED constant
MetadataBag::__construct public function Constructor.