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>
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
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;
|
2011-06-15 14:52:22 +01:00
|
|
|
protected $started;
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $attributes;
|
2011-06-15 14:52:22 +01:00
|
|
|
protected $flashes;
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $oldFlashes;
|
2011-06-15 14:52:22 +01:00
|
|
|
protected $locale;
|
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-06-15 14:52:22 +01:00
|
|
|
$this->locale = $defaultLocale;
|
|
|
|
$this->flashes = array();
|
|
|
|
$this->oldFlashes = array();
|
|
|
|
$this->attributes = array();
|
|
|
|
$this->setPhpDefaultLocale($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.
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-07-09 08:26:22 +01:00
|
|
|
*/
|
|
|
|
public function start()
|
|
|
|
{
|
|
|
|
if (true === $this->started) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->storage->start();
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
$attributes = $this->storage->read('_symfony2');
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
if (isset($attributes['attributes'])) {
|
|
|
|
$this->attributes = $attributes['attributes'];
|
|
|
|
$this->flashes = $attributes['flashes'];
|
|
|
|
$this->locale = $attributes['locale'];
|
|
|
|
$this->setPhpDefaultLocale($this->locale);
|
2010-08-22 14:31:44 +01:00
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
// flag current flash messages to be removed at shutdown
|
2011-06-20 10:06:27 +01:00
|
|
|
$this->oldFlashes = $this->flashes;
|
2010-08-22 14:31:44 +01:00
|
|
|
}
|
|
|
|
|
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
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-08-20 22:20:30 +01:00
|
|
|
*/
|
|
|
|
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
|
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
|
|
|
{
|
|
|
|
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
|
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
|
|
|
{
|
2010-08-20 22:20:30 +01:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
$this->attributes[$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
|
|
|
{
|
|
|
|
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
|
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
|
|
|
{
|
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
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-08-20 22:20:30 +01:00
|
|
|
*/
|
|
|
|
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.
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-10-19 12:09:48 +01:00
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->attributes = array();
|
2011-06-15 14:52:22 +01:00
|
|
|
$this->flashes = array();
|
|
|
|
$this->setPhpDefaultLocale($this->locale = $this->defaultLocale);
|
2010-10-19 12:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invalidates the current session.
|
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()
|
|
|
|
{
|
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.
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-23 20:43:07 +00:00
|
|
|
*/
|
|
|
|
public function migrate()
|
|
|
|
{
|
|
|
|
$this->storage->regenerate();
|
|
|
|
}
|
|
|
|
|
2011-01-03 16:00:28 +00:00
|
|
|
/**
|
|
|
|
* Returns the session ID
|
|
|
|
*
|
|
|
|
* @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()
|
|
|
|
{
|
2011-06-15 14:52:22 +01:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
2011-01-03 16:00:28 +00:00
|
|
|
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
|
|
|
{
|
2011-06-15 14:52:22 +01:00
|
|
|
return $this->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
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
$this->setPhpDefaultLocale($this->locale = $locale);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-06-11 10:44:30 +01:00
|
|
|
/**
|
|
|
|
* Gets the flash messages.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-11-26 08:47:17 +00:00
|
|
|
public function getFlashes()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-06-15 14:52:22 +01:00
|
|
|
return $this->flashes;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-06-11 10:44:30 +01:00
|
|
|
/**
|
|
|
|
* Sets the flash messages.
|
|
|
|
*
|
|
|
|
* @param array $values
|
|
|
|
*/
|
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();
|
|
|
|
}
|
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
$this->flashes = $values;
|
2011-03-11 19:23:17 +00:00
|
|
|
$this->oldFlashes = array();
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-06-11 10:44:30 +01:00
|
|
|
/**
|
|
|
|
* Gets a flash message.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string|null $default
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getFlash($name, $default = null)
|
|
|
|
{
|
2011-06-15 14:52:22 +01:00
|
|
|
return array_key_exists($name, $this->flashes) ? $this->flashes[$name] : $default;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-06-11 10:44:30 +01:00
|
|
|
/**
|
|
|
|
* Sets a flash message.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
*/
|
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();
|
|
|
|
}
|
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
$this->flashes[$name] = $value;
|
2010-05-06 12:25:53 +01:00
|
|
|
unset($this->oldFlashes[$name]);
|
|
|
|
}
|
|
|
|
|
2011-06-11 10:44:30 +01:00
|
|
|
/**
|
|
|
|
* Checks whether a flash message exists.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function hasFlash($name)
|
|
|
|
{
|
2011-03-11 19:23:17 +00:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
return array_key_exists($name, $this->flashes);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2011-06-11 10:44:30 +01:00
|
|
|
/**
|
|
|
|
* Removes a flash message.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
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();
|
|
|
|
}
|
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
unset($this->flashes[$name]);
|
2010-11-26 08:47:32 +00:00
|
|
|
}
|
|
|
|
|
2011-06-11 10:44:30 +01:00
|
|
|
/**
|
|
|
|
* Removes the flash messages.
|
|
|
|
*/
|
2010-11-26 08:47:32 +00:00
|
|
|
public function clearFlashes()
|
|
|
|
{
|
2011-03-11 19:23:17 +00:00
|
|
|
if (false === $this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
$this->flashes = 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();
|
|
|
|
}
|
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
$this->flashes = array_diff_key($this->flashes, $this->oldFlashes);
|
2011-04-27 23:03:32 +01:00
|
|
|
|
2011-06-15 14:52:22 +01:00
|
|
|
$this->storage->write('_symfony2', array(
|
|
|
|
'attributes' => $this->attributes,
|
|
|
|
'flashes' => $this->flashes,
|
|
|
|
'locale' => $this->locale,
|
|
|
|
));
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
2010-08-27 08:07:47 +01:00
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
2011-05-19 19:47:21 +01:00
|
|
|
if (true === $this->started) {
|
|
|
|
$this->save();
|
|
|
|
}
|
2010-08-27 08:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2011-06-15 14:52:22 +01:00
|
|
|
|
|
|
|
private function setPhpDefaultLocale($locale)
|
|
|
|
{
|
2011-07-10 14:15:04 +01:00
|
|
|
// if either the class Locale doesn't exist, or an exception is thrown when
|
|
|
|
// setting the default locale, the intl module is not installed, and
|
|
|
|
// the call can be ignored:
|
2011-06-15 14:52:22 +01:00
|
|
|
try {
|
2011-07-10 14:15:04 +01:00
|
|
|
if (class_exists('Locale', false)) {
|
|
|
|
\Locale::setDefault($locale);
|
|
|
|
}
|
2011-06-15 14:52:22 +01:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
}
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|