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;
14 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
15 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
16 use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
17 use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
18 use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
19 use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
22 * @author Fabien Potencier <fabien@symfony.com>
23 * @author Drak <drak@zikula.org>
25 class Session implements SessionInterface, \IteratorAggregate, \Countable
30 private $attributeName;
31 private $data = array();
32 private $usageIndex = 0;
35 * @param SessionStorageInterface $storage A SessionStorageInterface instance
36 * @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
37 * @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
39 public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
41 $this->storage = $storage ?: new NativeSessionStorage();
43 $attributes = $attributes ?: new AttributeBag();
44 $this->attributeName = $attributes->getName();
45 $this->registerBag($attributes);
47 $flashes = $flashes ?: new FlashBag();
48 $this->flashName = $flashes->getName();
49 $this->registerBag($flashes);
55 public function start()
57 return $this->storage->start();
63 public function has($name)
65 return $this->getAttributeBag()->has($name);
71 public function get($name, $default = null)
73 return $this->getAttributeBag()->get($name, $default);
79 public function set($name, $value)
81 $this->getAttributeBag()->set($name, $value);
89 return $this->getAttributeBag()->all();
95 public function replace(array $attributes)
97 $this->getAttributeBag()->replace($attributes);
103 public function remove($name)
105 return $this->getAttributeBag()->remove($name);
111 public function clear()
113 $this->getAttributeBag()->clear();
119 public function isStarted()
121 return $this->storage->isStarted();
125 * Returns an iterator for attributes.
127 * @return \ArrayIterator An \ArrayIterator instance
129 public function getIterator()
131 return new \ArrayIterator($this->getAttributeBag()->all());
135 * Returns the number of attributes.
137 * @return int The number of attributes
139 public function count()
141 return \count($this->getAttributeBag()->all());
149 public function getUsageIndex()
151 return $this->usageIndex;
159 public function isEmpty()
161 if ($this->isStarted()) {
164 foreach ($this->data as &$data) {
176 public function invalidate($lifetime = null)
178 $this->storage->clear();
180 return $this->migrate(true, $lifetime);
186 public function migrate($destroy = false, $lifetime = null)
188 return $this->storage->regenerate($destroy, $lifetime);
194 public function save()
196 $this->storage->save();
202 public function getId()
204 return $this->storage->getId();
210 public function setId($id)
212 if ($this->storage->getId() !== $id) {
213 $this->storage->setId($id);
220 public function getName()
222 return $this->storage->getName();
228 public function setName($name)
230 $this->storage->setName($name);
236 public function getMetadataBag()
240 return $this->storage->getMetadataBag();
246 public function registerBag(SessionBagInterface $bag)
248 $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex));
254 public function getBag($name)
256 return $this->storage->getBag($name)->getBag();
260 * Gets the flashbag interface.
262 * @return FlashBagInterface
264 public function getFlashBag()
266 return $this->getBag($this->flashName);
270 * Gets the attributebag interface.
272 * Note that this method was added to help with IDE autocompletion.
274 * @return AttributeBagInterface
276 private function getAttributeBag()
278 return $this->getBag($this->attributeName);