From d91a88219b0c26a597173714b9e1e29462701f72 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Thu, 26 Apr 2012 20:22:55 -0400 Subject: [PATCH] [WAMP] Static Factory I'm sorry... refs #11 Optionally, statically register namespaces for Command Factory --- .../Component/WAMP/WAMPServerComponent.php | 3 +++ src/Ratchet/Resource/Command/Factory.php | 24 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Ratchet/Component/WAMP/WAMPServerComponent.php b/src/Ratchet/Component/WAMP/WAMPServerComponent.php index d3f5dc1..1f92993 100644 --- a/src/Ratchet/Component/WAMP/WAMPServerComponent.php +++ b/src/Ratchet/Component/WAMP/WAMPServerComponent.php @@ -4,6 +4,7 @@ use Ratchet\Component\WebSocket\WebSocketComponentInterface; use Ratchet\Resource\ConnectionInterface; use Ratchet\Resource\Command\Composite; use Ratchet\Resource\Command\CommandInterface; +use Ratchet\Resource\Command\Factory as CmdFactory; use Ratchet\Component\WAMP\Command\Action\Prefix; use Ratchet\Component\WAMP\Command\Action\Welcome; @@ -168,6 +169,8 @@ class WAMPServerComponent implements WebSocketComponentInterface { * @param WAMPServerComponentInterface An class to propagate calls through */ public function __construct(WAMPServerComponentInterface $server_component) { + CmdFactory::registerActionPath(__NAMESPACE__ . '\\Command\\Action'); + $this->_decorating = $server_component; $this->_msg_buffer = new Composite; } diff --git a/src/Ratchet/Resource/Command/Factory.php b/src/Ratchet/Resource/Command/Factory.php index 1de6b6d..ebd2cc8 100644 --- a/src/Ratchet/Resource/Command/Factory.php +++ b/src/Ratchet/Resource/Command/Factory.php @@ -10,8 +10,16 @@ class Factory { protected $_mapped_commands = array(); - public function __construct() { + protected static $globalPaths = array(); + + protected $_ignoreGlobals = false; + + /** + * @param bool If set to TRUE this will ignore all the statically registered namespaces + */ + public function __construct($ignoreGlobals = false) { $this->addActionPath(__NAMESPACE__ . '\\Action'); + $this->_ignoreGlobals = (boolean)$ignoreGlobals; } /** @@ -22,6 +30,10 @@ class Factory { $this->_paths[] = $this->slashIt($namespace); } + public static function registerActionPath($namespace) { + static::$globalPaths[$namespace] = 1; + } + /** * @return Composite */ @@ -47,6 +59,16 @@ class Factory { } } + if (false === $this->_ignoreGlobals) { + foreach (static::$globalPaths as $path => $one) { + $path = $this->slashIt($path); + if (class_exists($path . $name)) { + $this->_mapped_commands[$name] = $path . $name; + return $this->newCommand($name, $conn); + } + } + } + throw new \UnexepctedValueException("Command {$name} not found"); }