Continuing logical NS refactor

Updated more components to reflect new terminology when using Ratchet components
This commit is contained in:
Chris Boden 2012-01-31 20:47:59 -05:00
parent 149a99cf7c
commit 2c976d0102
14 changed files with 38 additions and 78 deletions

View File

@ -17,7 +17,7 @@
, "autoload": {
"psr-0": {
"Ratchet\\Tests": "tests"
, "Ratchet": "lib"
, "Ratchet": "src"
}
}
, "repositories": {

2
composer.lock generated
View File

@ -1,5 +1,5 @@
{
"hash": "75f85f47f6365abbeeabb065601c6ec2",
"hash": "9c9347f555af9a961102c4158e6d2ae8",
"packages": [
{
"package": "guzzle",

View File

@ -9,7 +9,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
*/
class App implements MessageComponentInterface {
class IOServerComponent implements MessageComponentInterface {
/**
* @var array of Socket Resources
*/
@ -145,6 +145,9 @@ class App implements MessageComponentInterface {
}
}
/**
* @{inheritdoc}
*/
public function onOpen(ConnectionInterface $conn) {
$new_socket = clone $conn->getSocket();
$new_socket->set_nonblock();
@ -156,10 +159,16 @@ class App implements MessageComponentInterface {
return $this->_decorating->onOpen($new_connection);
}
/**
* @{inheritdoc}
*/
public function onMessage(ConnectionInterface $from, $msg) {
return $this->_decorating->onMessage($from, $msg);
}
/**
* @{inheritdoc}
*/
public function onClose(ConnectionInterface $conn) {
$resource = $conn->getSocket()->getResource();
@ -170,6 +179,9 @@ class App implements MessageComponentInterface {
return $cmd;
}
/**
* @{inheritdoc}
*/
public function onError(ConnectionInterface $conn, \Exception $e) {
return $this->_decorating->onError($conn, $e);
}

View File

@ -25,7 +25,7 @@ use Ratchet\Component\WAMP\Command\Action\Prefix;
* @link http://www.tavendo.de/autobahn/protocol.html
* @link https://raw.github.com/oberstet/Autobahn/master/lib/javascript/autobahn.js
*/
class WAMPComponent implements WebSocketAppInterface {
class WAMPServerComponent implements WebSocketAppInterface {
const MSG_WELCOME = 0;
const MSG_PREFIX = 1;
const MSG_CALL = 2;

View File

@ -1,5 +1,6 @@
<?php
namespace Ratchet\Component\WAMP;
use Ratchet\Component\ComponentInterface;
use Ratchet\Resource\ConnectionInterface;
/**
@ -7,7 +8,7 @@ use Ratchet\Resource\ConnectionInterface;
* 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)
*/
interface WAMPServerComponentInterface {
interface WAMPServerComponentInterface extends ComponentInterface {
/**
* When a new connection is opened it will be passed to this method
* @param Ratchet\Resource\Connection

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet\Component\WebSocket;
use Ratchet\Component\ComponentInterface;
use Ratchet\Component\MessageComponentInterface;
use Ratchet\Resource\ConnectionInterface;
use Ratchet\Resource\Command\Factory;
use Ratchet\Resource\Command\CommandInterface;
@ -14,10 +14,10 @@ use Ratchet\Component\WebSocket\Guzzle\Http\Message\RequestFactory;
* @link http://ca.php.net/manual/en/ref.http.php
* @link http://dev.w3.org/html5/websockets/
*/
class WebSocketComponent implements ComponentInterface {
class WebSocketComponent implements MessageComponentInterface {
/**
* Decorated component
* @var Ratchet\Component\ComponentInterface
* @var Ratchet\Component\MessageComponentInterface
*/
protected $_decorating;
@ -46,7 +46,7 @@ class WebSocketComponent implements ComponentInterface {
*/
public $accepted_subprotocols = array();
public function __construct(ComponentInterface $component) {
public function __construct(MessageComponentInterface $component) {
$this->_decorating = $component;
$this->_factory = new Factory;
}

View File

@ -1,13 +1,13 @@
<?php
namespace Ratchet\Component\WebSocket;
use Ratchet\Component\ComponentInterface;
use Ratchet\Component\MessageComponentInterface;
/**
* @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 WebSocketComponentInterface extends ComponentInterface {
interface WebSocketComponentInterface extends MessageComponentInterface {
/**
* 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,6 +1,7 @@
<?php
namespace Ratchet\Resource\Command\Action;
use Ratchet\Resource\ConnectionInterface;
use Ratchet\Resource\Command\CommandInterface;
/**
* A single command tied to 1 socket connection

View File

@ -22,6 +22,9 @@ class Composite extends \SplQueue implements CommandInterface {
}
}
/**
* @{inheritdoc}
*/
public function execute(ComponentInterface $scope = null) {
$this->setIteratorMode(static::IT_MODE_DELETE);

View File

@ -1,23 +1,23 @@
<?php
namespace Ratchet\Tests\Application\Server;
use Ratchet\Component\Server\App as ServerApp;
use Ratchet\Component\Server\IOServerComponent;
use Ratchet\Tests\Mock\FakeSocket as Socket;
use Ratchet\Tests\Mock\Component as TestApp;
/**
* @covers Ratchet\Component\Server\App
* @covers Ratchet\Component\Server\IOServerComponent
*/
class AppTest extends \PHPUnit_Framework_TestCase {
class IOServerComponentTest extends \PHPUnit_Framework_TestCase {
protected $_catalyst;
protected $_server;
protected $_app;
protected $_decorated;
public function setUp() {
$this->_catalyst = new Socket;
$this->_app = new TestApp;
$this->_server = new ServerApp($this->_app);
$this->_decorated = new TestApp;
$this->_server = new IOServerComponent($this->_decorated);
$ref = new \ReflectionClass('\\Ratchet\\Component\\Server\\App');
$ref = new \ReflectionClass('\\Ratchet\\Component\\Server\\IOServerComponent');
$prop = $ref->getProperty('_run');
$prop->setAccessible(true);
$prop->setValue($this->_server, false);
@ -41,7 +41,7 @@ class AppTest extends \PHPUnit_Framework_TestCase {
$master = $this->getMasterConnection();
$this->_server->onOpen($master);
$clone = $this->_app->_conn_open;
$clone = $this->_decorated->_conn_open;
$this->assertEquals($master->getID() + 1, $clone->getID());
}
@ -54,12 +54,12 @@ class AppTest extends \PHPUnit_Framework_TestCase {
// that way can mimic the TCP fragmentation/buffer situation
$this->_server->onOpen($master);
$clone = $this->_app->_conn_open;
$clone = $this->_decorated->_conn_open;
// $this->_server->run($this->_catalyst);
$msg = 'Hello World!';
$this->_server->onMessage($clone, $msg);
$this->assertEquals($msg, $this->_app->_msg_recv);
$this->assertEquals($msg, $this->_decorated->_msg_recv);
}
}

View File

@ -1,2 +0,0 @@
<?php
namespace Composer\Autoload; class ClassLoader { private $prefixes = array(); private $fallbackDirs = array(); public function getPrefixes() { return $this->prefixes; } public function getFallbackDirs() { return $this->fallbackDirs; } public function add($prefix, $paths) { if (!$prefix) { $this->fallbackDirs = (array) $paths; return; } if (isset($this->prefixes[$prefix])) { $this->prefixes[$prefix] = array_merge( $this->prefixes[$prefix], (array) $paths ); } else { $this->prefixes[$prefix] = (array) $paths; } } public function register($prepend = false) { spl_autoload_register(array($this, 'loadClass'), true, $prepend); } public function loadClass($class) { if ($file = $this->findFile($class)) { require $file; return true; } } public function findFile($class) { if ('\\' == $class[0]) { $class = substr($class, 1); } if (false !== $pos = strrpos($class, '\\')) { $classPath = DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)); $className = substr($class, $pos + 1); } else { $classPath = null; $className = $class; } $classPath .= DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; foreach ($this->prefixes as $prefix => $dirs) { foreach ($dirs as $dir) { if (0 === strpos($class, $prefix)) { if (file_exists($dir . $classPath)) { return $dir . $classPath; } } } } foreach ($this->fallbackDirs as $dir) { if (file_exists($dir . $classPath)) { return $dir . $classPath; } } } }

View File

@ -1,22 +0,0 @@
<?php
// autoload.php generated by Composer
if (!class_exists('Composer\\Autoload\\ClassLoader', false)) {
require __DIR__.'/ClassLoader.php';
}
$__composer_autoload_init = function() {
$loader = new \Composer\Autoload\ClassLoader();
$map = require __DIR__.'/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->add($namespace, $path);
}
$loader->register();
return $loader;
};
return $__composer_autoload_init();

View File

@ -1,11 +0,0 @@
<?php
// autoload_namespace.php generated by Composer
$vendorDir = dirname(__DIR__);
return array(
'Ratchet\\Tests' => dirname($vendorDir) . '/tests',
'Ratchet' => dirname($vendorDir) . '/lib',
'Guzzle' => $vendorDir . '/guzzle/src',
);

View File

@ -1,22 +0,0 @@
[
{
"name": "guzzle",
"version": "2.0.2",
"version_normalized": "2.0.2.0",
"source": {
"type": "git",
"url": "https:\/\/github.com\/guzzle\/guzzle.git",
"reference": "ac64abc2c05b921efc4623379c1674a282475ae5"
},
"type": "library",
"names": [
"guzzle"
],
"installation-source": "source",
"autoload": {
"psr-0": {
"Guzzle": "src"
}
}
}
]