diff --git a/src/Server.php b/src/Server.php index 6ae9f2f3..9ee01c7c 100644 --- a/src/Server.php +++ b/src/Server.php @@ -23,6 +23,19 @@ public function __construct($uri, LoopInterface $loop, array $context = array()) 'unix' => array(), ); + $server = $this->getServer($uri, $loop, $context); + $this->server = $server; + + $that = $this; + $server->on('connection', function (ConnectionInterface $conn) use ($that) { + $that->emit('connection', array($conn)); + }); + $server->on('error', function (\Exception $error) use ($that) { + $that->emit('error', array($error)); + }); + } + + private function getServer($uri, $loop, $context) { $scheme = 'tcp'; $pos = strpos($uri, '://'); if ($pos !== false) { @@ -30,25 +43,17 @@ public function __construct($uri, LoopInterface $loop, array $context = array()) } if ($scheme === 'unix') { - $server = new UnixServer($uri, $loop, $context['unix']); + return new UnixServer($uri, $loop, $context['unix']); } - else { - $server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']); - if ($scheme === 'tls') { - $server = new SecureServer($server, $loop, $context['tls']); - } + $server = new TcpServer(str_replace('tls://', '', $uri), $loop, $context['tcp']); + + if ($scheme === 'tls') { + $server = new SecureServer($server, $loop, $context['tls']); } - $this->server = $server; + return $server; - $that = $this; - $server->on('connection', function (ConnectionInterface $conn) use ($that) { - $that->emit('connection', array($conn)); - }); - $server->on('error', function (\Exception $error) use ($that) { - $that->emit('error', array($error)); - }); } public function getAddress()