2015-08-17 17:00:26 -07:00
< ? 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\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\Storage\NativeSessionStorage ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface ;
2015-08-17 17:00:26 -07:00
/**
* @ author Fabien Potencier < fabien @ symfony . com >
* @ author Drak < drak @ zikula . org >
*/
class Session implements SessionInterface , \IteratorAggregate , \Countable
{
protected $storage ;
private $flashName ;
private $attributeName ;
2018-11-23 12:29:20 +00:00
private $data = array ();
private $usageIndex = 0 ;
2015-08-17 17:00:26 -07:00
/**
2017-02-02 16:28:38 -08:00
* @ param SessionStorageInterface $storage A SessionStorageInterface instance
2015-08-17 17:00:26 -07:00
* @ param AttributeBagInterface $attributes An AttributeBagInterface instance , ( defaults null for default AttributeBag )
* @ param FlashBagInterface $flashes A FlashBagInterface instance ( defaults null for default FlashBag )
*/
public function __construct ( SessionStorageInterface $storage = null , AttributeBagInterface $attributes = null , FlashBagInterface $flashes = 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 );
}
/**
* { @ inheritdoc }
*/
public function start ()
{
return $this -> storage -> start ();
}
/**
* { @ inheritdoc }
*/
public function has ( $name )
{
2018-11-23 12:29:20 +00:00
return $this -> getAttributeBag () -> has ( $name );
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
public function get ( $name , $default = null )
{
2018-11-23 12:29:20 +00:00
return $this -> getAttributeBag () -> get ( $name , $default );
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
public function set ( $name , $value )
{
2018-11-23 12:29:20 +00:00
$this -> getAttributeBag () -> set ( $name , $value );
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
public function all ()
{
2018-11-23 12:29:20 +00:00
return $this -> getAttributeBag () -> all ();
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
public function replace ( array $attributes )
{
2018-11-23 12:29:20 +00:00
$this -> getAttributeBag () -> replace ( $attributes );
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
public function remove ( $name )
{
2018-11-23 12:29:20 +00:00
return $this -> getAttributeBag () -> remove ( $name );
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
public function clear ()
{
2018-11-23 12:29:20 +00:00
$this -> getAttributeBag () -> clear ();
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
public function isStarted ()
{
return $this -> storage -> isStarted ();
}
/**
* Returns an iterator for attributes .
*
* @ return \ArrayIterator An \ArrayIterator instance
*/
public function getIterator ()
{
2018-11-23 12:29:20 +00:00
return new \ArrayIterator ( $this -> getAttributeBag () -> all ());
2015-08-17 17:00:26 -07:00
}
/**
* Returns the number of attributes .
*
* @ return int The number of attributes
*/
public function count ()
{
2018-11-23 12:29:20 +00:00
return \count ( $this -> getAttributeBag () -> all ());
}
/**
* @ return int
*
* @ internal
*/
public function getUsageIndex ()
{
return $this -> usageIndex ;
}
/**
* @ return bool
*
* @ internal
*/
public function isEmpty ()
{
if ( $this -> isStarted ()) {
++ $this -> usageIndex ;
}
foreach ( $this -> data as & $data ) {
if ( ! empty ( $data )) {
return false ;
}
}
return true ;
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
public function invalidate ( $lifetime = null )
{
$this -> storage -> clear ();
return $this -> migrate ( true , $lifetime );
}
/**
* { @ inheritdoc }
*/
public function migrate ( $destroy = false , $lifetime = null )
{
return $this -> storage -> regenerate ( $destroy , $lifetime );
}
/**
* { @ inheritdoc }
*/
public function save ()
{
$this -> storage -> save ();
}
/**
* { @ inheritdoc }
*/
public function getId ()
{
return $this -> storage -> getId ();
}
/**
* { @ inheritdoc }
*/
public function setId ( $id )
{
2018-11-23 12:29:20 +00:00
if ( $this -> storage -> getId () !== $id ) {
$this -> storage -> setId ( $id );
}
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
public function getName ()
{
return $this -> storage -> getName ();
}
/**
* { @ inheritdoc }
*/
public function setName ( $name )
{
$this -> storage -> setName ( $name );
}
/**
* { @ inheritdoc }
*/
public function getMetadataBag ()
{
2018-11-23 12:29:20 +00:00
++ $this -> usageIndex ;
2015-08-17 17:00:26 -07:00
return $this -> storage -> getMetadataBag ();
}
/**
* { @ inheritdoc }
*/
public function registerBag ( SessionBagInterface $bag )
{
2018-11-23 12:29:20 +00:00
$this -> storage -> registerBag ( new SessionBagProxy ( $bag , $this -> data , $this -> usageIndex ));
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
public function getBag ( $name )
{
2018-11-23 12:29:20 +00:00
return $this -> storage -> getBag ( $name ) -> getBag ();
2015-08-17 17:00:26 -07:00
}
/**
* Gets the flashbag interface .
*
* @ return FlashBagInterface
*/
public function getFlashBag ()
{
return $this -> getBag ( $this -> flashName );
}
2018-11-23 12:29:20 +00:00
/**
* Gets the attributebag interface .
*
* Note that this method was added to help with IDE autocompletion .
*
* @ return AttributeBagInterface
*/
private function getAttributeBag ()
{
return $this -> getBag ( $this -> attributeName );
}
2015-08-17 17:00:26 -07:00
}