diff --git a/Phergie/Connection.php b/Phergie/Connection.php index df967f5..2cf7343 100644 --- a/Phergie/Connection.php +++ b/Phergie/Connection.php @@ -96,6 +96,13 @@ class Phergie_Connection */ protected $hostmask; + /** + * Stream context options + * + * @var array + */ + protected $context = array(); + /** * Constructor to initialize instance properties. * @@ -385,6 +392,28 @@ public function getPassword() return $this->password; } + /** + * Sets the stream context options to use. + * + * @param array $context Context options as they will be passed to stream_context_create() + * @return Phergie_Connection_Streams Implements a fluent interface + */ + public function setContext(array $context) + { + $this->context = $context; + return $this; + } + + /** + * Returns the stream context options in use. + * + * @return array Context options as they will be passed to stream_context_create() + */ + public function getContext() + { + return $this->context; + } + /** * Sets multiple connection settings using an array. * diff --git a/Phergie/Driver/Streams.php b/Phergie/Driver/Streams.php index 84f40fd..5699b04 100644 --- a/Phergie/Driver/Streams.php +++ b/Phergie/Driver/Streams.php @@ -342,13 +342,21 @@ public function getEvent() * @param string $remote Address to connect the socket to * @param int &$errno System level error number if connection fails * @param string &$errstr System level error message if connection fails + * @param array $context Optional socket context options * * @return resource Established socket */ - protected function connect($remote, &$errno, &$errstr) + protected function connect($remote, &$errno, &$errstr, array $context = array()) { // @codeCoverageIgnoreStart - return @stream_socket_client($remote, $errno, $errstr); + return @stream_socket_client( + $remote, + $errno, + $errstr, + ini_get('default_socket_timeout'), + STREAM_CLIENT_CONNECT, + stream_context_create($context) + ); // @codeCoverageIgnoreEnd } @@ -368,11 +376,12 @@ public function doConnect() $nick = $connection->getNick(); $realname = $connection->getRealname(); $transport = $connection->getTransport(); + $context = $connection->getContext(); // Establish and configure the socket connection $remote = $transport . '://' . $hostname . ':' . $port; $errno = $errstr = null; - $this->socket = $this->connect($remote, $errno, $errstr); + $this->socket = $this->connect($remote, $errno, $errstr, $context); if (!$this->socket) { throw new Phergie_Driver_Exception( 'Unable to connect: socket error ' . $errno . ' ' . $errstr, diff --git a/Settings.php.dist b/Settings.php.dist index 2a3a431..968d5c3 100644 --- a/Settings.php.dist +++ b/Settings.php.dist @@ -14,6 +14,7 @@ return array( // 'password' => 'password goes here if needed', // 'transport' => 'ssl', // uncomment to connect using SSL // 'encoding' => 'UTF-8', // uncomment if using UTF-8 + // 'context' => array('socket' => array('bindto' => '0.0.0.0:0')), // uncomment to force use of IPv4 ) ), diff --git a/Tests/Phergie/ConnectionTest.php b/Tests/Phergie/ConnectionTest.php index 72f5ed0..4fa79f4 100644 --- a/Tests/Phergie/ConnectionTest.php +++ b/Tests/Phergie/ConnectionTest.php @@ -44,6 +44,7 @@ class Phergie_ConnectionTest extends PHPUnit_Framework_TestCase 'username' => 'MyUsername', 'realname' => 'MyRealName', 'password' => 'MyPassword', + 'context' => array('socket' => array('bindto' => '0.0.0.0:0')), ); /** @@ -60,11 +61,12 @@ public function dataProviderTestGetOptionReturnsDefault() array('encoding', 'ISO-8859-1'), array('port', 6667), array('password', null), + array('context', array()), ); } /** - * Tests that a default values are used for some options. + * Tests that default values are used for some options. * * @param string $option Name of the option with a default value * @param mixed $value Default value of the option @@ -78,17 +80,6 @@ public function testGetOptionReturnsDefault($option, $value) $this->assertEquals($value, $connection->{'get' . ucfirst($option)}()); } - /** - * Tests that a default encoding is used if one isn't specified. - * - * @return void - */ - public function testGetEncodingReturnsDefault() - { - $connection = new Phergie_Connection; - $this->assertEquals('ISO-8859-1', $connection->getEncoding()); - } - /** * Tests that options can be set via the constructor. * diff --git a/Tests/Phergie/Driver/StreamsTest.php b/Tests/Phergie/Driver/StreamsTest.php index 7543ab1..7bb77d2 100644 --- a/Tests/Phergie/Driver/StreamsTest.php +++ b/Tests/Phergie/Driver/StreamsTest.php @@ -101,7 +101,8 @@ protected function getMockConnection() 'port' => 6667, 'username' => 'username', 'realname' => 'realname', - 'transport' => 'tcp' + 'transport' => 'tcp', + 'context' => array(), ); $connection = parent::getMockConnection(); foreach ($options as $key => $value) {