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;
57 * @param string $storageKey The key used to store bag in the session
58 * @param int $updateThreshold The time to wait between two UPDATED updates
60 public function __construct($storageKey = '_sf2_meta', $updateThreshold = 0)
62 $this->storageKey
= $storageKey;
63 $this->updateThreshold
= $updateThreshold;
69 public function initialize(array &$array)
71 $this->meta
= &$array;
73 if (isset($array[self
::CREATED
])) {
74 $this->lastUsed
= $this->meta
[self
::UPDATED
];
77 if ($timeStamp - $array[self
::UPDATED
] >= $this->updateThreshold
) {
78 $this->meta
[self
::UPDATED
] = $timeStamp;
81 $this->stampCreated();
86 * Gets the lifetime that the session cookie was set with.
90 public function getLifetime()
92 return $this->meta
[self
::LIFETIME
];
96 * Stamps a new session's metadata.
98 * @param int $lifetime Sets the cookie lifetime for the session cookie. A null value
99 * will leave the system settings unchanged, 0 sets the cookie
100 * to expire with browser session. Time is in seconds, and is
101 * not a Unix timestamp.
103 public function stampNew($lifetime = null)
105 $this->stampCreated($lifetime);
111 public function getStorageKey()
113 return $this->storageKey
;
117 * Gets the created timestamp metadata.
119 * @return int Unix timestamp
121 public function getCreated()
123 return $this->meta
[self
::CREATED
];
127 * Gets the last used metadata.
129 * @return int Unix timestamp
131 public function getLastUsed()
133 return $this->lastUsed
;
139 public function clear()
147 public function getName()
155 * @param string $name
157 public function setName($name)
162 private function stampCreated($lifetime = null)
165 $this->meta
[self
::CREATED
] = $this->meta
[self
::UPDATED
] = $this->lastUsed
= $timeStamp;
166 $this->meta
[self
::LIFETIME
] = (null === $lifetime) ?
ini_get('session.cookie_lifetime') : $lifetime;