Factory Caching

If a Command class was found in the factory save its class name. This prevents looping and checking 'class_exists' if it's already been found before.
This commit is contained in:
Chris Boden 2012-01-17 20:39:07 -05:00
parent 9b14684cbe
commit 5e82fc76c6

View File

@ -8,6 +8,8 @@ use Ratchet\Resource\Connection;
class Factory { class Factory {
protected $_paths = array(); protected $_paths = array();
protected $_mapped_commands = array();
public function __construct() { public function __construct() {
$this->addActionPath(__NAMESPACE__ . '\\Action'); $this->addActionPath(__NAMESPACE__ . '\\Action');
} }
@ -33,21 +35,21 @@ class Factory {
* @throws UnexpectedValueException * @throws UnexpectedValueException
*/ */
public function newCommand($name, Connection $conn) { public function newCommand($name, Connection $conn) {
$cmd = null; if (isset($this->_mapped_commands[$name])) {
$cmd = $this->_mapped_commands[$name];
return new $cmd($conn);
}
foreach ($this->_paths as $path) { foreach ($this->_paths as $path) {
if (class_exists($path . $name)) { if (class_exists($path . $name)) {
$cmd = $path . $name; $this->_mapped_commands[$name] = $path . $name;
break; return $this->newCommand($name, $conn);
} }
} }
if (null === $cmd) {
throw new \UnexepctedValueException("Command {$name} not found"); throw new \UnexepctedValueException("Command {$name} not found");
} }
return new $cmd($conn);
}
/** /**
* @param string * @param string
* @return string * @return string