2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-02-17 13:55:05 +00:00
|
|
|
*/
|
|
|
|
|
2012-02-08 23:15:47 +00:00
|
|
|
namespace Symfony\Component\HttpFoundation\Session;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2012-02-11 11:04:05 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
|
2012-02-08 23:15:47 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
|
2012-03-14 15:14:09 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2010-02-17 13:55:05 +00:00
|
|
|
/**
|
2010-07-09 08:26:22 +01:00
|
|
|
* Session.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-11-29 04:35:09 +00:00
|
|
|
* @author Drak <drak@zikula.org>
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-02-17 13:55:05 +00:00
|
|
|
*/
|
2012-03-15 20:41:06 +00:00
|
|
|
class Session implements SessionInterface, \IteratorAggregate, \Countable
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2011-11-29 04:35:09 +00:00
|
|
|
/**
|
|
|
|
* Storage driver.
|
|
|
|
*
|
2012-02-11 11:04:05 +00:00
|
|
|
* @var SessionStorageInterface
|
2011-11-29 04:35:09 +00:00
|
|
|
*/
|
2010-07-09 08:26:22 +01:00
|
|
|
protected $storage;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2012-03-15 04:06:39 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $flashName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $attributeName;
|
|
|
|
|
2012-03-28 16:22:12 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $metaName;
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-07-09 08:26:22 +01:00
|
|
|
* Constructor.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2012-03-04 11:04:40 +00:00
|
|
|
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
|
2012-02-08 09:46:38 +00:00
|
|
|
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
|
|
|
|
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
|
2012-03-28 16:22:12 +01:00
|
|
|
* @param MetaBag $meta A MetaBag instance (defaults null for default MetaBag)
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2012-03-28 16:22:12 +01:00
|
|
|
public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null, MetaBag $meta = null)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2012-03-14 15:14:09 +00:00
|
|
|
$this->storage = $storage ?: new NativeSessionStorage();
|
2012-03-15 04:06:39 +00:00
|
|
|
|
2012-03-28 16:22:12 +01:00
|
|
|
$attributes = $attributes ?: new AttributeBag();
|
|
|
|
$this->attributeName = $attributes->getName();
|
|
|
|
$this->registerBag($attributes);
|
|
|
|
|
|
|
|
$flashes = $flashes ?: new FlashBag();
|
|
|
|
$this->flashName = $flashes->getName();
|
|
|
|
$this->registerBag($flashes);
|
2012-03-15 04:06:39 +00:00
|
|
|
|
2012-03-28 16:22:12 +01:00
|
|
|
$metaBag = $meta ?: new MetaBag();
|
|
|
|
$this->metaName = $metaBag->getName();
|
|
|
|
$this->registerBag($metaBag);
|
2010-07-09 08:26:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2010-07-09 08:26:22 +01:00
|
|
|
*/
|
|
|
|
public function start()
|
|
|
|
{
|
2011-11-29 04:35:09 +00:00
|
|
|
return $this->storage->start();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2010-08-20 22:20:30 +01:00
|
|
|
*/
|
|
|
|
public function has($name)
|
|
|
|
{
|
2012-03-15 04:06:39 +00:00
|
|
|
return $this->storage->getBag($this->attributeName)->has($name);
|
2010-08-20 22:20:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-08-20 22:20:30 +01:00
|
|
|
public function get($name, $default = null)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2012-03-15 04:06:39 +00:00
|
|
|
return $this->storage->getBag($this->attributeName)->get($name, $default);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-08-20 22:20:30 +01:00
|
|
|
public function set($name, $value)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2012-03-15 04:06:39 +00:00
|
|
|
$this->storage->getBag($this->attributeName)->set($name, $value);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-07-15 17:06:18 +01:00
|
|
|
public function all()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2012-03-15 04:06:39 +00:00
|
|
|
return $this->storage->getBag($this->attributeName)->all();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-07-15 17:06:18 +01:00
|
|
|
public function replace(array $attributes)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2012-03-15 04:06:39 +00:00
|
|
|
$this->storage->getBag($this->attributeName)->replace($attributes);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-08-20 22:20:30 +01:00
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2010-08-20 22:20:30 +01:00
|
|
|
*/
|
|
|
|
public function remove($name)
|
|
|
|
{
|
2012-03-15 04:06:39 +00:00
|
|
|
return $this->storage->getBag($this->attributeName)->remove($name);
|
2010-08-20 22:20:30 +01:00
|
|
|
}
|
|
|
|
|
2010-10-19 12:09:48 +01:00
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2010-10-19 12:09:48 +01:00
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
2012-03-15 04:06:39 +00:00
|
|
|
$this->storage->getBag($this->attributeName)->clear();
|
2010-10-19 12:09:48 +01:00
|
|
|
}
|
|
|
|
|
2012-03-28 16:22:12 +01:00
|
|
|
/**
|
|
|
|
* Returns an iterator for attributes.
|
|
|
|
*
|
|
|
|
* @return \ArrayIterator An \ArrayIterator instance
|
|
|
|
*/
|
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
return new \ArrayIterator($this->storage->getBag($this->attributeName)->all());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of attributes.
|
|
|
|
*
|
|
|
|
* @return int The number of attributes
|
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return count($this->storage->getBag($this->attributeName)->all());
|
|
|
|
}
|
|
|
|
|
2010-10-19 12:09:48 +01:00
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2010-10-19 12:09:48 +01:00
|
|
|
*/
|
2010-10-12 07:58:12 +01:00
|
|
|
public function invalidate()
|
|
|
|
{
|
2011-11-29 04:35:09 +00:00
|
|
|
$this->storage->clear();
|
|
|
|
|
|
|
|
return $this->storage->regenerate(true);
|
2010-10-12 07:58:12 +01:00
|
|
|
}
|
|
|
|
|
2011-01-23 20:43:07 +00:00
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* {@inheritdoc}
|
2011-01-23 20:43:07 +00:00
|
|
|
*/
|
2011-11-29 04:35:09 +00:00
|
|
|
public function migrate($destroy = false)
|
|
|
|
{
|
|
|
|
return $this->storage->regenerate($destroy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function save()
|
2011-01-23 20:43:07 +00:00
|
|
|
{
|
2011-11-29 04:35:09 +00:00
|
|
|
$this->storage->save();
|
2011-01-23 20:43:07 +00:00
|
|
|
}
|
|
|
|
|
2011-01-03 16:00:28 +00:00
|
|
|
/**
|
2012-03-14 11:53:34 +00:00
|
|
|
* {@inheritdoc}
|
2011-01-03 16:00:28 +00:00
|
|
|
*/
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->storage->getId();
|
|
|
|
}
|
|
|
|
|
2012-02-11 14:30:36 +00:00
|
|
|
/**
|
2012-03-14 11:53:34 +00:00
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setId($id)
|
|
|
|
{
|
|
|
|
$this->storage->setId($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->storage->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->storage->setName($name);
|
|
|
|
}
|
|
|
|
|
2012-03-28 16:22:12 +01:00
|
|
|
/**
|
|
|
|
* Gets session meta.
|
|
|
|
*
|
|
|
|
* @return MetaBag
|
|
|
|
*/
|
|
|
|
public function getMeta()
|
|
|
|
{
|
|
|
|
return $this->getBag($this->metaName);
|
|
|
|
}
|
|
|
|
|
2012-03-14 11:53:34 +00:00
|
|
|
/**
|
|
|
|
* Registers a SessionBagInterface with the session.
|
2012-02-11 14:30:36 +00:00
|
|
|
*
|
|
|
|
* @param SessionBagInterface $bag
|
|
|
|
*/
|
2012-02-08 09:46:38 +00:00
|
|
|
public function registerBag(SessionBagInterface $bag)
|
|
|
|
{
|
|
|
|
$this->storage->registerBag($bag);
|
|
|
|
}
|
|
|
|
|
2012-02-11 14:30:36 +00:00
|
|
|
/**
|
|
|
|
* Get's a bag instance.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return SessionBagInterface
|
|
|
|
*/
|
2012-02-08 09:46:38 +00:00
|
|
|
public function getBag($name)
|
|
|
|
{
|
|
|
|
return $this->storage->getBag($name);
|
|
|
|
}
|
|
|
|
|
2011-11-29 04:35:09 +00:00
|
|
|
/**
|
2012-02-08 09:46:38 +00:00
|
|
|
* Gets the flashbag interface.
|
2011-11-29 04:35:09 +00:00
|
|
|
*
|
2012-02-02 11:07:02 +00:00
|
|
|
* @return FlashBagInterface
|
2011-11-29 04:35:09 +00:00
|
|
|
*/
|
2012-02-11 12:09:41 +00:00
|
|
|
public function getFlashBag()
|
2010-08-27 08:07:47 +01:00
|
|
|
{
|
2012-03-15 04:06:39 +00:00
|
|
|
return $this->getBag($this->flashName);
|
2010-08-27 08:07:47 +01:00
|
|
|
}
|
2012-02-11 11:43:38 +00:00
|
|
|
|
|
|
|
// the following methods are kept for compatibility with Symfony 2.0 (they will be removed for Symfony 2.3)
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* @return array
|
|
|
|
*
|
2012-02-11 14:30:36 +00:00
|
|
|
* @deprecated since 2.1, will be removed from 2.3
|
2012-02-11 11:43:38 +00:00
|
|
|
*/
|
|
|
|
public function getFlashes()
|
|
|
|
{
|
2012-02-12 05:04:04 +00:00
|
|
|
$all = $this->getBag($this->flashName)->all();
|
|
|
|
|
|
|
|
$return = array();
|
|
|
|
if ($all) {
|
|
|
|
foreach ($all as $name => $array) {
|
|
|
|
$return[$name] = reset($array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
2012-02-11 11:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* @param array $values
|
|
|
|
*
|
2012-02-11 14:30:36 +00:00
|
|
|
* @deprecated since 2.1, will be removed from 2.3
|
2012-02-11 11:43:38 +00:00
|
|
|
*/
|
|
|
|
public function setFlashes($values)
|
|
|
|
{
|
2012-02-12 05:04:04 +00:00
|
|
|
foreach ($values as $name => $value) {
|
|
|
|
$this->getBag($this->flashName)->set($name, $value);
|
|
|
|
}
|
2012-02-11 11:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param string $default
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*
|
2012-02-11 14:30:36 +00:00
|
|
|
* @deprecated since 2.1, will be removed from 2.3
|
2012-02-11 11:43:38 +00:00
|
|
|
*/
|
|
|
|
public function getFlash($name, $default = null)
|
|
|
|
{
|
2012-02-12 05:04:04 +00:00
|
|
|
$return = $this->getBag($this->flashName)->get($name);
|
|
|
|
|
|
|
|
return empty($return) ? $default : reset($return);
|
2012-02-11 11:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
*
|
2012-02-11 14:30:36 +00:00
|
|
|
* @deprecated since 2.1, will be removed from 2.3
|
2012-02-11 11:43:38 +00:00
|
|
|
*/
|
|
|
|
public function setFlash($name, $value)
|
|
|
|
{
|
2012-02-12 05:04:04 +00:00
|
|
|
$this->getBag($this->flashName)->set($name, $value);
|
2012-02-11 11:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*
|
2012-02-11 14:30:36 +00:00
|
|
|
* @deprecated since 2.1, will be removed from 2.3
|
2012-02-11 11:43:38 +00:00
|
|
|
*/
|
|
|
|
public function hasFlash($name)
|
|
|
|
{
|
2012-02-12 05:04:04 +00:00
|
|
|
return $this->getBag($this->flashName)->has($name);
|
2012-02-11 11:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* @param string $name
|
|
|
|
*
|
2012-02-11 14:30:36 +00:00
|
|
|
* @deprecated since 2.1, will be removed from 2.3
|
2012-02-11 11:43:38 +00:00
|
|
|
*/
|
|
|
|
public function removeFlash($name)
|
|
|
|
{
|
2012-02-12 05:04:04 +00:00
|
|
|
$this->getBag($this->flashName)->get($name);
|
2012-02-11 11:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-12 00:54:16 +00:00
|
|
|
* @return array
|
|
|
|
*
|
2012-02-11 14:30:36 +00:00
|
|
|
* @deprecated since 2.1, will be removed from 2.3
|
2012-02-11 11:43:38 +00:00
|
|
|
*/
|
|
|
|
public function clearFlashes()
|
|
|
|
{
|
2012-02-12 05:04:04 +00:00
|
|
|
return $this->getBag($this->flashName)->clear();
|
2012-02-11 11:43:38 +00:00
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|