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
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\HttpFoundation;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface;
|
2012-02-08 09:46:38 +00:00
|
|
|
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBag;
|
|
|
|
use Symfony\Component\HttpFoundation\SessionAttribute\AttributeBagInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\SessionFlash\FlashBag;
|
|
|
|
use Symfony\Component\HttpFoundation\SessionFlash\FlashBagInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\SessionBagInterface;
|
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
|
|
|
*/
|
2011-11-29 04:35:09 +00:00
|
|
|
class Session implements SessionInterface
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2011-11-29 04:35:09 +00:00
|
|
|
/**
|
|
|
|
* Storage driver.
|
|
|
|
*
|
|
|
|
* @var SessionStorageInterface
|
|
|
|
*/
|
2010-07-09 08:26:22 +01:00
|
|
|
protected $storage;
|
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
|
|
|
*
|
2011-11-29 04:35:09 +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)
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2012-02-08 09:46:38 +00:00
|
|
|
public function __construct(SessionStorageInterface $storage, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-07-09 08:26:22 +01:00
|
|
|
$this->storage = $storage;
|
2012-02-08 09:46:38 +00:00
|
|
|
$this->registerBag($attributes ?: new AttributeBag());
|
|
|
|
$this->registerBag($flashes ?: new FlashBag());
|
2010-07-09 08:26:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the session storage.
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
2011-11-29 04:35:09 +00:00
|
|
|
* @return boolean True if session started.
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-20 22:20:30 +01:00
|
|
|
* Checks if an attribute is defined.
|
|
|
|
*
|
|
|
|
* @param string $name The attribute name
|
|
|
|
*
|
|
|
|
* @return Boolean true if the attribute is defined, false otherwise
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-08-20 22:20:30 +01:00
|
|
|
*/
|
|
|
|
public function has($name)
|
|
|
|
{
|
2012-02-08 09:46:38 +00:00
|
|
|
return $this->storage->getBag('attributes')->has($name);
|
2010-08-20 22:20:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an attribute.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @param string $name The attribute name
|
|
|
|
* @param mixed $default The default value
|
|
|
|
*
|
|
|
|
* @return mixed
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
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-02-08 09:46:38 +00:00
|
|
|
return $this->storage->getBag('attributes')->get($name, $default);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-09 08:26:22 +01:00
|
|
|
* Sets an attribute.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
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-02-08 09:46:38 +00:00
|
|
|
$this->storage->getBag('attributes')->set($name, $value);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-09 08:26:22 +01:00
|
|
|
* Returns attributes.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-07-09 08:26:22 +01:00
|
|
|
* @return array Attributes
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
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-02-08 09:46:38 +00:00
|
|
|
return $this->storage->getBag('attributes')->all();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-20 22:20:30 +01:00
|
|
|
* Sets attributes.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-07-01 19:22:40 +01:00
|
|
|
* @param array $attributes Attributes
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
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-02-08 09:46:38 +00:00
|
|
|
$this->storage->getBag('attributes')->replace($attributes);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-08-20 22:20:30 +01:00
|
|
|
/**
|
|
|
|
* Removes an attribute.
|
|
|
|
*
|
|
|
|
* @param string $name
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-08-20 22:20:30 +01:00
|
|
|
*/
|
|
|
|
public function remove($name)
|
|
|
|
{
|
2012-02-08 09:46:38 +00:00
|
|
|
return $this->storage->getBag('attributes')->remove($name);
|
2010-08-20 22:20:30 +01:00
|
|
|
}
|
|
|
|
|
2010-10-19 12:09:48 +01:00
|
|
|
/**
|
|
|
|
* Clears all attributes.
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-10-19 12:09:48 +01:00
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
2012-02-08 09:46:38 +00:00
|
|
|
$this->storage->getBag('attributes')->clear();
|
2010-10-19 12:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidates the current session.
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
2011-11-29 04:35:09 +00:00
|
|
|
* Clears all session attributes and flashes and regenerates the
|
|
|
|
* session and deletes the old session from persistence.
|
|
|
|
*
|
|
|
|
* @return boolean True if session invalidated, false if error.
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
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
|
|
|
/**
|
|
|
|
* Migrates the current session to a new session id while maintaining all
|
|
|
|
* session attributes.
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
2011-11-29 04:35:09 +00:00
|
|
|
* @param boolean $destroy Whether to delete the old session or leave it to garbage collection.
|
|
|
|
*
|
|
|
|
* @return boolean True if session migrated, false if error
|
|
|
|
*
|
2011-07-20 09:06:02 +01:00
|
|
|
* @api
|
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
|
|
|
/**
|
|
|
|
* Returns the session ID
|
|
|
|
*
|
2011-11-29 04:35:09 +00:00
|
|
|
* @return mixed The session ID
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-03 16:00:28 +00:00
|
|
|
*/
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->storage->getId();
|
|
|
|
}
|
|
|
|
|
2011-06-11 10:44:30 +01:00
|
|
|
/**
|
2011-11-29 04:35:09 +00:00
|
|
|
* Implements the \Serialize interface.
|
2011-06-11 10:44:30 +01:00
|
|
|
*
|
2011-11-29 04:35:09 +00:00
|
|
|
* @return SessionStorageInterface
|
2011-06-11 10:44:30 +01:00
|
|
|
*/
|
2011-11-29 04:35:09 +00:00
|
|
|
public function serialize()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-11-29 04:35:09 +00:00
|
|
|
return serialize($this->storage);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-06-11 10:44:30 +01:00
|
|
|
/**
|
2011-11-29 04:35:09 +00:00
|
|
|
* Implements the \Serialize interface.
|
2011-06-11 10:44:30 +01:00
|
|
|
*
|
2011-11-29 04:35:09 +00:00
|
|
|
* @throws \InvalidArgumentException If the passed string does not unserialize to an instance of SessionStorageInterface
|
2011-06-11 10:44:30 +01:00
|
|
|
*/
|
2011-11-29 04:35:09 +00:00
|
|
|
public function unserialize($serialized)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-11-29 04:35:09 +00:00
|
|
|
$storage = unserialize($serialized);
|
|
|
|
if (!$storage instanceof SessionStorageInterface) {
|
|
|
|
throw new \InvalidArgumentException('Serialized data did not return a valid instance of SessionStorageInterface');
|
2010-08-20 22:20:30 +01:00
|
|
|
}
|
|
|
|
|
2011-11-29 04:35:09 +00:00
|
|
|
$this->storage = $storage;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2012-02-08 09:46:38 +00:00
|
|
|
public function registerBag(SessionBagInterface $bag)
|
|
|
|
{
|
|
|
|
$this->storage->registerBag($bag);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02 11:07:02 +00:00
|
|
|
public function getFlashes()
|
2010-08-27 08:07:47 +01:00
|
|
|
{
|
2012-02-08 09:46:38 +00:00
|
|
|
return $this->getBag('flashes');
|
2010-08-27 08:07:47 +01:00
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|