Started SessionComponent w/ Symfony integration

This commit is contained in:
Chris Boden 2012-03-27 10:24:50 -04:00
parent 9ff2d406f0
commit 592752e18a
5 changed files with 213 additions and 4 deletions

View File

@ -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"
}
}

10
composer.lock generated
View File

@ -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"
}
]
],
"aliases": []
}

View File

@ -0,0 +1,91 @@
<?php
namespace Ratchet\Component\Session;
use Ratchet\Component\MessageComponentInterface;
use Ratchet\Resource\ConnectionInterface;
use Ratchet\Component\Session\Storage\VirtualSessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
class SessionComponent implements MessageComponentInterface {
protected $_app;
protected $_options;
protected $_handler;
protected $_null;
public function __construct(MessageComponentInterface $app, \SessionHandlerInterface $handler, array $options = array()) {
$this->_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;
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace Ratchet\Component\Session\Storage\Proxy;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
class VirtualProxy extends SessionHandlerProxy {
protected $_sessionId;
protected $_sessionName;
/**
* {@inheritdoc}
*/
public function __construct(\SessionHandlerInterface $handler, $sessionId) {
parent::__construct($handler);
$this->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");
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace Ratchet\Component\Session\Storage;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Ratchet\Component\Session\Storage\Proxy\VirtualProxy;
class VirtualSessionStorage extends NativeSessionStorage {
/**
* {@inheritdoc}
*/
public function __construct(\SessionHandlerInterface $handler, $id) {
$this->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;
}
}