Init NS Refactor

Started refactor of namespaces from "app" to "component"
Added ConnectionInterface to replace concrete Connection
Removed socket config classes
This commit is contained in:
Chris Boden 2012-01-30 17:35:49 -05:00
parent ad9b8c1644
commit 451f7f4235
52 changed files with 205 additions and 255 deletions

View File

@ -38,11 +38,11 @@ I'm looking into a couple daemonized servers written in PHP to run Ratchet on to
```php
<?php
namespace MyApps;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Component\ComponentInterface;
use Ratchet\Resource\Connection;
use Ratchet\Socket;
use Ratchet\Application\Server\App as Server;
use Ratchet\Application\WebSocket\App as WebSocket;
use Ratchet\Component\Server\App as Server;
use Ratchet\Component\WebSocket\App as WebSocket;
use Ratchet\Resource\Command\Composite as Cmds;
use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Resource\Command\Action\CloseConnection;
@ -51,10 +51,10 @@ use Ratchet\Resource\Command\Action\CloseConnection;
* chat.php
* Send any incoming messages to all connected clients (except sender)
*/
class Chat implements ApplicationInterface {
class Chat implements ComponentInterface {
protected $_clients;
public function __construct(ApplicationInterface $app = null) {
public function __construct(ComponentInterface $app = null) {
$this->_clients = new \SplObjectStorage;
}

View File

@ -1,12 +0,0 @@
<?php
namespace Ratchet\Application;
/**
* @todo Does this belong in root dir of application
*/
interface ConfiguratorInterface {
/**
* @return array
*/
static function getDefaultConfig();
}

View File

@ -1,12 +0,0 @@
<?php
namespace Ratchet\Application\WebSocket\Command\Action;
use Ratchet\Resource\Command\ActionTemplate;
use Ratchet\Application\ApplicationInterface;
/**
* Not yet implemented/completed
*/
class Ping extends ActionTemplate {
public function execute(ApplicationInterface $scope = null) {
}
}

View File

@ -1,12 +0,0 @@
<?php
namespace Ratchet\Application\WebSocket\Command\Action;
use Ratchet\Resource\Command\ActionTemplate;
use Ratchet\Application\ApplicationInterface;
/**
* Not yet implemented/completed
*/
class Pong extends ActionTemplate {
public function execute(ApplicationInterface $scope = null) {
}
}

View File

@ -1,28 +1,19 @@
<?php
namespace Ratchet\Application;
use Ratchet\Resource\Connection;
namespace Ratchet\Component;
use Ratchet\Resource\ConnectionInterface;
/**
* This is the interface to build a Ratchet application with
* It impelemtns the decorator and command pattern to build an application stack
*/
interface ApplicationInterface {
interface ComponentInterface {
/**
* When a new connection is opened it will be passed to this method
* @param Ratchet\Resource\Connection The socket/connection that just connected to your application
* @return Ratchet\Resource\Command\CommandInterface|null
* @throws Exception
*/
function onOpen(Connection $conn);
/**
* Triggered when a client sends data through the socket
* @param Ratchet\Resource\Connection The socket/connection that sent the message to your application
* @param string The message received
* @return Ratchet\Resource\Command\CommandInterface|null
* @throws Exception
*/
function onMessage(Connection $from, $msg);
function onOpen(ConnectionInterface $conn);
/**
* This is called before or after a socket is closed (depends on how it's closed). SendMessage to $conn will not result in an error if it has already been closed.
@ -30,7 +21,7 @@ interface ApplicationInterface {
* @return Ratchet\Resource\Command\CommandInterface|null
* @throws Exception
*/
function onClose(Connection $conn);
function onClose(ConnectionInterface $conn);
/**
* If there is an error with one of the sockets, or somewhere in the application where an Exception is thrown,
@ -40,5 +31,5 @@ interface ApplicationInterface {
* @return Ratchet\Resource\Command\CommandInterface|null
* @throws Exception
*/
function onError(Connection $conn, \Exception $e);
function onError(ConnectionInterface $conn, \Exception $e);
}

View File

@ -1,7 +1,8 @@
<?php
namespace Ratchet\Application\Server;
use Ratchet\Application\ApplicationInterface;
namespace Ratchet\Component\Server;
use Ratchet\Component\ComponentInterface;
use Ratchet\SocketInterface;
use Ratchet\Resource\ConnectionInterface;
use Ratchet\Resource\Connection;
use Ratchet\Resource\Command\CommandInterface;
@ -9,7 +10,7 @@ use Ratchet\Resource\Command\CommandInterface;
* Creates an open-ended socket to listen on a port for incomming connections. Events are delegated through this to attached applications
* @todo With all these options for the server I should probably use a DIC
*/
class App implements ApplicationInterface {
class App implements ComponentInterface {
/**
* @var array of Socket Resources
*/
@ -22,7 +23,7 @@ class App implements ApplicationInterface {
/**
* The decorated application to send events to
* @var Ratchet\Application\ApplicationInterface
* @var Ratchet\Component\ComponentInterface
*/
protected $_app;
@ -41,7 +42,7 @@ class App implements ApplicationInterface {
*/
protected $_run = true;
public function __construct(ApplicationInterface $application) {
public function __construct(ComponentInterface $application) {
$this->_app = $application;
}
@ -147,7 +148,7 @@ class App implements ApplicationInterface {
}
}
public function onOpen(Connection $conn) {
public function onOpen(ConnectionInterface $conn) {
$new_socket = clone $conn->getSocket();
$new_socket->set_nonblock();
$new_connection = new Connection($new_socket);
@ -158,11 +159,11 @@ class App implements ApplicationInterface {
return $this->_app->onOpen($new_connection);
}
public function onMessage(Connection $from, $msg) {
public function onMessage(ConnectionInterface $from, $msg) {
return $this->_app->onMessage($from, $msg);
}
public function onClose(Connection $conn) {
public function onClose(ConnectionInterface $conn) {
$resource = $conn->getSocket()->getResource();
$cmd = $this->_app->onClose($conn);
@ -172,7 +173,7 @@ class App implements ApplicationInterface {
return $cmd;
}
public function onError(Connection $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, \Exception $e) {
return $this->_app->onError($conn, $e);
}
}

View File

@ -1,11 +1,11 @@
<?php
namespace Ratchet\Application\WAMP;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Application\WebSocket\WebSocketAppInterface;
use Ratchet\Resource\Connection;
namespace Ratchet\Component\WAMP;
use Ratchet\Component\ComponentInterface;
use Ratchet\Component\WebSocket\WebSocketAppInterface;
use Ratchet\Resource\ConnectionInterface;
use Ratchet\Resource\Command\Composite;
use Ratchet\Resource\Command\CommandInterface;
use Ratchet\Application\WAMP\Command\Action\Prefix;
use Ratchet\Component\WAMP\Command\Action\Prefix;
/**
* WebSocket Application Messaging Protocol
@ -52,7 +52,7 @@ class App implements WebSocketAppInterface {
/**
* @todo WAMP spec does not say what to do when there is an error with PREFIX...
*/
public function addPrefix(Connection $conn, $curie, $uri, $from_server = false) {
public function addPrefix(ConnectionInterface $conn, $curie, $uri, $from_server = false) {
// validate uri
// validate curie
@ -68,7 +68,7 @@ class App implements WebSocketAppInterface {
}
}
public function onOpen(Connection $conn) {
public function onOpen(ConnectionInterface $conn) {
$conn->WAMP = new \StdClass;
$conn->WAMP->prefixes = array();
$conn->WAMP->subscriptions = array();
@ -86,7 +86,7 @@ class App implements WebSocketAppInterface {
* @throws Exception
* @throws JSONException
*/
public function onMessage(Connection $from, $msg) {
public function onMessage(ConnectionInterface $from, $msg) {
if (null === ($json = @json_decode($msg, true))) {
throw new JSONException;
}
@ -133,7 +133,7 @@ class App implements WebSocketAppInterface {
* @param ...
* @return string
*/
protected function getUri(Connection $conn, $uri) {
protected function getUri(ConnectionInterface $conn, $uri) {
return (isset($conn->WAMP->prefixes[$uri]) ? $conn->WAMP->prefixes[$uri] : $uri);
}
@ -157,11 +157,11 @@ class App implements WebSocketAppInterface {
$this->_msg_buffer = new Composite;
}
public function onClose(Connection $conn) {
public function onClose(ConnectionInterface $conn) {
return $this->_app->onClose($conn);
}
public function onError(Connection $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, \Exception $e) {
return $this->_app->onError($conn, $e);
}
}

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet\Application\WAMP\Command\Action;
namespace Ratchet\Component\WAMP\Command\Action;
use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Application\WAMP\App as WAMP;
use Ratchet\Component\WAMP\App as WAMP;
class CallError extends SendMessage {
protected $_id;

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet\Application\WAMP\Command\Action;
namespace Ratchet\Component\WAMP\Command\Action;
use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Application\WAMP\App as WAMP;
use Ratchet\Component\WAMP\App as WAMP;
/**
*/

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet\Application\WAMP\Command\Action;
namespace Ratchet\Component\WAMP\Command\Action;
use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Application\WAMP\App as WAMP;
use Ratchet\Component\WAMP\App as WAMP;
/**
* This is an event in the context of a topicURI

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet\Application\WAMP\Command\Action;
namespace Ratchet\Component\WAMP\Command\Action;
use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Application\WAMP\App as WAMP;
use Ratchet\Component\WAMP\App as WAMP;
/**
* Send a curie to uri mapping to the client

View File

@ -1,5 +1,5 @@
<?php
namespace Ratchet\Application\WAMP;
namespace Ratchet\Component\WAMP;
class Exception extends \Exception {
}

View File

@ -1,5 +1,5 @@
<?php
namespace Ratchet\Application\WAMP;
namespace Ratchet\Component\WAMP;
class JSONException extends Exception {
public function __construct() {

View File

@ -1,9 +1,9 @@
<?php
namespace Ratchet\Application\WAMP;
use Ratchet\Resource\Connection;
namespace Ratchet\Component\WAMP;
use Ratchet\Resource\ConnectionInterface;
/**
* A (not literal) extension of Ratchet\Application\ApplicationInterface
* A (not literal) extension of Ratchet\Component\ComponentInterface
* onMessage is replaced by various types of messages for this protocol (pub/sub or rpc)
* @todo Thought: URI as class. Class has short and long version stored (if as prefix)
*/
@ -12,21 +12,21 @@ interface ServerInterface {
* When a new connection is opened it will be passed to this method
* @param Ratchet\Resource\Connection
*/
function onOpen(Connection $conn);
function onOpen(ConnectionInterface $conn);
/**
* The user closed their connection
* @param Ratchet\Resource\Connection
* @return Ratchet\Resource\Command\CommandInterface|null
*/
function onClose(Connection $conn);
function onClose(ConnectionInterface $conn);
/**
* @param Ratchet\Resource\Connection
* @param \Exception
* @return Ratchet\Resource\Command\CommandInterface|null
*/
function onError(Connection $conn, \Exception $e);
function onError(ConnectionInterface $conn, \Exception $e);
/**
* An RPC call has been received
@ -36,7 +36,7 @@ interface ServerInterface {
* @param array Call parameters received from the client
* @return Ratchet\Resource\Command\CommandInterface|null
*/
function onCall(Connection $conn, $id, $procURI, array $params);
function onCall(ConnectionInterface $conn, $id, $procURI, array $params);
/**
* A request to subscribe to a URI has been made
@ -44,7 +44,7 @@ interface ServerInterface {
* @param ...
* @return Ratchet\Resource\Command\CommandInterface|null
*/
function onSubscribe(Connection $conn, $uri);
function onSubscribe(ConnectionInterface $conn, $uri);
/**
* A request to unsubscribe from a URI has been made
@ -52,7 +52,7 @@ interface ServerInterface {
* @param ...
* @return Ratchet\Resource\Command\CommandInterface|null
*/
function onUnSubscribe(Connection $conn, $uri);
function onUnSubscribe(ConnectionInterface $conn, $uri);
/**
* A client is attempting to publish content to a subscribed connections on a URI
@ -61,5 +61,5 @@ interface ServerInterface {
* @param string
* @return Ratchet\Resource\Command\CommandInterface|null
*/
function onPublish(Connection $conn, $uri, $event);
function onPublish(ConnectionInterface $conn, $uri, $event);
}

View File

@ -1,13 +1,12 @@
<?php
namespace Ratchet\Application\WebSocket;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Application\ConfiguratorInterface;
use Ratchet\Resource\Connection;
namespace Ratchet\Component\WebSocket;
use Ratchet\Component\ComponentInterface;
use Ratchet\Resource\ConnectionInterface;
use Ratchet\Resource\Command\Factory;
use Ratchet\Resource\Command\CommandInterface;
use Ratchet\Resource\Command\Action\SendMessage;
use Guzzle\Http\Message\RequestInterface;
use Ratchet\Application\WebSocket\Guzzle\Http\Message\RequestFactory;
use Ratchet\Component\WebSocket\Guzzle\Http\Message\RequestFactory;
/**
* The adapter to handle WebSocket requests/responses
@ -17,10 +16,10 @@ use Ratchet\Application\WebSocket\Guzzle\Http\Message\RequestFactory;
* @todo Learn about closing the socket. A message has to be sent prior to closing - does the message get sent onClose event or CloseConnection command?
* @todo Consider chaning this class to a State Pattern. If a WS App interface is passed use different state for additional methods used
*/
class App implements ApplicationInterface, ConfiguratorInterface {
class App implements ComponentInterface {
/**
* Decorated application
* @var Ratchet\Application\ApplicationInterface
* @var Ratchet\Component\ComponentInterface
*/
protected $_app;
@ -48,28 +47,12 @@ class App implements ApplicationInterface, ConfiguratorInterface {
*/
public $accepted_subprotocols = array();
public function __construct(ApplicationInterface $app) {
public function __construct(ComponentInterface $app) {
$this->_app = $app;
$this->_factory = new Factory;
}
/**
* Return the desired socket configuration if hosting a WebSocket server
* This method may be removed
* @return array
*/
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(Connection $conn) {
public function onOpen(ConnectionInterface $conn) {
$conn->WebSocket = new \stdClass;
$conn->WebSocket->handshake = false;
$conn->WebSocket->headers = '';
@ -80,7 +63,7 @@ class App implements ApplicationInterface, ConfiguratorInterface {
* @todo This needs some major refactoring
* @todo "Once the client's opening handshake has been sent, the client MUST wait for a response from the server before sending any further data."
*/
public function onMessage(Connection $from, $msg) {
public function onMessage(ConnectionInterface $from, $msg) {
if (true !== $from->WebSocket->handshake) {
if (!isset($from->WebSocket->version)) {
$from->WebSocket->headers .= $msg;
@ -160,14 +143,14 @@ class App implements ApplicationInterface, ConfiguratorInterface {
}
}
public function onClose(Connection $conn) {
public function onClose(ConnectionInterface $conn) {
return $this->prepareCommand($this->_app->onClose($conn));
}
/**
* @todo Shouldn't I be using prepareCommand() on the return? look into this
*/
public function onError(Connection $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, \Exception $e) {
return $this->_app->onError($conn, $e);
}

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet\Application\WebSocket\Command\Action;
namespace Ratchet\Component\WebSocket\Command\Action;
use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Component\ComponentInterface;
/**
* Not yet implemented/completed
@ -15,7 +15,7 @@ class Disconnect extends SendMessage {
// re-do message based on code
}
public function execute(ApplicationInterface $scope = null) {
public function execute(ComponentInterface $scope = null) {
parent::execute();
$this->_socket->close();
}

View File

@ -0,0 +1,12 @@
<?php
namespace Ratchet\Component\WebSocket\Command\Action;
use Ratchet\Resource\Command\ActionTemplate;
use Ratchet\Component\ComponentInterface;
/**
* Not yet implemented/completed
*/
class Ping extends ActionTemplate {
public function execute(ComponentInterface $scope = null) {
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace Ratchet\Component\WebSocket\Command\Action;
use Ratchet\Resource\Command\ActionTemplate;
use Ratchet\Component\ComponentInterface;
/**
* Not yet implemented/completed
*/
class Pong extends ActionTemplate {
public function execute(ComponentInterface $scope = null) {
}
}

View File

@ -1,5 +1,5 @@
<?php
namespace Ratchet\Application\WebSocket\Guzzle\Http\Message;
namespace Ratchet\Component\WebSocket\Guzzle\Http\Message;
use Guzzle\Http\Message\RequestFactory as gReqFac;
use Guzzle\Http\Url;

View File

@ -1,5 +1,5 @@
<?php
namespace Ratchet\Application\WebSocket\Version;
namespace Ratchet\Component\WebSocket\Version;
interface FrameInterface {
/**

View File

@ -1,5 +1,5 @@
<?php
namespace Ratchet\Application\WebSocket\Version;
namespace Ratchet\Component\WebSocket\Version;
use Guzzle\Http\Message\RequestInterface;
/**

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet\Application\WebSocket\Version\Hixie76;
use Ratchet\Application\WebSocket\Version\FrameInterface;
namespace Ratchet\Component\WebSocket\Version\Hixie76;
use Ratchet\Component\WebSocket\Version\FrameInterface;
/**
* This does not entirely follow the protocol to spec, but (mostly) works

View File

@ -1,11 +1,11 @@
<?php
namespace Ratchet\Application\WebSocket\Version\Hixie76;
use Ratchet\Application\WebSocket\Version\MessageInterface;
use Ratchet\Application\WebSocket\Version\FrameInterface;
namespace Ratchet\Component\WebSocket\Version\Hixie76;
use Ratchet\Component\WebSocket\Version\MessageInterface;
use Ratchet\Component\WebSocket\Version\FrameInterface;
class Message implements MessageInterface {
/**
* @var Ratchet\Application\WebSocket\Version\FrameInterface
* @var Ratchet\Component\WebSocket\Version\FrameInterface
*/
protected $_frame = null;

View File

@ -1,5 +1,5 @@
<?php
namespace Ratchet\Application\WebSocket\Version;
namespace Ratchet\Component\WebSocket\Version;
use Guzzle\Http\Message\RequestInterface;
/**

View File

@ -1,5 +1,5 @@
<?php
namespace Ratchet\Application\WebSocket\Version;
namespace Ratchet\Component\WebSocket\Version;
/**
* @todo Consider making parent interface/composite for Message/Frame with (isCoalesced, getOpcdoe, getPayloadLength, getPayload)

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet\Application\WebSocket\Version;
use Ratchet\Application\WebSocket\Version\RFC6455\HandshakeVerifier;
namespace Ratchet\Component\WebSocket\Version;
use Ratchet\Component\WebSocket\Version\RFC6455\HandshakeVerifier;
use Guzzle\Http\Message\RequestInterface;
/**

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet\Application\WebSocket\Version\RFC6455;
use Ratchet\Application\WebSocket\Version\FrameInterface;
namespace Ratchet\Component\WebSocket\Version\RFC6455;
use Ratchet\Component\WebSocket\Version\FrameInterface;
class Frame implements FrameInterface {
/**

View File

@ -1,5 +1,5 @@
<?php
namespace Ratchet\Application\WebSocket\Version\RFC6455;
namespace Ratchet\Component\WebSocket\Version\RFC6455;
use Guzzle\Http\Message\RequestInterface;
/**

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet\Application\WebSocket\Version\RFC6455;
use Ratchet\Application\WebSocket\Version\MessageInterface;
use Ratchet\Application\WebSocket\Version\FrameInterface;
namespace Ratchet\Component\WebSocket\Version\RFC6455;
use Ratchet\Component\WebSocket\Version\MessageInterface;
use Ratchet\Component\WebSocket\Version\FrameInterface;
class Message implements MessageInterface {
/**

View File

@ -1,5 +1,5 @@
<?php
namespace Ratchet\Application\WebSocket\Version;
namespace Ratchet\Component\WebSocket\Version;
use Guzzle\Http\Message\RequestInterface;
/**

View File

@ -1,13 +1,13 @@
<?php
namespace Ratchet\Application\WebSocket;
use Ratchet\Application\ApplicationInterface;
namespace Ratchet\Component\WebSocket;
use Ratchet\Component\ComponentInterface;
/**
* @todo App interfaces this (optionally) if is meant for WebSocket
* @todo WebSocket checks if instanceof AppInterface, if so uses getSubProtocol() when doing handshake
* @todo Pick a better name for this...
*/
interface WebSocketAppInterface extends ApplicationInterface {
interface WebSocketAppInterface extends ComponentInterface {
/**
* Currently instead of this, I'm setting header in the Connection object passed around...not sure which I like more
* @param string

View File

@ -1,8 +1,8 @@
<?php
namespace Ratchet\Resource\Command\Action;
use Ratchet\Resource\Command\ActionTemplate;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Resource\Connection;
use Ratchet\Component\ComponentInterface;
use Ratchet\Resource\ConnectionInterface;
use Ratchet\Resource\Command\CommandInterface;
use Ratchet\Resource\Command\Composite;
@ -10,7 +10,7 @@ use Ratchet\Resource\Command\Composite;
* Close the connection to the sockets passed in the constructor
*/
class CloseConnection extends ActionTemplate {
function execute(ApplicationInterface $scope = null) {
function execute(ComponentInterface $scope = null) {
// All this code allows an application to have its onClose method executed before the socket is actually closed
$ret = $scope->onClose($this->getConnection());
@ -19,7 +19,7 @@ class CloseConnection extends ActionTemplate {
$comp->enqueue($ret);
$rt = new Runtime($this->getConnection());
$rt->setCommand(function(Connection $conn, ApplicationInterface $scope) {
$rt->setCommand(function(ConnectionInterface $conn, ComponentInterface $scope) {
$conn->getSocket()->close();
});
$comp->enqueue($rt);

View File

@ -1,12 +1,12 @@
<?php
namespace Ratchet\Resource\Command\Action;
use Ratchet\Resource\Command\ActionTemplate;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Component\ComponentInterface;
/**
* Null pattern - execution does nothing, something needs to be passed back though
*/
class Null extends ActionTemplate {
public function execute(ApplicationInterface $scope = null) {
public function execute(ComponentInterface $scope = null) {
}
}

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet\Resource\Command\Action;
use Ratchet\Resource\Command\ActionTemplate;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Component\ComponentInterface;
class Runtime extends ActionTemplate {
/**
@ -10,14 +10,14 @@ class Runtime extends ActionTemplate {
protected $_command = null;
/**
* Your closure should accept two parameters (\Ratchet\Resource\Connection, \Ratchet\Application\ApplicationInterface) parameter and return a CommandInterface or NULL
* Your closure should accept two parameters (\Ratchet\Resource\Connection, \Ratchet\Component\ComponentInterface) parameter and return a CommandInterface or NULL
* @param Closure Your closure/lambda to execute when the time comes
*/
public function setCommand(\Closure $callback) {
$this->_command = $callback;
}
public function execute(ApplicationInterface $scope = null) {
public function execute(ComponentInterface $scope = null) {
return call_user_func($this->_command, $this->getConnection(), $scope);
}
}

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet\Resource\Command\Action;
use Ratchet\Resource\Command\ActionTemplate;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Component\ComponentInterface;
/**
* Send text back to the client end of the socket(s)
@ -33,7 +33,7 @@ class SendMessage extends ActionTemplate {
/**
* @throws \UnexpectedValueException if a message was not set with setMessage()
*/
public function execute(ApplicationInterface $scope = null) {
public function execute(ComponentInterface $scope = null) {
if (empty($this->_message)) {
throw new \UnexpectedValueException("Message is empty");
}

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet\Resource\Command;
use Ratchet\Resource\Connection;
use Ratchet\Resource\ConnectionInterface;
/**
* A single command tied to 1 socket connection
@ -10,7 +10,7 @@ interface ActionInterface extends CommandInterface {
* Pass the Sockets to execute the command on
* @param Ratchet\Resource\Connection
*/
function __construct(Connection $conn);
function __construct(ConnectionInterface $conn);
/**
* @return Ratchet\Command\Connection

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet\Resource\Command;
use Ratchet\Resource\Connection;
use Ratchet\Resource\ConnectionInterface;
abstract class ActionTemplate implements ActionInterface {
/**
@ -8,7 +8,7 @@ abstract class ActionTemplate implements ActionInterface {
*/
protected $_conn;
public function __construct(Connection $conn) {
public function __construct(ConnectionInterface $conn) {
$this->_conn = $conn;
}

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet\Resource\Command;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Component\ComponentInterface;
/**
* Socket implementation of the Command Pattern
@ -9,8 +9,8 @@ use Ratchet\Application\ApplicationInterface;
interface CommandInterface {
/**
* The Server class will call the execution
* @param Ratchet\ApplicationInterface Scope to execute the command under
* @param Ratchet\ComponentInterface Scope to execute the command under
* @return CommandInterface|NULL
*/
function execute(ApplicationInterface $scope = null);
function execute(ComponentInterface $scope = null);
}

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet\Resource\Command;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Component\ComponentInterface;
class Composite extends \SplQueue implements CommandInterface {
/**
@ -22,7 +22,7 @@ class Composite extends \SplQueue implements CommandInterface {
}
}
public function execute(ApplicationInterface $scope = null) {
public function execute(ComponentInterface $scope = null) {
$this->setIteratorMode(static::IT_MODE_DELETE);
$recursive = new self;

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet\Resource\Command;
use Ratchet\Resource\Connection;
use Ratchet\Resource\ConnectionInterface;
/**
* A factory pattern class to easily create all the things in the Ratchet\Resource\Command interface
@ -34,7 +34,7 @@ class Factory {
* @return CommandInterface
* @throws UnexpectedValueException
*/
public function newCommand($name, Connection $conn) {
public function newCommand($name, ConnectionInterface $conn) {
if (isset($this->_mapped_commands[$name])) {
$cmd = $this->_mapped_commands[$name];
return new $cmd($conn);

View File

@ -3,11 +3,10 @@ namespace Ratchet\Resource;
use Ratchet\SocketInterface;
/**
* @todo Consider if this belongs under Application
* @todo Construct should have StorageInterface, currently all is memory, should be different ones
* That will allow a queue system, communication between threaded connections
* A proxy object representing a connection to the application
* This acts as a container to storm data (in memory) about the connection
*/
class Connection {
class Connection implements ConnectionInterface {
protected $_data = array();
/**
@ -38,18 +37,14 @@ class Connection {
}
/**
* Set an attribute to the connection
* @param mixed
* @param mixed
* @{inheritdoc}
*/
public function __set($name, $value) {
$this->_data[$name] = $value;
}
/**
* Get a previously set attribute bound to the connection
* @return mixed
* @throws \InvalidArgumentException
* @{inheritdoc}
*/
public function __get($name) {
if (!$this->__isset($name)) {
@ -64,15 +59,14 @@ class Connection {
}
/**
* @param mixed
* @return bool
* @{inheritdoc}
*/
public function __isset($name) {
return isset($this->_data[$name]);
}
/**
* @param mixed
* @{inheritdoc}
*/
public function __unset($name) {
unset($this->_data[$name]);

View File

@ -0,0 +1,35 @@
<?php
namespace Ratchet\Resource;
interface ConnectionInterface {
/**
* @return int
*/
function getId();
/**
* Set an attribute to the connection
* @param mixed
* @param mixed
*/
function __set($name, $value);
/**
* Get a previously set attribute bound to the connection
* @return mixed
* @throws \InvalidArgumentException
*/
function __get($name);
/**
* @param mixed
* @return bool
*/
function __isset($name);
/**
* @param mixed
*/
function __unset($name);
}

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet;
use Ratchet\Application\ProtocolInterface;
use Ratchet\Component\ProtocolInterface;
/**
* A wrapper for the PHP socket_ functions
@ -205,28 +205,6 @@ class Socket implements SocketInterface {
return $res;
}
/**
* @param Ratchet\Application\ProtocolInterface
* @return Socket
* @throws Exception
*/
public static function createFromConfig(ProtocolInterface $protocol) {
$config = $protocol::getDefaultConfig();
$class = get_called_class();
$socket = new $class($config['domain'] ?: null, $config['type'] ?: null, $config['protocol'] ?: null);
if (is_array($config['options'])) {
foreach ($config['options'] as $level => $pair) {
foreach ($pair as $optname => $optval) {
$socket->set_option($level, $optname, $optval);
}
}
}
return $socket;
}
/**
* @internal
* @param int Specifies the protocol family to be used by the socket.

View File

@ -1,11 +1,11 @@
<?php
namespace Ratchet\Tests\Application\Server;
use Ratchet\Application\Server\App as ServerApp;
use Ratchet\Component\Server\App as ServerApp;
use Ratchet\Tests\Mock\FakeSocket as Socket;
use Ratchet\Tests\Mock\Application as TestApp;
/**
* @covers Ratchet\Application\Server\App
* @covers Ratchet\Component\Server\App
*/
class AppTest extends \PHPUnit_Framework_TestCase {
protected $_catalyst;
@ -17,7 +17,7 @@ class AppTest extends \PHPUnit_Framework_TestCase {
$this->_app = new TestApp;
$this->_server = new ServerApp($this->_app);
$ref = new \ReflectionClass('\Ratchet\Application\Server\App');
$ref = new \ReflectionClass('\Ratchet\Component\Server\App');
$prop = $ref->getProperty('_run');
$prop->setAccessible(true);
$prop->setValue($this->_server, false);

View File

@ -1,20 +0,0 @@
<?php
namespace Ratchet\Tests\Application\WebSocket;
use Ratchet\Application\WebSocket\App as RealApp;
use Ratchet\Tests\Mock\Socket;
use Ratchet\Tests\Mock\Application;
/**
* @covers Ratchet\Application\WebSocket\App
*/
class AppTest extends \PHPUnit_Framework_TestCase {
protected $_ws;
public function setUp() {
$this->_ws = new RealApp(new Application);
}
public function testGetConfigReturnsArray() {
$this->assertInternalType('array', $this->_ws->getDefaultConfig());
}
}

View File

@ -1,9 +1,9 @@
<?php
namespace Ratchet\Tests\Application\WebSocket\Version;
use Ratchet\Application\WebSocket\Version\Hixie76;
use Ratchet\Component\WebSocket\Version\Hixie76;
/**
* @covers Ratchet\Application\WebSocket\Version\Hixie76
* @covers Ratchet\Component\WebSocket\Version\Hixie76
*/
class Hixie76Test extends \PHPUnit_Framework_TestCase {
protected $_version;
@ -13,7 +13,7 @@ class Hixie76Test extends \PHPUnit_Framework_TestCase {
}
public function testClassImplementsVersionInterface() {
$constraint = $this->isInstanceOf('\\Ratchet\\Application\\WebSocket\\Version\\VersionInterface');
$constraint = $this->isInstanceOf('\\Ratchet\\Component\\WebSocket\\Version\\VersionInterface');
$this->assertThat($this->_version, $constraint);
}

View File

@ -1,10 +1,10 @@
<?php
namespace Ratchet\Tests\Application\WebSocket\Version;
use Ratchet\Application\WebSocket\Version\HyBi10;
use Ratchet\Application\WebSocket\Version\RFC6455\Frame;
use Ratchet\Component\WebSocket\Version\HyBi10;
use Ratchet\Component\WebSocket\Version\RFC6455\Frame;
/**
* @covers Ratchet\Application\WebSocket\Version\Hybi10
* @covers Ratchet\Component\WebSocket\Version\Hybi10
*/
class HyBi10Test extends \PHPUnit_Framework_TestCase {
protected $_version;
@ -17,7 +17,7 @@ class HyBi10Test extends \PHPUnit_Framework_TestCase {
* Is this useful?
*/
public function testClassImplementsVersionInterface() {
$constraint = $this->isInstanceOf('\\Ratchet\\Application\\WebSocket\\Version\\VersionInterface');
$constraint = $this->isInstanceOf('\\Ratchet\\Component\\WebSocket\\Version\\VersionInterface');
$this->assertThat($this->_version, $constraint);
}

View File

@ -1,9 +1,9 @@
<?php
namespace Ratchet\Tests\Application\WebSocket\Version\RFC6455;
use Ratchet\Application\WebSocket\Version\RFC6455\Frame;
use Ratchet\Component\WebSocket\Version\RFC6455\Frame;
/**
* @covers Ratchet\Application\WebSocket\Version\RFC6455\Frame
* @covers Ratchet\Component\WebSocket\Version\RFC6455\Frame
* @todo getMaskingKey, getPayloadStartingByte don't have tests yet
* @todo Could use some clean up in general, I had to rush to fix a bug for a deadline, sorry.
*/

View File

@ -1,13 +1,13 @@
<?php
namespace Ratchet\Tests\Application\WebSocket\Version\RFC6455;
use Ratchet\Application\WebSocket\Version\RFC6455\HandshakeVerifier;
use Ratchet\Component\WebSocket\Version\RFC6455\HandshakeVerifier;
/**
* @covers Ratchet\Application\WebSocket\Version\RFC6455\HandshakeVerifier
* @covers Ratchet\Component\WebSocket\Version\RFC6455\HandshakeVerifier
*/
class HandshakeVerifierTest extends \PHPUnit_Framework_TestCase {
/**
* @var Ratchet\Application\WebSocket\Version\RFC6455\HandshakeVerifier
* @var Ratchet\Component\WebSocket\Version\RFC6455\HandshakeVerifier
*/
protected $_v;

View File

@ -1,11 +1,11 @@
<?php
namespace Ratchet\Tests\Application\WebSocket\Version;
use Ratchet\Application\WebSocket\Version\RFC6455;
use Ratchet\Application\WebSocket\Version\RFC6455\Frame;
use Ratchet\Component\WebSocket\Version\RFC6455;
use Ratchet\Component\WebSocket\Version\RFC6455\Frame;
use Guzzle\Http\Message\RequestFactory;
/**
* @covers Ratchet\Application\WebSocket\Version\RFC6455
* @covers Ratchet\Component\WebSocket\Version\RFC6455
*/
class RFC6455Test extends \PHPUnit_Framework_TestCase {
protected $_version;
@ -18,7 +18,7 @@ class RFC6455Test extends \PHPUnit_Framework_TestCase {
* Is this useful?
*/
public function testClassImplementsVersionInterface() {
$constraint = $this->isInstanceOf('\\Ratchet\\Application\\WebSocket\\Version\\VersionInterface');
$constraint = $this->isInstanceOf('\\Ratchet\\Component\\WebSocket\\Version\\VersionInterface');
$this->assertThat($this->_version, $constraint);
}

View File

@ -1,10 +1,10 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\Application\ApplicationInterface;
use Ratchet\Component\ComponentInterface;
use Ratchet\Tests\Mock\Socket as MockSocket;
use Ratchet\Resource\Connection;
use Ratchet\Resource\ConnectionInterface;
class Application implements ApplicationInterface {
class Application implements ComponentInterface {
public $_app;
public $_conn_open;
@ -17,25 +17,25 @@ class Application implements ApplicationInterface {
public $_conn_error;
public $_excep_error;
public function __construct(ApplicationInterface $app = null) {
public function __construct(ComponentInterface $app = null) {
// probably should make this null app
$this->_app = $app;
}
public function onOpen(Connection $conn) {
public function onOpen(ConnectionInterface $conn) {
$this->_conn_open = $conn;
}
public function onMessage(Connection $from, $msg) {
public function onMessage(ConnectionInterface $from, $msg) {
$this->_conn_recv = $from;
$this->_msg_recv = $msg;
}
public function onClose(Connection $conn) {
public function onClose(ConnectionInterface $conn) {
$this->_conn_close = $conn;
}
public function onError(Connection $conn, \Exception $e) {
public function onError(ConnectionInterface $conn, \Exception $e) {
$this->_conn_error = $conn;
$this->_excep_error = $e;
}