From 33ed96eba080260a6bf19183b845848f5ea4bc45 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Tue, 27 Mar 2012 14:29:56 -0400 Subject: [PATCH] Session read working --- .../Session/Serialize/HandlerInterface.php | 16 ++++++++ .../Session/Serialize/PhpBinaryHandler.php | 33 +++++++++++++++++ .../Session/Serialize/PhpHandler.php | 37 +++++++++++++++++++ .../Component/Session/SessionComponent.php | 20 ++++++++-- .../Session/Storage/Proxy/VirtualProxy.php | 15 ++++++-- .../Session/Storage/VirtualSessionStorage.php | 29 ++++++++++++--- 6 files changed, 138 insertions(+), 12 deletions(-) create mode 100644 src/Ratchet/Component/Session/Serialize/HandlerInterface.php create mode 100644 src/Ratchet/Component/Session/Serialize/PhpBinaryHandler.php create mode 100644 src/Ratchet/Component/Session/Serialize/PhpHandler.php diff --git a/src/Ratchet/Component/Session/Serialize/HandlerInterface.php b/src/Ratchet/Component/Session/Serialize/HandlerInterface.php new file mode 100644 index 0000000..42774b1 --- /dev/null +++ b/src/Ratchet/Component/Session/Serialize/HandlerInterface.php @@ -0,0 +1,16 @@ +_app = $app; $this->_handler = $handler; $this->_options = array(); @@ -24,6 +27,17 @@ class SessionComponent implements MessageComponentInterface { ini_set('session.use_cookies', 0); $options = $this->setOptions($options); + + if (null === $serializer) { + $serialClass = __NAMESPACE__ . '\\Serialize\\' . str_replace(' ', '', ucwords(str_replace('_', ' ', ini_get('session.serialize_handler')))) . 'Handler'; // awesome/terrible hack, eh? + if (!class_exists($serialClass)) { + throw new \RuntimeExcpetion('Unable to parse session serialize handler'); + } + + $serializer = new $serialClass; + } + + $this->_serializer = $serializer; } /** @@ -31,13 +45,13 @@ class SessionComponent implements MessageComponentInterface { */ function onOpen(ConnectionInterface $conn) { if (null === ($id = $conn->WebSocket->headers->getCookie($this->_options['name']))) { - $saveHandler = new NullSessionHandler; + $saveHandler = $this->_null; $id = ''; } else { $saveHandler = $this->_handler; } - $conn->Session = new Session(new VirtualSessionStorage($saveHandler, $id)); + $conn->Session = new Session(new VirtualSessionStorage($saveHandler, $id, $this->_serializer)); return $this->_app->onOpen($conn); } diff --git a/src/Ratchet/Component/Session/Storage/Proxy/VirtualProxy.php b/src/Ratchet/Component/Session/Storage/Proxy/VirtualProxy.php index 780286f..25ce486 100644 --- a/src/Ratchet/Component/Session/Storage/Proxy/VirtualProxy.php +++ b/src/Ratchet/Component/Session/Storage/Proxy/VirtualProxy.php @@ -3,17 +3,23 @@ namespace Ratchet\Component\Session\Storage\Proxy; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; class VirtualProxy extends SessionHandlerProxy { + /** + * @var string + */ protected $_sessionId; + + /** + * @var string + */ protected $_sessionName; /** * {@inheritdoc} */ - public function __construct(\SessionHandlerInterface $handler, $sessionId) { + public function __construct(\SessionHandlerInterface $handler) { parent::__construct($handler); $this->saveHandlerName = 'user'; - $this->_sessionId = $sessionId; $this->_sessionName = ini_get('session.name'); } @@ -24,8 +30,11 @@ class VirtualProxy extends SessionHandlerProxy { return $this->_sessionId; } + /** + * {@inheritdoc} + */ public function setId($id) { - throw new \RuntimeException("Can not change session id in VirtualProxy"); + $this->_sessionId = $id; } /** diff --git a/src/Ratchet/Component/Session/Storage/VirtualSessionStorage.php b/src/Ratchet/Component/Session/Storage/VirtualSessionStorage.php index 639093b..9e3bdd4 100644 --- a/src/Ratchet/Component/Session/Storage/VirtualSessionStorage.php +++ b/src/Ratchet/Component/Session/Storage/VirtualSessionStorage.php @@ -2,13 +2,21 @@ namespace Ratchet\Component\Session\Storage; use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; use Ratchet\Component\Session\Storage\Proxy\VirtualProxy; +use Ratchet\Component\Session\Serialize\HandlerInterface; class VirtualSessionStorage extends NativeSessionStorage { + /** + * @var Ratchet\Component\Session\Serialize\HandlerInterface + */ + protected $_serializer; + /** * {@inheritdoc} */ - public function __construct(\SessionHandlerInterface $handler, $id) { - $this->setSaveHandler($handler, $id); + public function __construct(\SessionHandlerInterface $handler, $sessionId, HandlerInterface $serializer) { + $this->setSaveHandler($handler); + $this->saveHandler->setId($sessionId); + $this->_serializer = $serializer; } /** @@ -19,8 +27,10 @@ class VirtualSessionStorage extends NativeSessionStorage { return true; } - $ignore = array(); - $this->loadSession($ignore); + $rawData = $this->saveHandler->read($this->saveHandler->getId()); + $sessionData = $this->_serializer->unserialize($rawData); + + $this->loadSession($sessionData); if (!$this->saveHandler->isWrapper() && !$this->saveHandler->isSessionHandlerInterface()) { $this->saveHandler->setActive(false); @@ -40,6 +50,9 @@ class VirtualSessionStorage extends NativeSessionStorage { * {@inheritdoc} */ public function save() { + // get the data from the bags? + // serialize the data + // save the data using the saveHandler // $this->saveHandler->write($this->saveHandler->getId(), if (!$this->saveHandler->isWrapper() && !$this->getSaveHandler()->isSessionHandlerInterface()) { @@ -52,9 +65,13 @@ class VirtualSessionStorage extends NativeSessionStorage { /** * {@inheritdoc} */ - public function setSaveHandler(\SessionHandlerInterface $saveHandler, $id) { + public function setSaveHandler($saveHandler = null) { + if (!($saveHandler instanceof \SessionHandlerInterface)) { + throw new \InvalidArgumentException('Handler must be instance of SessionHandlerInterface'); + } + if (!($saveHandler instanceof \VirtualProxy)) { - $saveHandler = new VirtualProxy($saveHandler, $id); + $saveHandler = new VirtualProxy($saveHandler); } $this->saveHandler = $saveHandler;