This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpFoundation/Session.php

290 lines
6.3 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\SessionStorage\SessionStorageInterface;
/**
* Session.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Session implements \Serializable
{
protected $storage;
protected $attributes;
protected $oldFlashes;
protected $started;
protected $options;
/**
* Constructor.
*
* @param SessionStorageInterface $session A SessionStorageInterface instance
* @param array $options An array of options
*/
public function __construct(SessionStorageInterface $storage, array $options = array())
{
$this->storage = $storage;
$this->options = $options;
$this->attributes = array('_flash' => array(), '_locale' => $this->getDefaultLocale());
2010-08-22 14:31:44 +01:00
$this->started = false;
}
/**
* Starts the session storage.
*/
public function start()
{
if (true === $this->started) {
return;
}
$this->storage->start();
2010-08-22 14:31:44 +01:00
$this->attributes = $this->storage->read('_symfony2');
2010-08-22 14:31:44 +01:00
if (!isset($this->attributes['_flash'])) {
$this->attributes['_flash'] = array();
}
if (!isset($this->attributes['_locale'])) {
$this->attributes['_locale'] = $this->getDefaultLocale();
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']));
$this->started = true;
}
/**
* 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.
*
* @param string $name The attribute name
* @param mixed $default The default value
*
* @return mixed
*/
public function get($name, $default = null)
{
return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
}
/**
* Sets an attribute.
*
* @param string $name
* @param mixed $value
*/
public function set($name, $value)
{
if (false === $this->started) {
$this->start();
}
$this->attributes[$name] = $value;
}
/**
* Returns attributes.
*
* @return array Attributes
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* Sets attributes.
*
2010-07-01 19:22:40 +01:00
* @param array $attributes Attributes
*/
public function setAttributes($attributes)
{
if (false === $this->started) {
$this->start();
}
$this->attributes = $attributes;
}
/**
* Removes an attribute.
*
* @param string $name
*/
public function remove($name)
{
if (false === $this->started) {
$this->start();
}
2010-08-29 19:28:33 +01:00
if (array_key_exists($name, $this->attributes)) {
unset($this->attributes[$name]);
}
}
/**
* Clears all attributes.
*/
public function clear()
{
if (false === $this->started) {
$this->start();
}
$this->attributes = array();
}
/**
* Invalidates the current session.
*/
public function invalidate()
{
$this->clear();
$this->storage->regenerate();
}
/**
* Migrates the current session to a new session id while maintaining all
* session attributes.
*/
public function migrate()
{
$this->storage->regenerate();
}
/**
* Returns the session ID
*
* @return mixed The session ID
*/
public function getId()
{
return $this->storage->getId();
}
/**
* Returns the locale
*
* @return string
*/
public function getLocale()
{
return $this->attributes['_locale'];
}
/**
* Sets the locale.
*
* @param string $locale
*/
public function setLocale($locale)
{
if (false === $this->started) {
$this->start();
}
$this->attributes['_locale'] = $locale;
}
public function getFlashes()
{
return $this->attributes['_flash'];
}
public function setFlashes($values)
{
if (false === $this->started) {
$this->start();
}
$this->attributes['_flash'] = $values;
}
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;
}
public function setFlash($name, $value)
{
if (false === $this->started) {
$this->start();
}
$this->attributes['_flash'][$name] = $value;
unset($this->oldFlashes[$name]);
}
public function hasFlash($name)
{
return array_key_exists($name, $this->attributes['_flash']);
}
public function removeFlash($name)
{
unset($this->attributes['_flash'][$name]);
}
public function clearFlashes()
{
$this->attributes['_flash'] = array();
}
public function save()
{
if (true === $this->started) {
if (isset($this->attributes['_flash'])) {
$this->attributes['_flash'] = array_diff_key($this->attributes['_flash'], $this->oldFlashes);
}
$this->storage->write('_symfony2', $this->attributes);
}
}
public function __destruct()
{
$this->save();
}
public function serialize()
{
return serialize(array($this->storage, $this->options));
}
public function unserialize($serialized)
{
list($this->storage, $this->options) = unserialize($serialized);
$this->attributes = array();
$this->started = false;
}
protected function getDefaultLocale()
{
return isset($this->options['default_locale']) ? $this->options['default_locale'] : 'en';
}
}