4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Session\Storage;
14 use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
19 * Adds metadata to the session.
21 * @author Drak <drak@zikula.org>
23 class MetadataBag implements SessionBagInterface
32 private $name = '__metadata';
42 protected $meta = array(self::CREATED => 0, self::UPDATED => 0, self::LIFETIME => 0);
54 private $updateThreshold;
59 * @param string $storageKey The key used to store bag in the session
60 * @param int $updateThreshold The time to wait between two UPDATED updates
62 public function __construct($storageKey = '_sf2_meta', $updateThreshold = 0)
64 $this->storageKey = $storageKey;
65 $this->updateThreshold = $updateThreshold;
71 public function initialize(array &$array)
73 $this->meta = &$array;
75 if (isset($array[self::CREATED])) {
76 $this->lastUsed = $this->meta[self::UPDATED];
79 if ($timeStamp - $array[self::UPDATED] >= $this->updateThreshold) {
80 $this->meta[self::UPDATED] = $timeStamp;
83 $this->stampCreated();
88 * Gets the lifetime that the session cookie was set with.
92 public function getLifetime()
94 return $this->meta[self::LIFETIME];
98 * Stamps a new session's metadata.
100 * @param int $lifetime Sets the cookie lifetime for the session cookie. A null value
101 * will leave the system settings unchanged, 0 sets the cookie
102 * to expire with browser session. Time is in seconds, and is
103 * not a Unix timestamp.
105 public function stampNew($lifetime = null)
107 $this->stampCreated($lifetime);
113 public function getStorageKey()
115 return $this->storageKey;
119 * Gets the created timestamp metadata.
121 * @return int Unix timestamp
123 public function getCreated()
125 return $this->meta[self::CREATED];
129 * Gets the last used metadata.
131 * @return int Unix timestamp
133 public function getLastUsed()
135 return $this->lastUsed;
141 public function clear()
149 public function getName()
157 * @param string $name
159 public function setName($name)
164 private function stampCreated($lifetime = null)
167 $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp;
168 $this->meta[self::LIFETIME] = (null === $lifetime) ? ini_get('session.cookie_lifetime') : $lifetime;