From 592752e18ac1303017a12ebbd76bb0b961c3d340 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Tue, 27 Mar 2012 10:24:50 -0400 Subject: [PATCH] Started SessionComponent w/ Symfony integration --- composer.json | 6 +- composer.lock | 12 ++- .../Component/Session/SessionComponent.php | 91 +++++++++++++++++++ .../Session/Storage/Proxy/VirtualProxy.php | 46 ++++++++++ .../Session/Storage/VirtualSessionStorage.php | 62 +++++++++++++ 5 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 src/Ratchet/Component/Session/SessionComponent.php create mode 100644 src/Ratchet/Component/Session/Storage/Proxy/VirtualProxy.php create mode 100644 src/Ratchet/Component/Session/Storage/VirtualSessionStorage.php diff --git a/composer.json b/composer.json index 3000123..de48276 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ } , "repositories": { "guzzle": { - "package": { + "type": "package" + , "package": { "name": "guzzle" , "type": "library" , "version": "2.0.2" @@ -41,4 +42,7 @@ "php": ">=5.3.2" , "guzzle": "2.0.2" } + , "suggest": { + "symfony/http-foundation": "dev-master" + } } \ No newline at end of file diff --git a/composer.lock b/composer.lock index 4e02f7a..fb1cd27 100644 --- a/composer.lock +++ b/composer.lock @@ -1,9 +1,15 @@ { - "hash": "e898a89b9f66807dae53937fe3b089d3", + "hash": "ad8d05f50ba77c0700a2019656894cc6", "packages": [ { "package": "guzzle", "version": "2.0.2" + }, + { + "package": "symfony/http-foundation", + "version": "dev-master", + "source-reference": "ec8ce75d0ec64dfe6de235a7c92282bae066532d" } - ] -} \ No newline at end of file + ], + "aliases": [] +} diff --git a/src/Ratchet/Component/Session/SessionComponent.php b/src/Ratchet/Component/Session/SessionComponent.php new file mode 100644 index 0000000..127e21a --- /dev/null +++ b/src/Ratchet/Component/Session/SessionComponent.php @@ -0,0 +1,91 @@ +_app = $app; + $this->_handler = $handler; + $this->_options = array(); + $this->_null = new NullSessionHandler; + + ini_set('session.auto_start', 0); + ini_set('session.cache_limiter', ''); + ini_set('session.use_cookies', 0); + + $options = $this->setOptions($options); + } + + /** + * {@inheritdoc} + */ + function onOpen(ConnectionInterface $conn) { + if (null === ($id = $conn->WebSocket->headers->getCookie($this->_options['name']))) { + $saveHandler = new NullSessionHandler; + $id = ''; + } else { + $saveHandler = $this->_handler; + } + + $conn->Session = new Session(new VirtualSessionStorage($saveHandler, $id)); + + return $this->_app->onOpen($conn); + } + + /** + * {@inheritdoc} + */ + function onMessage(ConnectionInterface $from, $msg) { + return $this->_app->onMessage($from, $msg); + } + + /** + * {@inheritdoc} + */ + function onClose(ConnectionInterface $conn) { + // "close" session for Connection + + return $this->_app->onClose($conn); + } + + /** + * {@inheritdoc} + */ + function onError(ConnectionInterface $conn, \Exception $e) { + return $this->_app->onError($conn, $e); + } + + protected function setOptions(array $options) { + $all = array( + 'auto_start', 'cache_limiter', 'cookie_domain', 'cookie_httponly', + 'cookie_lifetime', 'cookie_path', 'cookie_secure', + 'entropy_file', 'entropy_length', 'gc_divisor', + 'gc_maxlifetime', 'gc_probability', 'hash_bits_per_character', + 'hash_function', 'name', 'referer_check', + 'serialize_handler', 'use_cookies', + 'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled', + 'upload_progress.cleanup', 'upload_progress.prefix', 'upload_progress.name', + 'upload_progress.freq', 'upload_progress.min-freq', 'url_rewriter.tags' + ); + + foreach ($all as $key) { + if (!array_key_exists($key, $options)) { + $options[$key] = ini_get("session.{$key}"); + } else { + ini_set("session.{$key}", $options[$key]); + } + } + + return $options; + } +} \ No newline at end of file diff --git a/src/Ratchet/Component/Session/Storage/Proxy/VirtualProxy.php b/src/Ratchet/Component/Session/Storage/Proxy/VirtualProxy.php new file mode 100644 index 0000000..780286f --- /dev/null +++ b/src/Ratchet/Component/Session/Storage/Proxy/VirtualProxy.php @@ -0,0 +1,46 @@ +saveHandlerName = 'user'; + $this->_sessionId = $sessionId; + $this->_sessionName = ini_get('session.name'); + } + + /** + * {@inheritdoc} + */ + public function getId() { + return $this->_sessionId; + } + + public function setId($id) { + throw new \RuntimeException("Can not change session id in VirtualProxy"); + } + + /** + * {@inheritdoc} + */ + public function getName() { + return $this->_sessionName; + } + + /** + * DO NOT CALL THIS METHOD + * @param string + * @throws RuntimeException + */ + public function setName($name) { + throw new \RuntimeException("Can not change session name in VirtualProxy"); + } +} \ No newline at end of file diff --git a/src/Ratchet/Component/Session/Storage/VirtualSessionStorage.php b/src/Ratchet/Component/Session/Storage/VirtualSessionStorage.php new file mode 100644 index 0000000..639093b --- /dev/null +++ b/src/Ratchet/Component/Session/Storage/VirtualSessionStorage.php @@ -0,0 +1,62 @@ +setSaveHandler($handler, $id); + } + + /** + * {@inheritdoc} + */ + public function start() { + if ($this->started && !$this->closed) { + return true; + } + + $ignore = array(); + $this->loadSession($ignore); + + if (!$this->saveHandler->isWrapper() && !$this->saveHandler->isSessionHandlerInterface()) { + $this->saveHandler->setActive(false); + } + + return true; + } + + /** + * {@inheritdoc} + */ + public function regenerate($destroy = false) { + // .. ? + } + + /** + * {@inheritdoc} + */ + public function save() { +// $this->saveHandler->write($this->saveHandler->getId(), + + if (!$this->saveHandler->isWrapper() && !$this->getSaveHandler()->isSessionHandlerInterface()) { + $this->saveHandler->setActive(false); + } + + $this->closed = true; + } + + /** + * {@inheritdoc} + */ + public function setSaveHandler(\SessionHandlerInterface $saveHandler, $id) { + if (!($saveHandler instanceof \VirtualProxy)) { + $saveHandler = new VirtualProxy($saveHandler, $id); + } + + $this->saveHandler = $saveHandler; + } +} \ No newline at end of file