MetadataBag

class MetadataBag implements SessionBagInterface

Metadata container.

Adds metadata to the session.

Constants

CREATED = 'c';

UPDATED = 'u';

LIFETIME = 'l';

Properties

protected array $meta

Methods

public __construct(string $storageKey = '_sf2_meta', int $updateThreshold) No description
public initialize(array $array) Initializes the Bag.
public int getLifetime() Gets the lifetime that the session cookie was set with.
public stampNew(int $lifetime = null) Stamps a new session's metadata.
public string getStorageKey() Gets the storage key for this bag.
public int getCreated() Gets the created timestamp metadata.
public int getLastUsed() Gets the last used metadata.
public mixed clear() Clears out data from bag.
public string getName() Gets this bag's name.
public setName(string $name) Sets name.

Details

at line 60

__construct()

public __construct(string $storageKey = '_sf2_meta', int $updateThreshold)

Parameters

string $storageKey The key used to store bag in the session
int $updateThreshold The time to wait between two UPDATED updates
at line 69

initialize()

public initialize(array $array)

Initializes the Bag.

Parameters

array $array
at line 90

getLifetime()

public int getLifetime()

Gets the lifetime that the session cookie was set with.

Return Value

int
at line 103

stampNew()

public stampNew(int $lifetime = null)

Stamps a new session's metadata.

Parameters

int $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.
at line 111

getStorageKey()

public string getStorageKey()

Gets the storage key for this bag.

Return Value

string
at line 121

getCreated()

public int getCreated()

Gets the created timestamp metadata.

Return Value

int Unix timestamp
at line 131

getLastUsed()

public int getLastUsed()

Gets the last used metadata.

Return Value

int Unix timestamp
at line 139

clear()

public mixed clear()

Clears out data from bag.

Return Value

mixed Whatever data was contained
at line 147

getName()

public string getName()

Gets this bag's name.

Return Value

string
at line 157

setName()

public setName(string $name)

Sets name.

Parameters

string $name

Source code

<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\HttpFoundation\Session\Storage;

use Symfony\Component\HttpFoundation\Session\SessionBagInterface;

/**
 * Metadata container.
 *
 * Adds metadata to the session.
 *
 * @author Drak <drak@zikula.org>
 */
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(self::CREATED => 0, self::UPDATED => 0, self::LIFETIME => 0);

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

    /**
     * @var int
     */
    private $updateThreshold;

    /**
     * @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)
    {
        $this->storageKey = $storageKey;
        $this->updateThreshold = $updateThreshold;
    }

    /**
     * {@inheritdoc}
     */
    public function initialize(array &$array)
    {
        $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.
     *
     * @return int
     */
    public function getLifetime()
    {
        return $this->meta[self::LIFETIME];
    }

    /**
     * Stamps a new session's metadata.
     *
     * @param int $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 int Unix timestamp
     */
    public function getCreated()
    {
        return $this->meta[self::CREATED];
    }

    /**
     * Gets the last used metadata.
     *
     * @return int 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;
    }
}