[DoctribeBridge][SecurityBundle][WebProfiler] Refactor code for HttpFoundation changes.

This commit is contained in:
Drak 2011-12-24 16:20:59 +05:45
parent 7aaf024b2a
commit 1ed6ee325c
3 changed files with 35 additions and 35 deletions

View File

@ -3,7 +3,10 @@
namespace Symfony\Bridge\Doctrine\HttpFoundation;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\AttributeBagInterface;
use Symfony\Component\HttpFoundation\FlashBagInterface;
use Symfony\Component\HttpFoundation\SessionStorage\AbstractSessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\SessionSaveHandlerInterface;
use Doctrine\DBAL\Driver\Connection;
/**
@ -12,39 +15,32 @@ use Doctrine\DBAL\Driver\Connection;
* @author Fabien Potencier <fabien@symfony.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class DbalSessionStorage extends NativeSessionStorage
class DbalSessionStorage extends AbstractSessionStorage implements SessionSaveHandlerInterface
{
/**
* @var Connection
*/
private $con;
private $tableName;
public function __construct(Connection $con, $tableName = 'sessions', array $options = array())
{
parent::__construct($options);
$this->con = $con;
$this->tableName = $tableName;
}
/**
* Starts the session.
*/
public function start()
* @var string
*/
private $tableName;
/**
*
* @param Connection $con An instance of Connection.
* @param string $tableName Table name.
* @param array $options Session configuration options
* @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(Connection $con, $tableName = 'sessions', array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
{
if (self::$sessionStarted) {
return;
}
$this->con = $con;
$this->tableName = $tableName;
// use this object as the session handler
session_set_save_handler(
array($this, 'sessionOpen'),
array($this, 'sessionClose'),
array($this, 'sessionRead'),
array($this, 'sessionWrite'),
array($this, 'sessionDestroy'),
array($this, 'sessionGC')
);
parent::start();
parent::__construct($attributes, $flashes, $options);
}
/**
@ -102,7 +98,7 @@ class DbalSessionStorage extends NativeSessionStorage
*
* @throws \RuntimeException If any old sessions cannot be cleaned
*/
public function sessionGC($lifetime)
public function sessionGc($lifetime)
{
try {
$this->con->executeQuery("DELETE FROM {$this->tableName} WHERE sess_time < :time", array(
@ -140,7 +136,7 @@ class DbalSessionStorage extends NativeSessionStorage
return '';
} catch (\PDOException $e) {
throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
throw new \RuntimeException(sprintf('PDOException was thrown when trying to read the session data: %s', $e->getMessage()), 0, $e);
}
}
@ -181,7 +177,7 @@ class DbalSessionStorage extends NativeSessionStorage
$this->createNewSession($id, $data);
}
} catch (\PDOException $e) {
throw new \RuntimeException(sprintf('PDOException was thrown when trying to manipulate session data: %s', $e->getMessage()), 0, $e);
throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e);
}
return true;

View File

@ -10,7 +10,7 @@ framework:
default_locale: en
session:
auto_start: true
storage_id: session.storage.filesystem
storage_id: session.storage.mock_file
services:
logger: { class: Symfony\Component\HttpKernel\Log\NullLogger }

View File

@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Bundle\TwigBundle\TwigEngine;
use Symfony\Component\HttpFoundation\AutoExpireFlashBag;
/**
* WebDebugToolbarListener injects the Web Debug Toolbar.
@ -70,9 +71,12 @@ class WebDebugToolbarListener
}
if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects) {
if (null !== $session = $request->getSession()) {
// keep current flashes for one more request
$session->setFlashes($session->getFlashes());
$session = $request->getSession();
if ($session instanceof AutoExpireFlashBag) {
// keep current flashes for one more request if using AutoExpireFlashBag
foreach ($session->getFlashKeys() as $type) {
$session->setFlashes($session->getFlashes($type));
}
}
$response->setContent($this->templating->render('WebProfilerBundle:Profiler:toolbar_redirect.html.twig', array('location' => $response->headers->get('Location'))));