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-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;
|
2018-07-26 10:03:18 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
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
|
|
|
{
|
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
|
|
|
private $flashName;
|
|
|
|
private $attributeName;
|
2019-01-16 09:39:14 +00:00
|
|
|
private $data = [];
|
2018-06-01 13:37:16 +01:00
|
|
|
private $usageIndex = 0;
|
2012-03-15 04:06:39 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2017-01-12 20:02:22 +00:00
|
|
|
return $this->getAttributeBag()->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
|
|
|
{
|
2017-01-12 20:02:22 +00:00
|
|
|
return $this->getAttributeBag()->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
|
|
|
{
|
2017-01-12 20:02:22 +00:00
|
|
|
$this->getAttributeBag()->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
|
|
|
{
|
2017-01-12 20:02:22 +00:00
|
|
|
return $this->getAttributeBag()->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
|
|
|
{
|
2017-01-12 20:02:22 +00:00
|
|
|
$this->getAttributeBag()->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)
|
|
|
|
{
|
2017-01-12 20:02:22 +00:00
|
|
|
return $this->getAttributeBag()->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()
|
|
|
|
{
|
2017-11-30 10:09:16 +00:00
|
|
|
$this->getAttributeBag()->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()
|
|
|
|
{
|
2017-01-12 20:02:22 +00:00
|
|
|
return new \ArrayIterator($this->getAttributeBag()->all());
|
2012-03-28 16:22:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of attributes.
|
|
|
|
*
|
|
|
|
* @return int The number of attributes
|
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
2018-07-26 09:45:46 +01:00
|
|
|
return \count($this->getAttributeBag()->all());
|
2012-03-28 16:22:12 +01:00
|
|
|
}
|
|
|
|
|
2018-01-06 07:55:13 +00:00
|
|
|
/**
|
2018-06-01 13:37:16 +01:00
|
|
|
* @return int
|
2018-01-06 07:55:13 +00:00
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2018-06-01 13:37:16 +01:00
|
|
|
public function getUsageIndex()
|
2018-01-06 07:55:13 +00:00
|
|
|
{
|
2018-06-01 13:37:16 +01:00
|
|
|
return $this->usageIndex;
|
2018-01-06 07:55:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 10:09:16 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
public function isEmpty()
|
|
|
|
{
|
2018-06-27 12:18:14 +01:00
|
|
|
if ($this->isStarted()) {
|
|
|
|
++$this->usageIndex;
|
|
|
|
}
|
2017-11-30 10:09:16 +00:00
|
|
|
foreach ($this->data as &$data) {
|
|
|
|
if (!empty($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2019-03-21 08:24:23 +00:00
|
|
|
if ($this->isStarted()) {
|
|
|
|
$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)
|
|
|
|
{
|
2018-09-10 20:53:03 +01:00
|
|
|
if ($this->storage->getId() !== $id) {
|
|
|
|
$this->storage->setId($id);
|
|
|
|
}
|
2012-03-14 11:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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
|
|
|
{
|
2018-06-01 13:37:16 +01:00
|
|
|
++$this->usageIndex;
|
|
|
|
|
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)
|
|
|
|
{
|
2018-06-01 13:37:16 +01:00
|
|
|
$this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex));
|
2012-02-08 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2017-11-30 10:09:16 +00:00
|
|
|
return $this->storage->getBag($name)->getBag();
|
2012-02-08 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-01-12 20:02:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the attributebag interface.
|
|
|
|
*
|
2017-03-01 16:03:54 +00:00
|
|
|
* Note that this method was added to help with IDE autocompletion.
|
2017-03-01 15:10:23 +00:00
|
|
|
*
|
2017-01-12 20:02:22 +00:00
|
|
|
* @return AttributeBagInterface
|
|
|
|
*/
|
|
|
|
private function getAttributeBag()
|
|
|
|
{
|
2018-01-06 07:55:13 +00:00
|
|
|
return $this->getBag($this->attributeName);
|
2017-01-12 20:02:22 +00:00
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|