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;
|
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>
|
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;
|
|
|
|
|
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
|
|
|
*
|
2016-06-28 06:50:50 +01: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)
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2012-03-29 12:30:37 +01:00
|
|
|
public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = 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);
|
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-06-09 20:45:38 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function isStarted()
|
|
|
|
{
|
|
|
|
return $this->storage->isStarted();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2012-03-30 18:02:35 +01:00
|
|
|
public function invalidate($lifetime = null)
|
2010-10-12 07:58:12 +01:00
|
|
|
{
|
2011-11-29 04:35:09 +00:00
|
|
|
$this->storage->clear();
|
|
|
|
|
2012-03-30 18:02:35 +01:00
|
|
|
return $this->migrate(true, $lifetime);
|
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
|
|
|
*/
|
2012-03-30 18:02:35 +01:00
|
|
|
public function migrate($destroy = false, $lifetime = null)
|
2011-11-29 04:35:09 +00:00
|
|
|
{
|
2012-03-30 18:02:35 +01:00
|
|
|
return $this->storage->regenerate($destroy, $lifetime);
|
2011-11-29 04:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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
|
|
|
/**
|
2012-05-07 15:20:20 +01:00
|
|
|
* {@inheritdoc}
|
2012-03-28 16:22:12 +01:00
|
|
|
*/
|
2012-03-31 04:15:05 +01:00
|
|
|
public function getMetadataBag()
|
2012-03-28 16:22:12 +01:00
|
|
|
{
|
2012-03-31 04:15:05 +01:00
|
|
|
return $this->storage->getMetadataBag();
|
2012-03-28 16:22:12 +01:00
|
|
|
}
|
|
|
|
|
2012-03-14 11:53:34 +00:00
|
|
|
/**
|
2012-05-07 15:20:20 +01:00
|
|
|
* {@inheritdoc}
|
2012-02-11 14:30:36 +00:00
|
|
|
*/
|
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
|
|
|
/**
|
2012-05-07 15:20:20 +01:00
|
|
|
* {@inheritdoc}
|
2012-02-11 14:30:36 +00:00
|
|
|
*/
|
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
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|