From 0de53cf7ee1b6c48ac71debf5905d1f74b8bdd3f Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sun, 6 Nov 2011 18:24:50 -0500 Subject: [PATCH] Refactored Commands --- .../Command/{ => Action}/CloseConnection.php | 3 +- lib/Ratchet/Command/{ => Action}/Null.php | 3 +- lib/Ratchet/Command/{ => Action}/Runtime.php | 3 +- .../Command/{ => Action}/SendMessage.php | 3 +- lib/Ratchet/Command/Composite.php | 2 +- lib/Ratchet/Command/Factory.php | 44 +++++++++++++++++++ lib/Ratchet/Protocol/WebSocket.php | 2 +- .../WebSocket/Command/Action}/Ping.php | 0 .../WebSocket/Command/Action}/Pong.php | 0 9 files changed, 54 insertions(+), 6 deletions(-) rename lib/Ratchet/Command/{ => Action}/CloseConnection.php (84%) rename lib/Ratchet/Command/{ => Action}/Null.php (78%) rename lib/Ratchet/Command/{ => Action}/Runtime.php (90%) rename lib/Ratchet/Command/{ => Action}/SendMessage.php (93%) create mode 100644 lib/Ratchet/Command/Factory.php rename lib/Ratchet/{Command => Protocol/WebSocket/Command/Action}/Ping.php (100%) rename lib/Ratchet/{Command => Protocol/WebSocket/Command/Action}/Pong.php (100%) diff --git a/lib/Ratchet/Command/CloseConnection.php b/lib/Ratchet/Command/Action/CloseConnection.php similarity index 84% rename from lib/Ratchet/Command/CloseConnection.php rename to lib/Ratchet/Command/Action/CloseConnection.php index 41308de..cd7b374 100644 --- a/lib/Ratchet/Command/CloseConnection.php +++ b/lib/Ratchet/Command/Action/CloseConnection.php @@ -1,5 +1,6 @@ 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 . '\\'); + } +} \ No newline at end of file diff --git a/lib/Ratchet/Protocol/WebSocket.php b/lib/Ratchet/Protocol/WebSocket.php index 80e5a55..5ca2ad6 100644 --- a/lib/Ratchet/Protocol/WebSocket.php +++ b/lib/Ratchet/Protocol/WebSocket.php @@ -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; /** diff --git a/lib/Ratchet/Command/Ping.php b/lib/Ratchet/Protocol/WebSocket/Command/Action/Ping.php similarity index 100% rename from lib/Ratchet/Command/Ping.php rename to lib/Ratchet/Protocol/WebSocket/Command/Action/Ping.php diff --git a/lib/Ratchet/Command/Pong.php b/lib/Ratchet/Protocol/WebSocket/Command/Action/Pong.php similarity index 100% rename from lib/Ratchet/Command/Pong.php rename to lib/Ratchet/Protocol/WebSocket/Command/Action/Pong.php