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;
24 * @author Fabien Potencier <fabien@symfony.com>
25 * @author Drak <drak@zikula.org>
27 class Session implements SessionInterface, \IteratorAggregate, \Countable
32 * @var SessionStorageInterface
44 private $attributeName;
49 * @param SessionStorageInterface $storage A SessionStorageInterface instance
50 * @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
51 * @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
53 public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
55 $this->storage = $storage ?: new NativeSessionStorage();
57 $attributes = $attributes ?: new AttributeBag();
58 $this->attributeName = $attributes->getName();
59 $this->registerBag($attributes);
61 $flashes = $flashes ?: new FlashBag();
62 $this->flashName = $flashes->getName();
63 $this->registerBag($flashes);
69 public function start()
71 return $this->storage->start();
77 public function has($name)
79 return $this->storage->getBag($this->attributeName)->has($name);
85 public function get($name, $default = null)
87 return $this->storage->getBag($this->attributeName)->get($name, $default);
93 public function set($name, $value)
95 $this->storage->getBag($this->attributeName)->set($name, $value);
101 public function all()
103 return $this->storage->getBag($this->attributeName)->all();
109 public function replace(array $attributes)
111 $this->storage->getBag($this->attributeName)->replace($attributes);
117 public function remove($name)
119 return $this->storage->getBag($this->attributeName)->remove($name);
125 public function clear()
127 $this->storage->getBag($this->attributeName)->clear();
133 public function isStarted()
135 return $this->storage->isStarted();
139 * Returns an iterator for attributes.
141 * @return \ArrayIterator An \ArrayIterator instance
143 public function getIterator()
145 return new \ArrayIterator($this->storage->getBag($this->attributeName)->all());
149 * Returns the number of attributes.
151 * @return int The number of attributes
153 public function count()
155 return count($this->storage->getBag($this->attributeName)->all());
161 public function invalidate($lifetime = null)
163 $this->storage->clear();
165 return $this->migrate(true, $lifetime);
171 public function migrate($destroy = false, $lifetime = null)
173 return $this->storage->regenerate($destroy, $lifetime);
179 public function save()
181 $this->storage->save();
187 public function getId()
189 return $this->storage->getId();
195 public function setId($id)
197 $this->storage->setId($id);
203 public function getName()
205 return $this->storage->getName();
211 public function setName($name)
213 $this->storage->setName($name);
219 public function getMetadataBag()
221 return $this->storage->getMetadataBag();
227 public function registerBag(SessionBagInterface $bag)
229 $this->storage->registerBag($bag);
235 public function getBag($name)
237 return $this->storage->getBag($name);
241 * Gets the flashbag interface.
243 * @return FlashBagInterface
245 public function getFlashBag()
247 return $this->getBag($this->flashName);