From 1ed6ee325c8dcba6b9da72a7ab0d2730d7e91ebe Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 24 Dec 2011 16:20:59 +0545 Subject: [PATCH] [DoctribeBridge][SecurityBundle][WebProfiler] Refactor code for HttpFoundation changes. --- .../HttpFoundation/DbalSessionStorage.php | 58 +++++++++---------- .../Tests/Functional/app/config/framework.yml | 2 +- .../EventListener/WebDebugToolbarListener.php | 10 +++- 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php index 6fa324f85e..12697fe86f 100644 --- a/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php +++ b/src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionStorage.php @@ -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 * @author Johannes M. Schmitt */ -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; diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml index 18ed6f4d87..0cc6b74d30 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml @@ -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 } diff --git a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php index a1b6d31a7c..9d6c6e766b 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php +++ b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php @@ -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'))));