Merge branch 'refs/heads/pr/cboden-session-refactor' into 0.4
This commit is contained in:
commit
2691f26e9e
@ -2,7 +2,8 @@
|
|||||||
namespace Ratchet\Session;
|
namespace Ratchet\Session;
|
||||||
use Ratchet\MessageComponentInterface;
|
use Ratchet\MessageComponentInterface;
|
||||||
use Ratchet\ConnectionInterface;
|
use Ratchet\ConnectionInterface;
|
||||||
use Ratchet\WebSocket\WsServerInterface;
|
use Ratchet\Http\HttpServerInterface;
|
||||||
|
use Guzzle\Http\Message\RequestInterface;
|
||||||
use Ratchet\Session\Storage\VirtualSessionStorage;
|
use Ratchet\Session\Storage\VirtualSessionStorage;
|
||||||
use Ratchet\Session\Serialize\HandlerInterface;
|
use Ratchet\Session\Serialize\HandlerInterface;
|
||||||
use Symfony\Component\HttpFoundation\Session\Session;
|
use Symfony\Component\HttpFoundation\Session\Session;
|
||||||
@ -14,7 +15,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
|
|||||||
* Your website must also use Symfony HttpFoundation Sessions to read your sites session data
|
* Your website must also use Symfony HttpFoundation Sessions to read your sites session data
|
||||||
* If your are not using at least PHP 5.4 you must include a SessionHandlerInterface stub (is included in Symfony HttpFoundation, loaded w/ composer)
|
* If your are not using at least PHP 5.4 you must include a SessionHandlerInterface stub (is included in Symfony HttpFoundation, loaded w/ composer)
|
||||||
*/
|
*/
|
||||||
class SessionProvider implements MessageComponentInterface, WsServerInterface {
|
class SessionProvider implements HttpServerInterface {
|
||||||
/**
|
/**
|
||||||
* @var \Ratchet\MessageComponentInterface
|
* @var \Ratchet\MessageComponentInterface
|
||||||
*/
|
*/
|
||||||
@ -38,13 +39,13 @@ class SessionProvider implements MessageComponentInterface, WsServerInterface {
|
|||||||
protected $_serializer;
|
protected $_serializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \Ratchet\MessageComponentInterface $app
|
* @param \Ratchet\HttpServerInterface $app
|
||||||
* @param \SessionHandlerInterface $handler
|
* @param \SessionHandlerInterface $handler
|
||||||
* @param array $options
|
* @param array $options
|
||||||
* @param \Ratchet\Session\Serialize\HandlerInterface $serializer
|
* @param \Ratchet\Session\Serialize\HandlerInterface $serializer
|
||||||
* @throws \RuntimeException
|
* @throws \RuntimeException
|
||||||
*/
|
*/
|
||||||
public function __construct(MessageComponentInterface $app, \SessionHandlerInterface $handler, array $options = array(), HandlerInterface $serializer = null) {
|
public function __construct(HttpServerInterface $app, \SessionHandlerInterface $handler, array $options = array(), HandlerInterface $serializer = null) {
|
||||||
$this->_app = $app;
|
$this->_app = $app;
|
||||||
$this->_handler = $handler;
|
$this->_handler = $handler;
|
||||||
$this->_null = new NullSessionHandler;
|
$this->_null = new NullSessionHandler;
|
||||||
@ -77,8 +78,8 @@ class SessionProvider implements MessageComponentInterface, WsServerInterface {
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
function onOpen(ConnectionInterface $conn) {
|
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) {
|
||||||
if (!isset($conn->WebSocket) || null === ($id = $conn->WebSocket->request->getCookie(ini_get('session.name')))) {
|
if (null === $request || null === ($id = $request->getCookie(ini_get('session.name')))) {
|
||||||
$saveHandler = $this->_null;
|
$saveHandler = $this->_null;
|
||||||
$id = '';
|
$id = '';
|
||||||
} else {
|
} else {
|
||||||
@ -91,7 +92,7 @@ class SessionProvider implements MessageComponentInterface, WsServerInterface {
|
|||||||
$conn->Session->start();
|
$conn->Session->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_app->onOpen($conn);
|
return $this->_app->onOpen($conn, $request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -117,17 +118,6 @@ class SessionProvider implements MessageComponentInterface, WsServerInterface {
|
|||||||
return $this->_app->onError($conn, $e);
|
return $this->_app->onError($conn, $e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getSubProtocols() {
|
|
||||||
if ($this->_app instanceof WsServerInterface) {
|
|
||||||
return $this->_app->getSubProtocols();
|
|
||||||
} else {
|
|
||||||
return array();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set all the php session. ini options
|
* Set all the php session. ini options
|
||||||
* © Symfony
|
* © Symfony
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Session;
|
namespace Ratchet\Session;
|
||||||
use Ratchet\AbstractMessageComponentTestCase;
|
use Ratchet\AbstractMessageComponentTestCase;
|
||||||
use Ratchet\Session\SessionProvider;
|
|
||||||
use Ratchet\Mock\MemorySessionHandler;
|
use Ratchet\Mock\MemorySessionHandler;
|
||||||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
|
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
|
||||||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
|
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
|
||||||
use Guzzle\Http\Message\Request;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers Ratchet\Session\SessionProvider
|
* @covers Ratchet\Session\SessionProvider
|
||||||
@ -35,7 +33,7 @@ class SessionProviderTest extends AbstractMessageComponentTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getComponentClassString() {
|
public function getComponentClassString() {
|
||||||
return '\Ratchet\MessageComponentInterface';
|
return '\Ratchet\Http\HttpServerInterface';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function classCaseProvider() {
|
public function classCaseProvider() {
|
||||||
@ -53,7 +51,7 @@ class SessionProviderTest extends AbstractMessageComponentTestCase {
|
|||||||
$method = $ref->getMethod('toClassCase');
|
$method = $ref->getMethod('toClassCase');
|
||||||
$method->setAccessible(true);
|
$method->setAccessible(true);
|
||||||
|
|
||||||
$component = new SessionProvider($this->getMock('Ratchet\\MessageComponentInterface'), $this->getMock('\SessionHandlerInterface'));
|
$component = new SessionProvider($this->getMock($this->getComponentClassString()), $this->getMock('\SessionHandlerInterface'));
|
||||||
$this->assertEquals($out, $method->invokeArgs($component, array($in)));
|
$this->assertEquals($out, $method->invokeArgs($component, array($in)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,16 +77,13 @@ class SessionProviderTest extends AbstractMessageComponentTestCase {
|
|||||||
$pdo->exec(vsprintf("CREATE TABLE %s (%s VARCHAR(255) PRIMARY KEY, %s TEXT, %s INTEGER)", $dbOptions));
|
$pdo->exec(vsprintf("CREATE TABLE %s (%s VARCHAR(255) PRIMARY KEY, %s TEXT, %s INTEGER)", $dbOptions));
|
||||||
$pdo->prepare(vsprintf("INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?)", $dbOptions))->execute(array($sessionId, base64_encode('_sf2_attributes|a:2:{s:5:"hello";s:5:"world";s:4:"last";i:1332872102;}_sf2_flashes|a:0:{}'), time()));
|
$pdo->prepare(vsprintf("INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?)", $dbOptions))->execute(array($sessionId, base64_encode('_sf2_attributes|a:2:{s:5:"hello";s:5:"world";s:4:"last";i:1332872102;}_sf2_flashes|a:0:{}'), time()));
|
||||||
|
|
||||||
$component = new SessionProvider($this->getMock('Ratchet\\MessageComponentInterface'), new PdoSessionHandler($pdo, $dbOptions), array('auto_start' => 1));
|
$component = new SessionProvider($this->getMock($this->getComponentClassString()), new PdoSessionHandler($pdo, $dbOptions), array('auto_start' => 1));
|
||||||
$connection = $this->getMock('Ratchet\\ConnectionInterface');
|
$connection = $this->getMock('Ratchet\\ConnectionInterface');
|
||||||
|
|
||||||
$headers = $this->getMock('Guzzle\\Http\\Message\\Request', array('getCookie'), array('POST', '/', array()));
|
$headers = $this->getMock('Guzzle\\Http\\Message\\Request', array('getCookie'), array('POST', '/', array()));
|
||||||
$headers->expects($this->once())->method('getCookie', array(ini_get('session.name')))->will($this->returnValue($sessionId));
|
$headers->expects($this->once())->method('getCookie', array(ini_get('session.name')))->will($this->returnValue($sessionId));
|
||||||
|
|
||||||
$connection->WebSocket = new \StdClass;
|
$component->onOpen($connection, $headers);
|
||||||
$connection->WebSocket->request = $headers;
|
|
||||||
|
|
||||||
$component->onOpen($connection);
|
|
||||||
|
|
||||||
$this->assertEquals('world', $connection->Session->get('hello'));
|
$this->assertEquals('world', $connection->Session->get('hello'));
|
||||||
}
|
}
|
||||||
@ -99,9 +94,6 @@ class SessionProviderTest extends AbstractMessageComponentTestCase {
|
|||||||
$headers = $this->getMock('Guzzle\Http\Message\Request', array('getCookie'), array('POST', '/', array()));
|
$headers = $this->getMock('Guzzle\Http\Message\Request', array('getCookie'), array('POST', '/', array()));
|
||||||
$headers->expects($this->once())->method('getCookie', array(ini_get('session.name')))->will($this->returnValue(null));
|
$headers->expects($this->once())->method('getCookie', array(ini_get('session.name')))->will($this->returnValue(null));
|
||||||
|
|
||||||
$conn->WebSocket = new \StdClass;
|
|
||||||
$conn->WebSocket->request = $headers;
|
|
||||||
|
|
||||||
return $conn;
|
return $conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,21 +103,6 @@ class SessionProviderTest extends AbstractMessageComponentTestCase {
|
|||||||
$this->_serv->onMessage($this->_conn, $message);
|
$this->_serv->onMessage($this->_conn, $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetSubProtocolsReturnsArray() {
|
|
||||||
$mock = $this->getMock('Ratchet\\MessageComponentInterface');
|
|
||||||
$comp = new SessionProvider($mock, new NullSessionHandler);
|
|
||||||
|
|
||||||
$this->assertInternalType('array', $comp->getSubProtocols());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetSubProtocolsGetFromApp() {
|
|
||||||
$mock = $this->getMock('Ratchet\WebSocket\Stub\WsMessageComponentInterface');
|
|
||||||
$mock->expects($this->once())->method('getSubProtocols')->will($this->returnValue(array('hello', 'world')));
|
|
||||||
$comp = new SessionProvider($mock, new NullSessionHandler);
|
|
||||||
|
|
||||||
$this->assertGreaterThanOrEqual(2, count($comp->getSubProtocols()));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRejectInvalidSeralizers() {
|
public function testRejectInvalidSeralizers() {
|
||||||
if (!function_exists('wddx_serialize_value')) {
|
if (!function_exists('wddx_serialize_value')) {
|
||||||
$this->markTestSkipped();
|
$this->markTestSkipped();
|
||||||
@ -133,6 +110,6 @@ class SessionProviderTest extends AbstractMessageComponentTestCase {
|
|||||||
|
|
||||||
ini_set('session.serialize_handler', 'wddx');
|
ini_set('session.serialize_handler', 'wddx');
|
||||||
$this->setExpectedException('\RuntimeException');
|
$this->setExpectedException('\RuntimeException');
|
||||||
new SessionProvider($this->getMock('\Ratchet\MessageComponentInterface'), $this->getMock('\SessionHandlerInterface'));
|
new SessionProvider($this->getMock($this->getComponentClassString()), $this->getMock('\SessionHandlerInterface'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user