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;
|
|
|
|
|
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>
|
2010-02-17 13:55:05 +00:00
|
|
|
*/
|
2010-08-27 08:07:47 +01:00
|
|
|
class Session implements \Serializable
|
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
|
|
|
protected $attributes;
|
|
|
|
protected $oldFlashes;
|
2010-07-09 08:26:22 +01:00
|
|
|
protected $started;
|
2011-03-30 16:26:13 +01:00
|
|
|
protected $defaultLocale;
|
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-04-23 16:05:44 +01:00
|
|
|
* @param SessionStorageInterface $storage A SessionStorageInterface instance
|
2011-03-30 16:26:13 +01:00
|
|
|
* @param string $defaultLocale The default locale
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-03-30 16:26:13 +01:00
|
|
|
public function __construct(SessionStorageInterface $storage, $defaultLocale = 'en')
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-07-09 08:26:22 +01:00
|
|
|
$this->storage = $storage;
|
2011-03-30 16:26:13 +01:00
|
|
|
$this->defaultLocale = $defaultLocale;
|
2011-05-19 19:44:15 +01:00
|
|
|
$this->attributes = array('_flash' => array(), '_locale' => $this->defaultLocale);
|
2010-08-22 14:31:44 +01:00
|
|
|
$this->started = false;
|
2010-07-09 08:26:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the session storage.
|
|
|
|
*/
|
|
|
|
public function start()
|
|
|
|
{
|
|
|
|
if (true === $this->started) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->storage->start();
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-08-22 14:31:44 +01:00
|
|
|
$this->attributes = $this->storage->read('_symfony2');
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-08-22 14:31:44 +01:00
|
|
|
if (!isset($this->attributes['_flash'])) {
|
|
|
|
$this->attributes['_flash'] = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($this->attributes['_locale'])) {
|
2011-05-19 19:44:15 +01:00
|
|
|
$this->attributes['_locale'] = $this->defaultLocale;
|
2010-08-22 14:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// flag current flash messages to be removed at shutdown
|
|
|
|
$this->oldFlashes = array_flip(array_keys($this->attributes['_flash']));
|
2010-07-09 08:26:22 +01:00
|
|
|
|
|
|
|
$this->started = true;
|
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
|
|
|
|
*/
|
|
|
|
public function has($name)
|
|
|
|
{
|
|
|
|
return array_key_exists($name, $this->attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2010-08-20 22:20:30 +01:00
|
|
|
public function get($name, $default = null)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
2010-08-20 22:20:30 +01:00
|
|
|
public function set($name, $value)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-08-20 22:20:30 +01:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->attributes[$name] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getAttributes()
|
|
|
|
{
|
|
|
|
return $this->attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-02-09 22:14:36 +00:00
|
|
|
public function setAttributes(array $attributes)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-08-20 22:20:30 +01:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->attributes = $attributes;
|
|
|
|
}
|
|
|
|
|
2010-08-20 22:20:30 +01:00
|
|
|
/**
|
|
|
|
* Removes an attribute.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public function remove($name)
|
|
|
|
{
|
2010-09-29 21:20:52 +01:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
2010-10-19 12:09:48 +01:00
|
|
|
|
2010-08-29 19:28:33 +01:00
|
|
|
if (array_key_exists($name, $this->attributes)) {
|
2010-08-20 22:20:30 +01:00
|
|
|
unset($this->attributes[$name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-19 12:09:48 +01:00
|
|
|
/**
|
|
|
|
* Clears all attributes.
|
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->attributes = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidates the current session.
|
|
|
|
*/
|
2010-10-12 07:58:12 +01:00
|
|
|
public function invalidate()
|
|
|
|
{
|
2010-10-19 12:09:48 +01:00
|
|
|
$this->clear();
|
2010-10-12 07:58:12 +01:00
|
|
|
$this->storage->regenerate();
|
|
|
|
}
|
|
|
|
|
2011-01-23 20:43:07 +00:00
|
|
|
/**
|
|
|
|
* Migrates the current session to a new session id while maintaining all
|
|
|
|
* session attributes.
|
|
|
|
*/
|
|
|
|
public function migrate()
|
|
|
|
{
|
|
|
|
$this->storage->regenerate();
|
|
|
|
}
|
|
|
|
|
2011-01-03 16:00:28 +00:00
|
|
|
/**
|
|
|
|
* Returns the session ID
|
|
|
|
*
|
|
|
|
* @return mixed The session ID
|
|
|
|
*/
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->storage->getId();
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-07-09 08:26:22 +01:00
|
|
|
* Returns the locale
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-06-07 09:10:00 +01:00
|
|
|
public function getLocale()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-08-29 19:53:54 +01:00
|
|
|
return $this->attributes['_locale'];
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-07-09 08:26:22 +01:00
|
|
|
* Sets the locale.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-06-07 09:10:00 +01:00
|
|
|
* @param string $locale
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-06-07 09:10:00 +01:00
|
|
|
public function setLocale($locale)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-08-29 19:53:54 +01:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-08-29 19:53:54 +01:00
|
|
|
|
|
|
|
$this->attributes['_locale'] = $locale;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-11-26 08:47:17 +00:00
|
|
|
public function getFlashes()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-03-05 12:30:54 +00:00
|
|
|
return isset($this->attributes['_flash']) ? $this->attributes['_flash'] : array();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-11-26 08:47:17 +00:00
|
|
|
public function setFlashes($values)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2010-08-20 22:20:30 +01:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->attributes['_flash'] = $values;
|
2011-03-11 19:23:17 +00:00
|
|
|
$this->oldFlashes = array();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFlash($name, $default = null)
|
|
|
|
{
|
2010-08-22 14:31:44 +01:00
|
|
|
return array_key_exists($name, $this->attributes['_flash']) ? $this->attributes['_flash'][$name] : $default;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setFlash($name, $value)
|
|
|
|
{
|
2010-08-20 22:20:30 +01:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->attributes['_flash'][$name] = $value;
|
|
|
|
unset($this->oldFlashes[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasFlash($name)
|
|
|
|
{
|
2011-03-11 19:23:17 +00:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return array_key_exists($name, $this->attributes['_flash']);
|
|
|
|
}
|
|
|
|
|
2010-11-26 08:47:32 +00:00
|
|
|
public function removeFlash($name)
|
|
|
|
{
|
2011-03-11 19:23:17 +00:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
2010-11-26 08:47:32 +00:00
|
|
|
unset($this->attributes['_flash'][$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clearFlashes()
|
|
|
|
{
|
2011-03-11 19:23:17 +00:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
2010-11-26 08:47:32 +00:00
|
|
|
$this->attributes['_flash'] = array();
|
2011-03-11 19:23:17 +00:00
|
|
|
$this->oldFlashes = array();
|
2010-11-26 08:47:32 +00:00
|
|
|
}
|
|
|
|
|
2010-08-27 08:07:47 +01:00
|
|
|
public function save()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-03-11 19:23:17 +00:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->attributes['_flash'])) {
|
|
|
|
$this->attributes['_flash'] = array_diff_key($this->attributes['_flash'], $this->oldFlashes);
|
2010-07-09 08:26:22 +01:00
|
|
|
}
|
2011-03-11 19:23:17 +00:00
|
|
|
$this->storage->write('_symfony2', $this->attributes);
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
2010-08-27 08:07:47 +01:00
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
$this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function serialize()
|
|
|
|
{
|
2011-03-30 16:26:13 +01:00
|
|
|
return serialize(array($this->storage, $this->defaultLocale));
|
2010-08-27 08:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function unserialize($serialized)
|
|
|
|
{
|
2011-03-30 16:26:13 +01:00
|
|
|
list($this->storage, $this->defaultLocale) = unserialize($serialized);
|
2010-08-27 08:07:47 +01:00
|
|
|
$this->attributes = array();
|
|
|
|
$this->started = false;
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|