Interface Cleanup
Separated Observable interface from Decorator interface, also separated config method to its own interface Cleaned up unit tests to reflect interface changes
This commit is contained in:
parent
47b7110dc1
commit
c6a91692f7
12
lib/Ratchet/Application/ApplicationInterface.php
Normal file
12
lib/Ratchet/Application/ApplicationInterface.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\Application;
|
||||||
|
use Ratchet\ObserverInterface;
|
||||||
|
|
||||||
|
interface ApplicationInterface extends ObserverInterface {
|
||||||
|
/**
|
||||||
|
* Decorator pattern
|
||||||
|
* @param Ratchet\ObserverInterface Application to wrap in protocol
|
||||||
|
* @throws UnexpectedValueException
|
||||||
|
*/
|
||||||
|
public function __construct(ApplicationInterface $app = null);
|
||||||
|
}
|
12
lib/Ratchet/Application/ConfiguratorInterface.php
Normal file
12
lib/Ratchet/Application/ConfiguratorInterface.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
namespace Ratchet\Application;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @todo Does this belong in root dir of application
|
||||||
|
*/
|
||||||
|
interface ConfiguratorInterface {
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
static function getDefaultConfig();
|
||||||
|
}
|
@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace Ratchet\Application;
|
|
||||||
use Ratchet\ObserverInterface;
|
|
||||||
|
|
||||||
interface ProtocolInterface extends ObserverInterface {
|
|
||||||
/**
|
|
||||||
* @param Ratchet\ObserverInterface Application to wrap in protocol
|
|
||||||
*/
|
|
||||||
// function __construct(ObserverInterface $application);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
static function getDefaultConfig();
|
|
||||||
}
|
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Application\Server;
|
namespace Ratchet\Application\Server;
|
||||||
use Ratchet\ObserverInterface;
|
use Ratchet\Application\ApplicationInterface;
|
||||||
use Ratchet\SocketInterface;
|
use Ratchet\SocketInterface;
|
||||||
use Ratchet\Resource\Command\CommandInterface;
|
use Ratchet\Resource\Command\CommandInterface;
|
||||||
|
|
||||||
@ -10,7 +10,7 @@ use Ratchet\Resource\Command\CommandInterface;
|
|||||||
* @todo Currently passing Socket object down the decorated chain - should be sending reference to it instead; Receivers do not interact with the Socket directly, they do so through the Command pattern
|
* @todo Currently passing Socket object down the decorated chain - should be sending reference to it instead; Receivers do not interact with the Socket directly, they do so through the Command pattern
|
||||||
* @todo With all these options for the server I should probably use a DIC
|
* @todo With all these options for the server I should probably use a DIC
|
||||||
*/
|
*/
|
||||||
class App implements ObserverInterface {
|
class App implements ApplicationInterface {
|
||||||
/**
|
/**
|
||||||
* The master socket, receives all connections
|
* The master socket, receives all connections
|
||||||
* @var Socket
|
* @var Socket
|
||||||
@ -36,7 +36,7 @@ class App implements ObserverInterface {
|
|||||||
/**
|
/**
|
||||||
* @param Ratchet\ObserverInterface
|
* @param Ratchet\ObserverInterface
|
||||||
*/
|
*/
|
||||||
public function __construct(ObserverInterface $application = null) {
|
public function __construct(ApplicationInterface $application = null) {
|
||||||
if (null === $application) {
|
if (null === $application) {
|
||||||
throw new \UnexpectedValueException("Server requires an application to run off of");
|
throw new \UnexpectedValueException("Server requires an application to run off of");
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,12 @@ namespace Ratchet\Application\WebSocket;
|
|||||||
use Ratchet\Application\WebSocket\Client;
|
use Ratchet\Application\WebSocket\Client;
|
||||||
use Ratchet\Application\WebSocket\VersionInterface;
|
use Ratchet\Application\WebSocket\VersionInterface;
|
||||||
use Ratchet\SocketInterface;
|
use Ratchet\SocketInterface;
|
||||||
use Ratchet\ObserverInterface;
|
use Ratchet\Application\ApplicationInterface;
|
||||||
|
use Ratchet\Application\ConfiguratorInterface;
|
||||||
use Ratchet\Resource\Command\Factory;
|
use Ratchet\Resource\Command\Factory;
|
||||||
use Ratchet\Resource\Command\CommandInterface;
|
use Ratchet\Resource\Command\CommandInterface;
|
||||||
use Ratchet\Resource\Command\Action\SendMessage;
|
use Ratchet\Resource\Command\Action\SendMessage;
|
||||||
use Ratchet\Application\WebSocket\Util\HTTP;
|
use Ratchet\Application\WebSocket\Util\HTTP;
|
||||||
use Ratchet\Application\ProtocolInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The adapter to handle WebSocket requests/responses
|
* The adapter to handle WebSocket requests/responses
|
||||||
@ -20,7 +20,7 @@ use Ratchet\Application\ProtocolInterface;
|
|||||||
* @todo Consider chaning this class to a State Pattern. If a ObserverInterface is passed in __construct, do what is there now. If it's an AppInterface change behaviour of socket interaction (onOpen, handshake, etc)
|
* @todo Consider chaning this class to a State Pattern. If a ObserverInterface is passed in __construct, do what is there now. If it's an AppInterface change behaviour of socket interaction (onOpen, handshake, etc)
|
||||||
* @todo Change namespace to Ratchet\Application\WebSocket\Adapter
|
* @todo Change namespace to Ratchet\Application\WebSocket\Adapter
|
||||||
*/
|
*/
|
||||||
class App implements ProtocolInterface {
|
class App implements ApplicationInterface, ConfiguratorInterface {
|
||||||
/**
|
/**
|
||||||
* Lookup for connected clients
|
* Lookup for connected clients
|
||||||
* @var SplObjectStorage
|
* @var SplObjectStorage
|
||||||
@ -46,7 +46,7 @@ class App implements ProtocolInterface {
|
|||||||
, 'Hixie76' => null
|
, 'Hixie76' => null
|
||||||
);
|
);
|
||||||
|
|
||||||
public function __construct(ObserverInterface $app = null) {
|
public function __construct(ApplicationInterface $app = null) {
|
||||||
if (null === $app) {
|
if (null === $app) {
|
||||||
throw new \UnexpectedValueException("WebSocket requires an application to run off of");
|
throw new \UnexpectedValueException("WebSocket requires an application to run off of");
|
||||||
}
|
}
|
||||||
|
@ -9,13 +9,6 @@ namespace Ratchet;
|
|||||||
* @todo Does this belong in \Ratchet\Server\?
|
* @todo Does this belong in \Ratchet\Server\?
|
||||||
*/
|
*/
|
||||||
interface ObserverInterface {
|
interface ObserverInterface {
|
||||||
/**
|
|
||||||
* Decorator pattern
|
|
||||||
* @param ObserverInterface If nothing is passed it's the end of the line
|
|
||||||
* @throws UnexpectedValueException
|
|
||||||
*/
|
|
||||||
public function __construct(ObserverInterface $app = null);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When a new connection is opened it will be passed to this method
|
* When a new connection is opened it will be passed to this method
|
||||||
* @param SocketInterface The socket/connection that just connected to your application
|
* @param SocketInterface The socket/connection that just connected to your application
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Tests\Mock;
|
namespace Ratchet\Tests\Mock;
|
||||||
use Ratchet\ObserverInterface;
|
use Ratchet\Application\ApplicationInterface;
|
||||||
use Ratchet\Tests\Mock\Socket as MockSocket;
|
use Ratchet\Tests\Mock\Socket as MockSocket;
|
||||||
use Ratchet\SocketInterface;
|
use Ratchet\SocketInterface;
|
||||||
|
|
||||||
class Application implements ObserverInterface {
|
class Application implements ApplicationInterface {
|
||||||
public function __construct(ObserverInterface $app = null) {
|
public function __construct(ApplicationInterface $app = null) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onOpen(SocketInterface $conn) {
|
public function onOpen(SocketInterface $conn) {
|
||||||
|
@ -1,34 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace Ratchet\Tests\Mock;
|
|
||||||
use Ratchet\ObserverInterface;
|
|
||||||
use Ratchet\Application\ProtocolInterface;
|
|
||||||
use Ratchet\Server;
|
|
||||||
use Ratchet\SocketInterface;
|
|
||||||
|
|
||||||
class Protocol implements ProtocolInterface {
|
|
||||||
public function __construct(ObserverInterface $app = null) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function getDefaultConfig() {
|
|
||||||
return array(
|
|
||||||
'domain' => AF_INET
|
|
||||||
, 'type' => SOCK_STREAM
|
|
||||||
, 'protocol' => SOL_TCP
|
|
||||||
, 'options' => array(
|
|
||||||
SOL_SOCKET => array(SO_REUSEADDR => 1)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onOpen(SocketInterface $conn) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onRecv(SocketInterface $from, $msg) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onClose(SocketInterface $conn) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onError(SocketInterface $conn, \Exception $e) {
|
|
||||||
}
|
|
||||||
}
|
|
@ -14,11 +14,6 @@ class WebSocketTest extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->_ws = new WebSocket(new Application);
|
$this->_ws = new WebSocket(new Application);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testServerImplementsServerInterface() {
|
|
||||||
$constraint = $this->isInstanceOf('\\Ratchet\\ObserverInterface');
|
|
||||||
$this->assertThat($this->_ws, $constraint);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetConfigReturnsArray() {
|
public function testGetConfigReturnsArray() {
|
||||||
$this->assertInternalType('array', $this->_ws->getDefaultConfig());
|
$this->assertInternalType('array', $this->_ws->getDefaultConfig());
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
namespace Ratchet\Tests;
|
namespace Ratchet\Tests;
|
||||||
use Ratchet\Tests\Mock\FakeSocket as Socket;
|
use Ratchet\Tests\Mock\FakeSocket as Socket;
|
||||||
use Ratchet\Socket as RealSocket;
|
use Ratchet\Socket as RealSocket;
|
||||||
use Ratchet\Tests\Mock\Protocol;
|
|
||||||
use Ratchet\Tests\Mock\Application as TestApp;
|
use Ratchet\Tests\Mock\Application as TestApp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,24 +44,6 @@ class SocketTest extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->_socket->fake_method();
|
$this->_socket->fake_method();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstructionFromProtocolInterfaceConfig() {
|
|
||||||
$protocol = new Protocol(new TestApp);
|
|
||||||
$socket = Socket::createFromConfig($protocol);
|
|
||||||
|
|
||||||
$this->assertInstanceOf('\\Ratchet\\Socket', $socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testCreationFromConfigOutputMatchesInput() {
|
|
||||||
$protocol = new Protocol(new TestApp);
|
|
||||||
$socket = Socket::createFromConfig($protocol);
|
|
||||||
$config = $protocol::getDefaultConfig();
|
|
||||||
|
|
||||||
// change this to array_filter late
|
|
||||||
unset($config['options']);
|
|
||||||
|
|
||||||
$this->assertAttributeEquals($config, '_arguments', $socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function asArrayProvider() {
|
public function asArrayProvider() {
|
||||||
return array(
|
return array(
|
||||||
array(array('hello' => 'world'), array('hello' => 'world'))
|
array(array('hello' => 'world'), array('hello' => 'world'))
|
||||||
|
Loading…
Reference in New Issue
Block a user