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\Storage\SessionStorageInterface;
15 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
16 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
17 use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
18 use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
19 use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
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 $hasBeenStarted;
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 hasBeenStarted()
151 return $this->hasBeenStarted;
159 public function isEmpty()
161 foreach ($this->data as &$data) {
173 public function invalidate($lifetime = null)
175 $this->storage->clear();
177 return $this->migrate(true, $lifetime);
183 public function migrate($destroy = false, $lifetime = null)
185 return $this->storage->regenerate($destroy, $lifetime);
191 public function save()
193 $this->storage->save();
199 public function getId()
201 return $this->storage->getId();
207 public function setId($id)
209 $this->storage->setId($id);
215 public function getName()
217 return $this->storage->getName();
223 public function setName($name)
225 $this->storage->setName($name);
231 public function getMetadataBag()
233 return $this->storage->getMetadataBag();
239 public function registerBag(SessionBagInterface $bag)
241 $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->hasBeenStarted));
247 public function getBag($name)
249 return $this->storage->getBag($name)->getBag();
253 * Gets the flashbag interface.
255 * @return FlashBagInterface
257 public function getFlashBag()
259 return $this->getBag($this->flashName);
263 * Gets the attributebag interface.
265 * Note that this method was added to help with IDE autocompletion.
267 * @return AttributeBagInterface
269 private function getAttributeBag()
271 return $this->getBag($this->attributeName);