Refactored Commands

This commit is contained in:
Chris Boden 2011-11-06 18:24:50 -05:00
parent a6073a87eb
commit 0de53cf7ee
9 changed files with 54 additions and 6 deletions

View File

@ -1,5 +1,6 @@
<?php
namespace Ratchet\Command;
namespace Ratchet\Command\Action;
use Ratchet\Command\CommandInterface;
use Ratchet\SocketInterface;
/**

View File

@ -1,5 +1,6 @@
<?php
namespace Ratchet\Command;
namespace Ratchet\Command\Action;
use Ratchet\Command\CommandInterface;
use Ratchet\SocketInterface;
/**

View File

@ -1,5 +1,6 @@
<?php
namespace Ratchet\Command;
namespace Ratchet\Command\Action;
use Ratchet\Command\CommandInterface;
use Ratchet\SocketInterface;
class Runtime implements CommandInterface {

View File

@ -1,5 +1,6 @@
<?php
namespace Ratchet\Command;
namespace Ratchet\Command\Action;
use Ratchet\Command\CommandInterface;
use Ratchet\SocketInterface;
/**

View File

@ -8,7 +8,7 @@ class Composite extends \SplQueue {
* @param Ratchet\SocketInterface
* @return CommandInterface
*/
public function create($name, SocketInterface $socket) {
public function NOPEcreate($name, SocketInterface $socket) {
$class = __NAMESPACE__ . "\\{$name}\\";
if (!class_exists($class)) {
throw new \UnexpectedValueException("Command {$name} not found");

View File

@ -0,0 +1,44 @@
<?php
namespace Ratchet\Command;
use Ratchet\SocketInterface;
class Factory {
protected $_paths = array();
public function __construct() {
$this->addActionPath(__NAMESPACE__ . '\\Action');
}
public function addActionPath($namespace) {
$this->_paths[] = $this->slashIt($namespace);
}
public function newComposite() {
return new Composite;
}
/**
* @param string
* @return CommandInterface
* @throws UnexpectedValueException
*/
public function newCommand($name, SocketInterface $conn) {
$cmd = null;
foreach ($this->_paths as $path) {
if (class_exists($path . $name)) {
$cmd = $path . $name;
break;
}
}
if (null === $cmd) {
throw new \UnexepctedValueException("Command {$name} not found");
}
return new $cmd($conn);
}
protected function slashIt($ns) {
return (substr($ns, -1) == '\\' ? $ns : $ns . '\\');
}
}

View File

@ -7,7 +7,7 @@ use Ratchet\Protocol\WebSocket\VersionInterface;
use Ratchet\SocketInterface;
use Ratchet\SocketObserver;
use Ratchet\Command\CommandInterface;
use Ratchet\Command\SendMessage;
use Ratchet\Command\Action\SendMessage;
use Ratchet\Command\Composite;
/**