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/Session.php

354 lines
7.7 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation\Session;
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
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;
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
/**
* Session.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Drak <drak@zikula.org>
2011-07-20 09:06:02 +01:00
*
* @api
*/
class Session implements SessionInterface, \IteratorAggregate, \Countable
{
/**
* Storage driver.
*
* @var SessionStorageInterface
*/
protected $storage;
/**
* @var string
*/
private $flashName;
/**
* @var string
*/
private $attributeName;
/**
* @var string
*/
private $metaName;
/**
* Constructor.
*
* @param SessionStorageInterface $storage A SessionStorageInterface instance.
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
* @param MetaBag $meta A MetaBag instance (defaults null for default MetaBag)
*/
public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null, MetaBag $meta = null)
{
$this->storage = $storage ?: new NativeSessionStorage();
$attributes = $attributes ?: new AttributeBag();
$this->attributeName = $attributes->getName();
$this->registerBag($attributes);
$flashes = $flashes ?: new FlashBag();
$this->flashName = $flashes->getName();
$this->registerBag($flashes);
$metaBag = $meta ?: new MetaBag();
$this->metaName = $metaBag->getName();
$this->registerBag($metaBag);
}
/**
* {@inheritdoc}
*/
public function start()
{
return $this->storage->start();
}
/**
* {@inheritdoc}
*/
public function has($name)
{
return $this->storage->getBag($this->attributeName)->has($name);
}
/**
* {@inheritdoc}
*/
public function get($name, $default = null)
{
return $this->storage->getBag($this->attributeName)->get($name, $default);
}
/**
* {@inheritdoc}
*/
public function set($name, $value)
{
$this->storage->getBag($this->attributeName)->set($name, $value);
}
/**
* {@inheritdoc}
*/
2011-07-15 17:06:18 +01:00
public function all()
{
return $this->storage->getBag($this->attributeName)->all();
}
/**
* {@inheritdoc}
*/
2011-07-15 17:06:18 +01:00
public function replace(array $attributes)
{
$this->storage->getBag($this->attributeName)->replace($attributes);
}
/**
* {@inheritdoc}
*/
public function remove($name)
{
return $this->storage->getBag($this->attributeName)->remove($name);
}
/**
* {@inheritdoc}
*/
public function clear()
{
$this->storage->getBag($this->attributeName)->clear();
}
/**
* Returns an iterator for attributes.
*
* @return \ArrayIterator An \ArrayIterator instance
*/
public function getIterator()
{
return new \ArrayIterator($this->storage->getBag($this->attributeName)->all());
}
/**
* Returns the number of attributes.
*
* @return int The number of attributes
*/
public function count()
{
return count($this->storage->getBag($this->attributeName)->all());
}
/**
* {@inheritdoc}
*/
public function invalidate()
{
$this->storage->clear();
return $this->storage->regenerate(true);
}
/**
* {@inheritdoc}
*/
public function migrate($destroy = false)
{
return $this->storage->regenerate($destroy);
}
/**
* {@inheritdoc}
*/
public function save()
{
$this->storage->save();
}
/**
* {@inheritdoc}
*/
public function getId()
{
return $this->storage->getId();
}
2012-02-11 14:30:36 +00:00
/**
* {@inheritdoc}
*/
public function setId($id)
{
$this->storage->setId($id);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->storage->getName();
}
/**
* {@inheritdoc}
*/
public function setName($name)
{
$this->storage->setName($name);
}
/**
* Gets session meta.
*
* @return MetaBag
*/
public function getMeta()
{
return $this->getBag($this->metaName);
}
/**
* Registers a SessionBagInterface with the session.
2012-02-11 14:30:36 +00:00
*
* @param SessionBagInterface $bag
*/
public function registerBag(SessionBagInterface $bag)
{
$this->storage->registerBag($bag);
}
2012-02-11 14:30:36 +00:00
/**
* Get's a bag instance.
*
* @param string $name
*
* @return SessionBagInterface
*/
public function getBag($name)
{
return $this->storage->getBag($name);
}
/**
* Gets the flashbag interface.
*
* @return FlashBagInterface
*/
public function getFlashBag()
{
return $this->getBag($this->flashName);
}
// the following methods are kept for compatibility with Symfony 2.0 (they will be removed for Symfony 2.3)
/**
* @return array
*
2012-02-11 14:30:36 +00:00
* @deprecated since 2.1, will be removed from 2.3
*/
public function getFlashes()
{
$all = $this->getBag($this->flashName)->all();
$return = array();
if ($all) {
foreach ($all as $name => $array) {
$return[$name] = reset($array);
}
}
return $return;
}
/**
* @param array $values
*
2012-02-11 14:30:36 +00:00
* @deprecated since 2.1, will be removed from 2.3
*/
public function setFlashes($values)
{
foreach ($values as $name => $value) {
$this->getBag($this->flashName)->set($name, $value);
}
}
/**
* @param string $name
* @param string $default
*
* @return string
*
2012-02-11 14:30:36 +00:00
* @deprecated since 2.1, will be removed from 2.3
*/
public function getFlash($name, $default = null)
{
$return = $this->getBag($this->flashName)->get($name);
return empty($return) ? $default : reset($return);
}
/**
* @param string $name
* @param string $value
*
2012-02-11 14:30:36 +00:00
* @deprecated since 2.1, will be removed from 2.3
*/
public function setFlash($name, $value)
{
$this->getBag($this->flashName)->set($name, $value);
}
/**
* @param string $name
*
* @return Boolean
*
2012-02-11 14:30:36 +00:00
* @deprecated since 2.1, will be removed from 2.3
*/
public function hasFlash($name)
{
return $this->getBag($this->flashName)->has($name);
}
/**
* @param string $name
*
2012-02-11 14:30:36 +00:00
* @deprecated since 2.1, will be removed from 2.3
*/
public function removeFlash($name)
{
$this->getBag($this->flashName)->get($name);
}
/**
* @return array
*
2012-02-11 14:30:36 +00:00
* @deprecated since 2.1, will be removed from 2.3
*/
public function clearFlashes()
{
return $this->getBag($this->flashName)->clear();
}
}