Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. MetadataBag.php

class MetadataBag

Same name in this branch
  1. 11.1.x core/lib/Drupal/Core/Session/MetadataBag.php \Drupal\Core\Session\MetadataBag

Metadata container.

Adds metadata to the session.

@author Drak <drak@zikula.org>

Hierarchy

  • class \Symfony\Component\HttpFoundation\Session\Storage\MetadataBag implements \Symfony\Component\HttpFoundation\Session\SessionBagInterface

Expanded class hierarchy of MetadataBag

3 files declare their use of MetadataBag
MetadataBag.php in core/lib/Drupal/Core/Session/MetadataBag.php
Session.php in vendor/symfony/http-foundation/Session/Session.php
SessionInterface.php in vendor/symfony/http-foundation/Session/SessionInterface.php

File

vendor/symfony/http-foundation/Session/Storage/MetadataBag.php, line 23

Namespace

Symfony\Component\HttpFoundation\Session\Storage
View source
class MetadataBag implements SessionBagInterface {
    public const CREATED = 'c';
    public const UPDATED = 'u';
    public const LIFETIME = 'l';
    protected array $meta = [
        self::CREATED => 0,
        self::UPDATED => 0,
        self::LIFETIME => 0,
    ];
    private string $name = '__metadata';
    private int $lastUsed;
    
    /**
     * @param string $storageKey      The key used to store bag in the session
     * @param int    $updateThreshold The time to wait between two UPDATED updates
     */
    public function __construct(string $storageKey = '_sf2_meta', int $updateThreshold = 0) {
    }
    public function initialize(array &$array) : void {
        $this->meta =& $array;
        if (isset($array[self::CREATED])) {
            $this->lastUsed = $this->meta[self::UPDATED];
            $timeStamp = time();
            if ($timeStamp - $array[self::UPDATED] >= $this->updateThreshold) {
                $this->meta[self::UPDATED] = $timeStamp;
            }
        }
        else {
            $this->stampCreated();
        }
    }
    
    /**
     * Gets the lifetime that the session cookie was set with.
     */
    public function getLifetime() : int {
        return $this->meta[self::LIFETIME];
    }
    
    /**
     * Stamps a new session's metadata.
     *
     * @param int|null $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(?int $lifetime = null) : void {
        $this->stampCreated($lifetime);
    }
    public function getStorageKey() : string {
        return $this->storageKey;
    }
    
    /**
     * Gets the created timestamp metadata.
     *
     * @return int Unix timestamp
     */
    public function getCreated() : int {
        return $this->meta[self::CREATED];
    }
    
    /**
     * Gets the last used metadata.
     *
     * @return int Unix timestamp
     */
    public function getLastUsed() : int {
        return $this->lastUsed;
    }
    public function clear() : mixed {
        // nothing to do
        return null;
    }
    public function getName() : string {
        return $this->name;
    }
    
    /**
     * Sets name.
     */
    public function setName(string $name) : void {
        $this->name = $name;
    }
    private function stampCreated(?int $lifetime = null) : void {
        $timeStamp = time();
        $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp;
        $this->meta[self::LIFETIME] = $lifetime ?? (int) \ini_get('session.cookie_lifetime');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
MetadataBag::$lastUsed private property
MetadataBag::$meta protected property
MetadataBag::$name private property
MetadataBag::clear public function Clears out data from bag. Overrides SessionBagInterface::clear
MetadataBag::CREATED public 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&#039;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 public constant
MetadataBag::setName public function Sets name.
MetadataBag::stampCreated private function
MetadataBag::stampNew public function Stamps a new session&#039;s metadata. 1
MetadataBag::UPDATED public constant
MetadataBag::__construct public function 1

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal