2012-01-02 15:32:41 +00: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.
|
|
|
|
*/
|
|
|
|
|
2012-02-08 23:15:47 +00:00
|
|
|
namespace Symfony\Component\HttpFoundation\Session\Storage;
|
2012-01-02 15:32:41 +00:00
|
|
|
|
2012-03-03 02:54:20 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
|
|
|
|
|
2012-01-02 15:32:41 +00:00
|
|
|
/**
|
2012-02-11 11:04:05 +00:00
|
|
|
* MockArraySessionStorage mocks the session for unit tests.
|
2012-01-02 15:32:41 +00:00
|
|
|
*
|
|
|
|
* No PHP session is actually started since a session can be initialized
|
|
|
|
* and shutdown only once per PHP execution cycle.
|
|
|
|
*
|
2012-02-11 11:04:05 +00:00
|
|
|
* When doing functional testing, you should use MockFileSessionStorage instead.
|
2012-01-02 15:32:41 +00:00
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
|
|
|
|
* @author Drak <drak@zikula.org>
|
|
|
|
*/
|
2012-03-03 02:54:20 +00:00
|
|
|
class MockArraySessionStorage extends SessionStorage
|
2012-01-02 15:32:41 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $sessionId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2012-02-12 00:54:16 +00:00
|
|
|
protected $sessionData = array();
|
2012-01-02 15:32:41 +00:00
|
|
|
|
2012-03-03 02:54:20 +00:00
|
|
|
public function __construct(array $options = array())
|
|
|
|
{
|
|
|
|
parent::__construct($options, new NullSessionHandler());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the session data.
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
*/
|
2012-02-08 09:46:38 +00:00
|
|
|
public function setSessionData(array $array)
|
2012-01-02 15:32:41 +00:00
|
|
|
{
|
2012-02-08 09:46:38 +00:00
|
|
|
$this->sessionData = $array;
|
2012-01-02 15:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function start()
|
|
|
|
{
|
|
|
|
if ($this->started && !$this->closed) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->started = true;
|
2012-02-08 09:46:38 +00:00
|
|
|
$this->loadSession($this->sessionData);
|
|
|
|
|
2012-01-02 15:32:41 +00:00
|
|
|
$this->sessionId = $this->generateSessionId();
|
|
|
|
session_id($this->sessionId);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-08 09:46:38 +00:00
|
|
|
|
2012-01-02 15:32:41 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function regenerate($destroy = false)
|
|
|
|
{
|
2012-03-08 09:26:10 +00:00
|
|
|
if (!$this->started) {
|
2012-01-02 15:32:41 +00:00
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->sessionId = $this->generateSessionId();
|
|
|
|
session_id($this->sessionId);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
if (!$this->started) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function save()
|
|
|
|
{
|
|
|
|
// nothing to do since we don't persist the session data
|
|
|
|
$this->closed = false;
|
|
|
|
}
|
|
|
|
|
2012-02-08 09:46:38 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
// clear out the bags
|
|
|
|
foreach ($this->bags as $bag) {
|
|
|
|
$bag->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear out the session
|
|
|
|
$this->sessionData = array();
|
|
|
|
|
|
|
|
// reconnect the bags to the session
|
|
|
|
$this->loadSession($this->sessionData);
|
|
|
|
}
|
|
|
|
|
2012-03-08 09:26:10 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getBag($name)
|
|
|
|
{
|
|
|
|
if (!isset($this->bags[$name])) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->started) {
|
|
|
|
$this->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->bags[$name];
|
|
|
|
}
|
|
|
|
|
2012-01-02 15:32:41 +00:00
|
|
|
/**
|
|
|
|
* Generates a session ID.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function generateSessionId()
|
|
|
|
{
|
|
|
|
return sha1(uniqid(mt_rand(), true));
|
|
|
|
}
|
|
|
|
}
|